% 
%  This Matlab script file solves the nonlinear Schrodinger equation
%  with the first order split-step Fourier method due to Hardin and Tappert. 
%
%                              Andre Weideman, 1995

N = input(' N = ? ');          % Number of Fourier modes
q = input(' q = ? ');          % Coefficient of nonlinear term 
dt = input(' dt = ? ');        % Time step
M = input(' M = ? ');          % Total number of time steps
J = input(' J  = ? ');         % Steps between output
L = input(' L = ? ');          % Space period
u0 = input(' u0(x) = ? ');     % Initial condition: string function 

h = L/N;                       % Space step
n = [-N/2:1:N/2-1]';           % Indices
x = n*h;                       % Grid points

u = eval(u0); U = u;           % Compute initial condition; save it in U
e = -4*n.*n*pi*pi/L/L;         % Squares of wavenumbers.

for m = 1:1:M                  % Start time evolution

u = exp(dt*i*q*(abs(u).*abs(u))).*u;  % Solve nonlinear part of NLS
c = fftshift(fft(u));                 % Take Fourier transform
c = exp(dt*i*e).*c;                   % Advance in Fourier space
u = ifft(fftshift(c));                % Return to physical space 

if rem(m,J) == 0                      % Save output every J steps.
U = [U u];                           
disp([m M])
end
                                      % Solution is stored in U.
end                                   % Display with mesh(abs(U))









