% Solves u_t + u_{xxx} = 0 using exact time integration and discrete
% Fourier Transform on the interval [0,1]
N = 1024;  % the number of collocation points
nt = 1000;  % the number of time steps
tmax = 0.1;  % the time till that the solution is computed
t = linspace(0,tmax,nt);
x = linspace(0,1,N);
u0 = sin(2*pi*x); % the initial condition
f0 = fft(u0);  % Fourier coefficients at time 0
k = [0:N/2-1,-N/2:-1]; % frequencies

pi3 = pi^3;
fig = figure;
grid;
hold on;
he = plot(x,u0,'Linewidth',1,'color','r');
h = plot(x,u0,'Linewidth',2,'color','b');
axis([0,1,-1,1]);
drawnow;

for j = 1 : nt
    ft = f0.*exp(1i*8*pi3*k.^3*t(j)); % Fourier coefficients at time t(j)
    ut = ifft(ft);  % solution at time t(j)
     % plot the computed solution at time t(j)     
    set(h,'xdata',x,'ydata',real(ut)); 
     % plot the exact solution at time t(j)     
    set(he,'xdata',x,'ydata',sin(2*pi*x+8*pi3*t(j)));
    axis([0,1,-1,1]);
    drawnow;
end