function [t,err]=expliciterr(dx); % EXPLICITERR For reproducing figure 2.4 in Morton & Mayers. % At command line: % >> [t1,err1]=expliciterr(0.1); [t2,err2]=expliciterr(0.05); % >> semilogy(t1,err1,t2,err2) % >> xlabel('t_n'), ylabel('E^n'), set(gca,'XTick',0:0.25:1) % ELB 2/17/05 nu=0.5; dt=nu*dx^2; t=0:dt:1; x=0:dx:1; J=length(x)-1; % J is number of space steps U=x.*(1-x); % initial condition % to see i.c. and exact soln: plot(x,U,x,uexact(x,0.1)) err(1)=0; for n=1:length(t)-1 % take explicit step and compare to exact U(2:J)=U(2:J) + nu*( U(3:J+1) - 2*U(2:J) + U(1:J-1) ); err(n+1)=max(abs(uexact(x,n*dt)-U)); end function y=uexact(x,t); % Fourier series sum w/o loop!; x is row vector; t must be scalar N=181; mm=(1:2:2*N-1)'; pimm=pi*mm; c=exp(-t*pimm.^2)./(mm.^3); cc=repmat(c,1,length(x)); y=(8/pi^3)*sum(cc.*sin(pimm*x));