Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab code problem

  1. Nov 15, 2013 #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
    %-- plot input pulse shape and 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');
    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
    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

    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
  2. jcsd
  3. Nov 15, 2013 #2


    Staff: Mentor

    Last edited: Nov 15, 2013
  4. Nov 15, 2013 #3
    I will do it the next time but do you know the answer???
  5. Nov 16, 2013 #4


    User Avatar
    Gold Member

    Are you sure that you're running the code in the version of MATLAB it was written for?
  6. Nov 16, 2013 #5


    Staff: Mentor

    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 (Text):

    %---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.
  7. Nov 16, 2013 #6
    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
  8. Nov 16, 2013 #7
    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
  9. Nov 17, 2013 #8


    Staff: Mentor

    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.
  10. Nov 17, 2013 #9
    I know where is the error but I don't know how to solve it if can try it I really appreciate it thanks
  11. Nov 17, 2013 #10


    User Avatar
    Gold Member

    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.
  12. Nov 17, 2013 #11
    Actually it worked I just want to know if I can copy a 3d figure to a jpg or word do you know ???
  13. Nov 17, 2013 #12


    Staff: Mentor

    So what was the problem?
  14. Nov 18, 2013 #13
    Input was a name in matlab so I changed the name
  15. Nov 18, 2013 #14


    Staff: Mentor

    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.
  16. Nov 18, 2013 #15
    Hahahaha your. Is much funnier any way thanks a lot
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook