Solve Matlab Code Problem: Nonlinear Fiber Optics

In summary,I took this code from the agrawal book: nonlinear fiber optics and I wanted to see the results. I copied and pasted the code, but when I tried to run it, I got an error. I think the error means my script (your script has no name so its untitled) has an error at line #3, the 'input' line.
  • #1
eahaidar
71
1
I took this code from the agrawal book: nonlinear fiber optics and I wanted to see the results :

%---specify input parameters
clear all;
distance= input('enter fiber length (in units of L_D)= ');
beta2=input(' dispersion: 1 for normal, -1 for anomalous');
N=input('Nonlinear parameter N=');
mshape=input('m=0 for sech , m>0 for super-Gaussian =');
chirp0=0; % input pulse chirp
%--- set stimulation parameters
nt=1024; Tmax=32; % FFT points and window size
step_num=round(20*distance*N^2); % number of z steps
deltaz=distance/step_num; % step size in tau
%--- tau and omega arrays
tau=(-nt/2:nt/2-1)*dtau; % temporal grid
omega=(pi/Tmax)*[(0:nt/2-1)*(-nt/2 :-1)];% frequency grid
%--- input field profile
if mshape==0
uu= sech(tau)*exxp(-0.5i*chirp0*tau.^2); %soliton
else %super Gaussian
uu=exp((-0.5*(1+1i*chirp0)*tau.^(2*mshape)));
end
%-- plot input pulse shape and spectrum
temp=fftshift(ifft(uu)).*(nt*dtau)/sqrt(2*pi);%spectrum
figure; subplot(2,1,1);
plot(tau,abs(uu).^2,'--k');hold on;
axis([-5 5 0 inf]);
xlabel('normalized time');
yalebl('normalized power');
title('input and output pulse shape and spectrum');
subplot(2,1,2);
plot(fftshift(omega)/(2*pi),abs(temp).^2,'--k');hold on;
axis([-.5 .5 0 inf]);
xlabel('normalized frequency');
ylabel('spectral power');
%---store dispersive phase shifts to speedup code
dispersion=exp(1i*0.5*beta2*omega.^2 *deltaz);%phase factor
hhz=1i*N^2*deltaz; % nonlinear phase factor
%******[beginning of main loop]****
%scheme:1/2N -> D-> 1/2N; first half step nonlinear
temp=uu.*exp(abs(uu).^2.*hhz/2); %note hhz/2
for n=1:step_num
f_temp=ifft(temp).*dispersion;
u=fft(f_temp);
temp=uu.*exp(abs(uu).^2.*hhz);
end
uu=temp.*exp(-abs(uu).^2.*hhz/2); % final field
temp=fftshift(ifft(uu)).*(nt*dtau)/sqrt(2*pi);%final spectrum
%*****[end of main loop]********

%----- plot output pulse shape and spectrum
subplot(2,1,1)
plot(tau,abs(uu).^2,'-k')
subplot(2,1,2)
plot(fftshift(omega)/(2*pi),abs(temp).^2,'-k')

The error was the following:
Error in ==> Untitlednls at 3
distance= input('enter fiber length(in units of L_D)=');
in line 3 i wrote
distance= input('enter fiber length(in units of L_D)=');Any advice or help please thank you
 
Physics news on Phys.org
  • #3
jedishrfu said:
to make your post more readable please edit it and put in
Code:
tags

this will preserve the spacing.

I will do it the next time but do you know the answer?
 
  • #4
Are you sure that you're running the code in the version of MATLAB it was written for?
 
  • #5
eahaidar said:
I will do it the next time but do you know the answer?

No, I don't and your code needs formatting. Its takes seconds to edit your post and add the [ code ] ... [ /code ] tags around your block of code.

Code:
%---specify input parameters
clear all;
distance= input('enter fiber length (in units of L_D)= ');
beta2=input(' dispersion: 1 for normal, -1 for anomalous');
N=input('Nonlinear parameter N=');
mshape=input('m=0 for sech , m>0 for super-Gaussian =');
chirp0=0; % input pulse chirp

Try running the input line by itself from in the same way you ran this script, then try the same with the clear all; line added and then with the %--line added. This will show if the one of those statements has a problem.

Also did you copy and paste this script from somewhere? sometimes that will bring in extra unseen trailing characters like tabs, spaces, or it will strip off lifefeeds or carriage returns. So it may work if you retype the lines causing the problem.

I think the error means your script (your script has no name so its untitled) has an error at line #3, the 'input' line which means that maybe the prior line, the 'clear all;' line has a problem.
 
  • #6
jedishrfu said:
No, I don't and your code needs formatting. Its takes seconds to edit your post and add the [ code ] ... [ /code ] tags around your block of code.

Code:
%---specify input parameters
clear all;
distance= input('enter fiber length (in units of L_D)= ');
beta2=input(' dispersion: 1 for normal, -1 for anomalous');
N=input('Nonlinear parameter N=');
mshape=input('m=0 for sech , m>0 for super-Gaussian =');
chirp0=0; % input pulse chirp

Try running the input line by itself from in the same way you ran this script, then try the same with the clear all; line added and then with the %--line added. This will show if the one of those statements has a problem.

Also did you copy and paste this script from somewhere? sometimes that will bring in extra unseen trailing characters like tabs, spaces, or it will strip off lifefeeds or carriage returns. So it may work if you retype the lines causing the problem.

