% Animation of Time Evolution of Fourier's Solution to the Heat Equation
% David Bloore, August 10, 2011

tend=1.5;
i=1000;
dt=tend/i;
n=150;
x=10000;

X=linspace(0,pi,x);
U=ones(1,x);

subplot('Position',[0.05 0.1 0.85 0.8]);
p=plot(X,U);
title('Truncated Fourier Sine Series and Time Evolution for Heat Equation');
xlabel('0 \leq x \leq \pi');
ylabel('Temperature');
grid on;
xlim([0 pi]);
ylim([0 1.3])
set(gcf,'Position',[0 356 1440 900]);

tpx=0;t=0;
subplot('Position',[0.94 0.1 0.02 0.8]);
title('Time');
tp=plot(tpx,t,'o');
xlim([-0.1 0.1]);
ylim([-0.1 tend+0.1]);
grid on;

for t=0:dt:tend
    U=zeros(1,x);
    for nn=1:2:n
        for xx=1:x
            U(xx)=U(xx)+(4/pi)*(1/nn)*exp(-(nn^2)*t)*sin(nn*X(xx));
        end
    end
    set(p,'YData',U);
    set(tp,'YData',t);
    drawnow;
end