MATLAB Solve Matlab Code Problem: Nonlinear Fiber Optics

AI Thread Summary
The discussion revolves around troubleshooting a MATLAB script for simulating nonlinear fiber optics. The user encountered an error at line 3, specifically with the input function for fiber length. Suggestions included checking the MATLAB version compatibility, as certain functions may not exist in older versions like R2010. Additionally, it was advised to isolate and test individual lines of code to identify the source of the error. The user discovered that renaming a variable that conflicted with MATLAB's built-in functions resolved the issue. The conversation also touched on formatting code for better readability and sharing experiences with similar programming errors. Finally, the user inquired about exporting 3D figures to JPG or Word documents, indicating a successful resolution of the initial problem.
eahaidar
Messages
69
Reaction score
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
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?
 
Are you sure that you're running the code in the version of MATLAB it was written for?
 
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.
 
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
 
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
 
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.
 
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
 

Similar threads

Back
Top