How to write a subroutine for initial condition in Nonlinear Schrodinger Equation that depends on x

121 Views Asked by At

I am solving Nonlinear Schrodinger equation by split-step Fourier method: i df/dz+1/2* d^2f/dX^2+|f|^2*f=0

using an initial condition: f=q*exp(-(X/X0)^24).

But I have to use the condition that q=1 for |x|<1, otherwise, q=0. So I write the following subroutine (excerpt of the code involving transverse variable) for transverse variable x:

   fs=120;
   N_fx=2^11; %number of points in frequency domain
   dX=1/fs;
   N_X=N_fx;
   X=(-(N_X-1)/2:(N_X-1)/2)*dX;
   X0=1;
   Xn=length(X);

   for m=1:Xn
   Xnn=Xn/8;
   pp=m;
   if pp>3*Xnn && pp<5*Xnn
   q=1.0;
   f=q*exp(-(X/X0).^24);
   else  
   f=0;
   end
   end

But it seems that 'f' is getting wrong and it is a 1 by 2048 vector with all entries zero. I'm not getting the expected result. If my initial condition is only f=q*exp(-(X/X0).^24), q=1, it is straightforward, but with the above condition (q=1 for |x|<1, otherwise, q=0) what I need to do? Any help will be much appreciated. Thanks in advance.

1

There are 1 best solutions below

1
On BEST ANSWER

A MWE, this has 0 < [f(867) : f(1162)] <= 1:

fs=120;
N_fx=2^11; %number of points in frequency domain
dX=1/fs;
N_X=N_fx;
X=(-(N_X-1)/2:(N_X-1)/2)*dX;
X0=1;
Xn=length(X);
f = zeros(1,Xn);  % new: preallocate f size and initialise it to 0

for m=1:Xn
    Xnn=Xn/8;
    if m>3*Xnn && m<5*Xnn
   %if abs(X(m)) < 1   %alternative to line above
        q=1.0;
        % error was here below: you overwrote a 1x1 f at each iteration
        f(m)=q*exp(-(X(m)/X0).^24);   
    else
        f(m)=0;
    end
end