Error calculating diffusion equation solution by fft

149 Views Asked by At

I''m trying to compute the solutions of: $$\partial_tu=\partial_{xx}u$$ with $x\in[0,1]$ and initial conditions$$u(0,x)=sin(\pi x) $$ Solution are periodic. We have to apply forward euler in time and fft in space and compute the solution in $t=1$.

I've made this code in octave/matlab to do so, but can't obtain the correct result. Anyone has any hint or spots the error?

thank you.

%number of nodes, space step, nodes, time step
N=50;
h=1/N;
x=h*(1:N); 
t=0; 
dt=h/1000;

%initial condition
u_0=sin(pi.*x); 
%auxiliar variable for the while part
u_aux=u_0; 

while (t<=1)
%apply transform
u_n=fft(u_aux);
%perform second derivative
u_n_2_derivative=(1i*1i*[0:N/2-1 0 -N/2+1:-1].*[0:N/2-1 0 -N/2+1:-1]).*u_n;
%inverse transform
u_n_xx=real(ifft(u_n_2_derivative));
%do the time step
u_nn=u_aux+dt.*u_n_xx;
%renew variables
u_aux=u_nn;
t=t+dt;
endwhile

plot(x,sol)