% m28_adaptiveRK.m - examine adaptive time stepping in ODE45 f = @(t,u) u.*tanh(exp(t).*sin(5*t)); LW = 'linewidth'; MS = 'markersize'; FS = 'fontsize'; while 1 tol = input('tol? (e.g. 1e-2, 1e-4,..., 1e-14) '); % pick a tolerance opts = odeset('reltol',tol,'abstol',tol); sol = ode45(f,[0 3],1,opts); % ODE45 returns a structure tt = linspace(0,3,1000); % which we then evaluate uu = deval(sol,tt); % on a 1000-point grid hold off, plot(tt,uu,LW,.6) % for plotting grid on, hold on plot(sol.x,sol.y,'.k',MS,20+0.4*log(tol)) % ODE45's adaptive grid pts npts = length(sol.x); % the number of these pts err = deval(sol,3) - 1.403737560405910; % error at right-hand BC format = 'tol = %3.1e err = %5.1e npts = %i'; s = sprintf(format,tol,err,npts); title(s,'fontsize',16) end