www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CompareStiffODESolversExample.m

    %% Compare Stiff ODE Solvers
% The |ode15s| solver is a good first choice for most stiff problems.
% However, the other stiff solvers might be more efficient for certain
% types of problems. This example solves a stiff test equation using all
% four stiff ODE solvers.

%%
% Consider the test equation
%
% $$y' = -\lambda y.$$
%
% The equation becomes increasingly stiff as the magnitude of $\lambda$
% increases. Use $\lambda = 1 \times 10^9$ and the initial condition
% $y(0)=1$ over the time interval |[0 0.5]|. With these values, the problem
% is stiff enough that |ode45| and |ode23| struggle to integrate the
% equation. Also, use |odeset| to pass in the constant Jacobian $J =
% \frac{\partial f}{\partial y} = - \lambda$ and turn on the display of
% solver statistics.
lambda = 1e9;
opts = odeset('Stats','on','Jacobian',-lambda);
tspan = [0 0.5];
y0 = 1;
subplot(2,2,1)
disp('ode15s stats:')
tic, ode15s(@(t,y) -lambda*y, tspan, y0, opts), toc
title('ode15s')

subplot(2,2,2)
disp(' ')
disp('ode23s stats:')
tic, ode23s(@(t,y) -lambda*y, tspan, y0, opts), toc
title('ode23s')

subplot(2,2,3)
disp(' ')
disp('ode23t stats:')
tic, ode23t(@(t,y) -lambda*y, tspan, y0, opts), toc
title('ode23t')

subplot(2,2,4)
disp(' ')
disp('ode23tb stats:')
tic, ode23tb(@(t,y) -lambda*y, tspan, y0, opts), toc
title('ode23tb')

%%
% The stiff solvers all perform well, but |ode23s| completes the
% integration with the fewest steps and runs the fastest for this
% particular problem. Since the constant Jacobian is specified, none of the
% solvers need to calculate partial derivatives to compute the solution.
% Specifying the Jacobian benefits |ode23s| the most since it normally
% evaluates the Jacobian in every step.
%
% For general stiff problems, the performance of the stiff solvers varies
% depending on the format of the problem and specified options. Providing
% the Jacobian matrix or sparsity pattern always improves solver efficiency
% for stiff problems. But since the stiff solvers use the Jacobian
% differently, the improvement can vary significantly. Practically
% speaking, if a system of equations is very large or needs to be solved
% many times, then it is worthwhile to investigate the performance of the
% different solvers to minimize execution time.