clear all; close all % 1D nonlinear Schrodinger equation % % i u_t + 1/2 u_xx + |u|^2 u = 0 % L=20; n=128; tspan=0:1:10; x2=linspace(-L/2,L/2,n+1); x=x2(1:n); k=(2*pi/L)*[0:n/2-1 -n/2:-1]; u=sech(x); ut=fft(u); [t,utsol]=ode45('rhs1',tspan,ut,[],k); for j=1:length(t) usol(:,j)=ifft( utsol(j,:).' ); end figure(1), surf(t,x,abs(usol)); figure(2), surfl(t,x,abs(usol)); figure(3), surfc(t,x,abs(usol)); figure(4), mesh(t,x,abs(usol)); figure(5), waterfall(x,t,abs(usol')); % 2D coupled nonlinear heat equation % % u_t = u_xx + u_yy + v^2 % v_t = v_xx + v_yy + u^2 % Lx=20; nx=64; Ly=20; ny=64; tspan2=[0:0.1:1]; x2=linspace(-Lx/2,Lx/2,nx+1); x=x2(1:nx); y2=linspace(-Ly/2,Ly/2,ny+1); y=y2(1:ny); kx=(2*pi/Lx)*[0:nx/2-1 -nx/2:-1]; ky=(2*pi/Ly)*[0:ny/2-1 -ny/2:-1]; [X,Y]=meshgrid(x,y); [KX,KY]=meshgrid(kx,ky); kx2=reshape(KX,nx*ny,1); ky2=reshape(KY,nx*ny,1); u=exp(-X.^2-Y.^2); v=exp(-X.^2-Y.^2); ut=fft2(u); vt=fft2(v); ut2=reshape(ut,nx*ny,1); vt2=reshape(vt,nx*ny,1); uvt=[ut2; vt2]; [t,uvtsol]=ode45('rhs2',tspan2,uvt,[],kx2,kx2,ny,ny); for j=1:9 u2t=uvtsol(j,1:nx*ny); v2t=uvtsol(j,nx*ny+1:2*nx*ny); u=ifft2( reshape(u2t,nx,ny) ); v=ifft2( reshape(v2t,nx,ny) ); figure(6), subplot(3,3,j), surf(real(u)) figure(7), subplot(3,3,j), surf(real(v)) end