function [xtU] = pot(n,xmin,xmax,potent,B,s,T,wrap) % parameters .......... dt = .0001; %time step k = 10; %wave number c = 2; %width % ................. dx = (xmax-xmin)/n; x = xmin + (1:n)'*dx; %x is col vector V = zeros(1,n); if potent == 0; %no potential. potent = 0 x0 = 2; else if potent == 1; for a = floor(5*n/12):floor(7*n/12) x0 = xmin + 5*n/8*dx + 3; %displacement V(a) = s; end else %x0 = 0; %V = s*x.^2; end end a = -1/(dx)^2 - V/2; b = 1/(2*dx^2); % creation of K the sparse matrix D = sparse(1:n,1:n,a,n,n); E = sparse(2:n,1:n-1,b*ones(1,n-1),n,n); E(1,n) = wrap*b; K = E+D+E'; U = c*sech(c*(x-x0)).*exp(-1i*k*x); I = speye(n,n); % keeps everything sparse t = 0; %using CN scheme in paper... Uold = U; Unew = zeros(n,1); modT = 10; xtU = zeros(n,(T + modT*dt)/(modT*dt)); %records U as time goes on while t < T if (mod(floor(t/dt),modT)==0) plot(x, [U.*conj(U) real(U) imag(U)], x, V); axis([xmin xmax -1 5]); text(1,1, sprintf('t=%g, prob=%g', t, dx*dot(U,U))); drawnow; end xtU(:,floor(t/(modT*dt) + 1)) = U.*conj(U); Unew = (I - 1i*dt*K)\((I + 1i*dt*K)*U - B*1i*dt*(3/2*conj(U).*U.*U - 1/2*conj(Uold).*Uold.*Uold)); Uold = U; U = Unew; t = t + dt; end