%% ODE With Crude Error Threshold
% Compared to ode45, the ode23 solver is better at solving problems with crude error tolerances.
% Compare the performance of ode45 and ode23 by solving the moderately-stiff ODE
% y' = -\lambda y
% for \lambda = 1000. This ODE is a test equation that becomes increasingly stiff as \lambda increases. Use odeset to turn on the display of solver statistics.

opts = odeset('Stats','on');
tspan = [0 2];
y0 = 1;
lambda = 1e3;
subplot(1,2,1)
tic, ode45(@(t,y) -lambda*y, tspan, y0, opts), toc
title('ode45')
subplot(1,2,2)
tic, ode23(@(t,y) -lambda*y, tspan, y0, opts), toc
title('ode23')

% For this moderately stiff problem, ode23 executes slightly faster than ode45 and also has fewer failed steps. The step sizes taken by ode45 and ode23 for this problem are limited by the stability requirements of the equation rather than by accuracy. Since steps taken by ode23 are cheaper than with ode45, the ode23 solver executes quicker even though it takes more steps.
% Copyright 2012 The MathWorks, Inc.