%===================================================================== % Solving PDE's in spectral space % % Computer Lab for Math 221, Fall 2002 % Sorin Mitran 09/24/2002 %===================================================================== % Clear previous work clc; clf; clear; % I. Advection equation % % q_t + u q_x = 0 % q(x,t=0)=q0(x) 0<=x<=2 pi % u=1 n=64; dx=2*pi/n; x=(0:n-1)*dx+dx/2; t=0.; tfinal=2.0; nmodesmax=4; u=1; % Initial condition is built up as a sum of Fourier modes for nmodes=1:nmodesmax q0=zeros(size(x)); a0=zeros([nmodes 1]); b0=zeros([nmodes 1]); for k=1:nmodes a0(k)=rand; b0(k)=rand; q0 = q0 + a0(k) * cos(k*x) + b0(k) * sin(k*x); end figure(1); plot(x,q0); in_ax=axis; txt = strcat('Initial condition. nmodes = ',num2str(nmodes)); title(txt); xlabel('x'); ylabel('q0'); % Fourier transform along x Qf0=fft(q0); figure(2); plot(1:n,abs(Qf0),'or',1:n,angle(Qf0),'xb'); fin_ax=axis; title('Initial condition in Fourier space'); xlabel('k'); ylabel('qhat'); legend('abs(qhat)','arg(qhat)'); clc; nmodes disp('Fourier modes included. Strike a key'); pause end % Time loop cfl=1; % Change this to experiment with effect of different time steps qf0=q0; qf1=qf0; q1=q0; qinit=q0; Qf0=fft(q0); nt=1; dt=cfl*dx/u; sigma=u*dt/dx; I=sqrt(-1); while t