[Matlab] Numerical Code for the NLS equation

In summary, the NLS (Nonlinear Schrödinger) equation is a mathematical model used to study the behavior of waves in nonlinear media. Matlab is commonly used to implement the numerical code for the NLS equation due to its built-in functions for solving differential equations. The code works by discretizing the NLS equation and solving it iteratively using numerical methods. The NLS equation has various applications in different fields, including optics, fluid dynamics, and quantum mechanics. Additionally, the numerical code can be adapted for other equations, but may require modifications to fit the specific properties and behavior of the new equation.
  • #1
queenstudy
101
0
hello
i just copied the Numerical Code for the NLS equation which is the following:
%---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')


and as syntax there no mistake but when i wanted to run it they told me the following error:

Error in ==> Untitlednls at 3
distance= input('enter fiber length(in units of L_D)=');
what should i do even though i just copied it
 
Physics news on Phys.org
  • #2
I just copied and pasted the code in your post, and I don't get the same error as you, line 3 works fine.

However, I do get other errors; dtau is not defined, the line where the frequency grid is defined (line 14) is not working because the (I assume) there is a dot missing in front of the "*"

Hence, the code as is does not work.

I am using Matlab 2012b

Edit: Btw, I just realized that the error message you posted does not look like Matlab. Are you trying to run this in Python using scipy?
If so I don't think it will work without some modifications.
 
  • #3
no i am trying to run it in matlab2010 does it make any difference between different editons
 
  • #4
thats the exact error that is given to me

? Attempt to execute SCRIPT input as a function:
C:\Users\Abd Lebnen\Documents\MATLAB\input.m

Error in ==> leboz at 3
distance= input('enter fiber length (in units of L_D)= ');
 
  • #5
queenstudy said:
? Attempt to execute SCRIPT input as a function:
C:\Users\Abd Lebnen\Documents\MATLAB\input.m

Error in ==> leboz at 3
distance= input('enter fiber length (in units of L_D)= ');
If only you'd given the full error message before!

It looks like the problem is that instead of using the built-in "input" function, it's trying to use the contents of "C:\Users\Abd Lebnen\Documents\MATLAB\input.m" instead. You need to rename that file or move it somewhere else where MATLAB won't find it.
 

1. What is the NLS equation?

The NLS (Nonlinear Schrödinger) equation is a mathematical model that describes the evolution of a wave in a nonlinear medium, such as a laser or an optical fiber. It is widely used in physics, engineering, and other fields to study the behavior of nonlinear systems.

2. Why is Matlab used for the numerical code for the NLS equation?

Matlab is a powerful and widely used programming language and software environment for scientific computing. It has built-in functions and tools specifically designed for solving differential equations, making it a convenient and efficient choice for implementing the numerical code for the NLS equation.

3. How does the numerical code for the NLS equation work?

The numerical code for the NLS equation uses a finite difference method to discretize the NLS equation into a system of algebraic equations. These equations are then solved iteratively using numerical methods such as the Runge-Kutta method to approximate the solution of the NLS equation at different time steps.

4. What are some applications of the NLS equation?

The NLS equation has many applications in different fields, such as optics, fluid dynamics, and quantum mechanics. It is used to model and understand the behavior of nonlinear systems, including optical solitons, rogue waves, and Bose-Einstein condensates.

5. Can the numerical code for the NLS equation be adapted for other equations?

Yes, the numerical code for the NLS equation can be adapted for other equations by changing the specific equations and parameters used in the code. However, the code may need to be modified to fit the specific properties and behavior of the new equation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
735
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top