function [] = prob_2_8( iiMax ) % EX_2_8 - % % Written by: % -- % John L. Weatherwax 2005-03-10 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- maxError_Eq_2_29 = []; maxError_Eq_2_30 = []; for ii=2:iiMax, h = 2^(-ii); nSteps = 2^ii; yTruth = exp( -h*(0:nSteps) ); % % Solve using Eq. 2.29: % % Note: This doesn't seem to agree with the text? % but I couldn't find any errors so ... I'm not % sure where the differences lie... ynm2 = exp(-2*h); ynm1 = exp(-h); yn = 1; ys = [ yn ]; for jj=1:nSteps, yp1 = -(1.5+3*h)*yn + 3*ynm1 - 0.5*ynm2; ys = [ ys yp1 ]; ynm2 = ynm1; ynm1 = yn; yn = yp1; end % $$$ figure; % $$$ plot( -h*(0:nSteps), yTruth, '-g', -h*(0:nSteps), ys, '-r' ); maxError_Eq_2_29 = [ maxError_Eq_2_29, max( abs(yTruth-ys) ) ]; % % Solve using Eq. 2.30: % ynm2 = exp(-2*h); ynm1 = exp(-h); yn = 1; ys = [ yn ]; for jj=1:nSteps, yp1 = (1-(23/12)*h)*yn + (16/12)*h*ynm1 - (5/12)*h*ynm2; ys = [ ys yp1 ]; ynm2 = ynm1; ynm1 = yn; yn = yp1; end % $$$ figure; % $$$ plot( -h*(0:nSteps), yTruth, '-g', -h*(0:nSteps), ys, '-r' ); maxError_Eq_2_30 = [ maxError_Eq_2_30, max( abs(yTruth-ys) ) ]; end % Print the results: [ (2:iiMax).', maxError_Eq_2_30.' ] [ (2:iiMax).', maxError_Eq_2_29.' ]