%Fourier Series Example 10.1 from Lecture 10
clear;clf;
figure(1)
x=-2:.01:2;
xr=0:.01:1;
t=0;
for n=0:4
  K    =0:1:n;
  lamK = pi*(2*K+1);
  u(:,n+1)=8*(sin(lamK'*x)'*((-1).^K.*exp(-lamK.^2*t)./lamK.^2)');
  plot(x',u(:,n+1),'r-',xr',f(xr'),'k-','linewidth',2)
  tit=[num2str(n+1),' terms of the Fourier Series '];
  title(tit)
  xlabel('x');ylabel('f(x)')
  pause(.2)
end
figure(2)
K=0:1:50;
lamK = pi*(2*K+1);
x=0:.01:1;
it=0;
for t=0:0.05:0.3
  it=it+1;
  U(:,it)=8*(sin(lamK'*x)'*((-1).^K.*exp(-lamK.^2*t)./lamK.^2)');
  plot(x',U,'r-',xr',f(xr'),'k-','linewidth',2)
  axis([0 1 0 1])
  tit=[' u(x,t) using 50 terms of the Fourier Series 0< t <0.3 '];
  title(tit)
  xlabel('x');ylabel('u(x,t)')
  pause(.2)
end
pause(.1)
figure(3)
[X,T]=meshgrid(x,0:0.05:0.3);
surf(X,T,U')
xlabel('x');ylabel('t');zlabel('u(x,t)')