I think the error means your script (your script has no name so its untitled) has an error at line #3, the 'input' line which means that maybe the prior line, the 'clear all;' line has a problem.

I copied it from a book and I like triple checked it so do I remove the clear all you mean or what should i do exactly
 
  • #7
kreil said:
Are you sure that you're running the code in the version of MATLAB it was written for?

That's what I want to know does it make a difference if I have matlab2010 and requires a different MATLAB even though it is not written which MATLAB I should use
 
  • #8
eahaidar said:
I copied it from a book and I like triple checked it so do I remove the clear all you mean or what should i do exactly

You're getting an error at line 3 in your script so you have to ask yourself is it correct?

You test it by entering it by itself and see if MATLAB accepts it.

If that works then try adding in the comment line and if that works then add in the clear all line.

I'm trying to show you how to diagnose the problem. I don't have MATLAB readily available but I do have it at work and if you don't find the error then I'll try it on my copy which r2011a I think.
 
  • #9
jedishrfu said:
You're getting an error at line 3 in your script so you have to ask yourself is it correct?

You test it by entering it by itself and see if MATLAB accepts it.

If that works then try adding in the comment line and if that works then add in the clear all line.

I'm trying to show you how to diagnose the problem. I don't have MATLAB readily available but I do have it at work and if you don't find the error then I'll try it on my copy which r2011a I think.

I know where is the error but I don't know how to solve it if can try it I really appreciate it thanks
 
  • #10
eahaidar said:
That's what I want to know does it make a difference if I have matlab2010 and requires a different MATLAB even though it is not written which MATLAB I should use

Yes, it matters a great deal. If you run MATLAB code in a version it wasn't written for, it's possible certain functionality is missing that prevents it from running properly.

You might, for example, be unable to run this code because the input() function is not in R2010 (I'm not sure whether it is or not).

When I ran this in R2012a, I got a different error. The variable dtau is multiplied by before ever being defined.
 
  • #11
kreil said:
Yes, it matters a great deal. If you run MATLAB code in a version it wasn't written for, it's possible certain functionality is missing that prevents it from running properly.

You might, for example, be unable to run this code because the input() function is not in R2010 (I'm not sure whether it is or not).

When I ran this in R2012a, I got a different error. The variable dtau is multiplied by before ever being defined.

Actually it worked I just want to know if I can copy a 3d figure to a jpg or word do you know ?
 
  • #12
eahaidar said:
Actually it worked I just want to know if I can copy a 3d figure to a jpg or word do you know ?

So what was the problem?
 
  • #13
jedishrfu said:
So what was the problem?

Input was a name in MATLAB so I changed the name
 
  • #14
eahaidar said:
Input was a name in MATLAB so I changed the name

thats a good one.

I had one like that with GE basic where i entered a for loop

for i=1 to z step 2 which worked

and then decided z was a poor choice for time and thought t isn't being used

for i=1 to t step 2

but the basic interpreter saw it differently it saw

for i=1 to tst...

and stopped saying it expected a (

it had an implicitly defined function called tst() and bad parsing

i had to write a trouble report for this bug.
 
  • #15
jedishrfu said:
thats a good one.

I had one like that with GE basic where i entered a for loop

for i=1 to z step 2 which worked

and then decided z was a poor choice for time and thought t isn't being used

for i=1 to t step 2

but the basic interpreter saw it differently it saw

for i=1 to tst...

and stopped saying it expected a (

it had an implicitly defined function called tst() and bad parsing

i had to write a trouble report for this bug.

Hahahaha your. Is much funnier any way thanks a lot
 

FAQ: Solve Matlab Code Problem: Nonlinear Fiber Optics

1. How do I solve a nonlinear fiber optics problem using Matlab code?

To solve a nonlinear fiber optics problem using Matlab code, you will need to first define the parameters of the problem, such as the nonlinear coefficient and the input signal. Then, you can use the built-in functions and algorithms in Matlab to simulate the propagation of the signal through the fiber and obtain the output signal. It is also important to check for convergence and adjust the simulation parameters as needed.

2. What are some common challenges when solving nonlinear fiber optics problems with Matlab code?

One common challenge is ensuring the accuracy of the results. Nonlinear fiber optics problems can be complex and require a large number of iterations to obtain accurate solutions. Another challenge is choosing the appropriate simulation parameters, such as the step size and simulation time, to balance accuracy and computational efficiency.

3. Can Matlab code be used to solve real-world nonlinear fiber optics problems?

Yes, Matlab code can be used to solve real-world nonlinear fiber optics problems. However, it is important to validate the results with experimental data and adjust the simulation parameters accordingly. Real-world problems may also require more advanced techniques, such as incorporating noise and non-ideal components, to accurately simulate the behavior of the fiber.

4. Are there any resources available to help with solving nonlinear fiber optics problems using Matlab code?

Yes, there are many resources available, such as online tutorials, forums, and textbooks, that can help with solving nonlinear fiber optics problems using Matlab code. Additionally, Matlab has its own documentation and support resources that can assist with specific questions and troubleshooting.

5. Can I use Matlab code to optimize the design of a nonlinear fiber optic system?

Yes, Matlab code can be used for optimization purposes in nonlinear fiber optic systems. By defining the objective function and constraints, Matlab's built-in optimization algorithms can help find the optimal parameters for a given design. However, it is important to note that the accuracy of the optimization results depends on the accuracy of the simulation model.

Similar threads

Replies
4
Views
5K
Replies
1
Views
2K
Replies
1
Views
3K
Back
Top