% plot solution of example problem 2 (see Notes)
%   boundary conditions u_x(0,t)=0, u_x(1,t)=0
%   initial condition   u(x,0)=x

c = @(j)  mod(j,2)*(-4)/(j^2*pi^2); % coefficent c(j) for j=1,2,...
lambda = @(j)  j^2*pi^2;            % eigenvalue lambda(j) for j=1,2,...
v = @(j,x)  cos(j*pi*x);            % eigenfunction v(j,x) 

N = 20;                             % use j=1:20
xv = 0:.01:1;                       % x-values for plotting
tv = 0:.01:.2;                      % t-values for plotting

[X,T] = ndgrid(xv,tv);
U = .5*ones(size(X));               % initialize U as .5 (for j=0)
for j=1:N
  U = U + c(j)*v(j,X).*exp(-lambda(j)*T);
end
colorcurves(X,T,U)

