Lab 03
Lab 03
Lab 03
Introduction Exercise 1
Stiff Systems Exercise 2
Direction Field Plots Exercise 3
The Backward Euler Method Exercise 4
Newton’s method Exercise 5
The Trapezoid Method Exercise 6
Matlab ODE solvers Exercise 7
Backwards difference methods (Extra) Exercise 8
Exercise 9
Exercise 10
Extra Credit
1 Introduction
The explicit methods that we discussed last time are well suited to handling a large class of ODE’s. These
methods perform poorly, however, for a class of “stiff” problems that occur all too frequently in applications.
We will examine implicit methods that are suitable for such problems. We will find that the implementation
of an implicit method has a complication we didn’t see with the explicit method: a (possibly nonlinear)
equation needs to be solved.
The term “stiff” as applied to ODE’s does not have a precise definition. Loosely, it means that there
is a very wide range between the most rapid and least rapid (with x) changes in solution components. A
reasonably good rule of thumb is that if Runge-Kutta or Adams-Bashforth or other similar methods require
much smaller steps than you would expect, based on your expectations for the solution accuracy, then the
system is probably stiff. The term itself arose in the context of solution for the motion of a stiff spring when
it is being driven at a modest frequency of oscillation. The natural modes of the spring have relatively high
frequencies, and the ODE solver must be accurate enough to resolve these high frequencies, despite the low
driving frequency.
This lab will take three sessions. If you print this lab, you may prefer to use the pdf version.
2 Stiff Systems
I’ve warned you that there are problems that defeat the explicit Runge-Kutta and Adams-Bashforth methods.
In fact, for such problems, the higher order methods perform even more poorly than the low order methods.
These problems are called “stiff” ODE’s. We will only look at some very simple examples.
Consider the differential system
y ′ = λ(−y + sin x)
y(0) = 0 (1)
whose solution is
λ2 sin x − λ cos x
y(x) = Ce−λx +
1 + λ2
2
λ λ
= Ce−λx + sin x − cos x
1 + λ2 1 + λ2
1
for C a constant. For the initial condition y = 0 at x = 0, the constant C can easily be seen to be
λ
C=
1 + λ2
The ODE becomes stiff when λ gets large: at least λ = 10, but in practice the equivalent of λ might be a
million or more. One key to understanding stiffness of this system is to make the following observations.
• For large λ and except very near x = 0, the solution behaves as if it were approximately y(x) = sin x,
which has a derivative of modest size.
• Small deviations from the curve y(x) = sin x (because of initial conditions or numerical errors) cause
the solution to have large derivatives that depend on λ.
In other words, the interesting solution has modest derivative and should be easy to approximate, but nearby
solutions have large (depending on λ) derivatives and are hard to approximate. Again, this is characteristic
of stiff systems of ODEs.
We will be using this stiff differential equation in the exercises below, and in the next section you will
see a graphical display of stiffness for this equation.
Exercise 1: In this exercise, you will see a graphical illustration of why a differential equation is
“stiff.”
(a) Copy the following lines into a file called stiff4 ode.m:
function fValue = stiff4_ode ( x, y )
% fValue = stiff4_ode ( x, y )
% computes the right side of the ODE
% dy/dx=f_ode(x,y)=lambda*(-y+sin(x)) for lambda = 4
% x is independent variable
% y is dependent variable
% output, fValue is the value of f_ode(x,y).
LAMBDA=4;
fValue = LAMBDA * ( -y + sin(x) );
(b) Also copy the following lines into a second file called stiff4 solution.m.
function y = stiff4_solution ( x )
% y = stiff4_solution ( x )
% computes the solution of the ODE
% dy/dx=f_ode(x,y)=lambda*(-y+sin(x)) for lambda = 4
% and initial condition y=0 at x=0
% x is the independent variable
% y is the solution value
2
LAMBDA=4;
y = (LAMBDA^2/(1+LAMBDA^2))*sin(x) + ...
(LAMBDA /(1+LAMBDA^2))*(exp(-LAMBDA*x)-cos(x));
(c) Create new versions of these files, two using LAMBDA=55 and named stiff55 ode.m and stiff55 solution.m,
and two using LAMBDA=10000 and named stiff10000 ode.m and stiff10000 solution.m.
(d) The solution to our stiff ODE is roughly sin x, so we are interested in values of x between 0
and 2π, and values of y between -1 and 1. The Matlab meshgrid command is designed for that
(it is kind of a two-dimensional linspace). To evaluate the direction vector p = (px , py ) =
(dx)(1, fode (x, y)), px will be all 1’s (use the Matlab ones function), and py comes from our right
hand side function. Finally, we use the Matlab command quiver to display the vector plot. Use
the following commands to plot the direction field for the [0, 2π] × [−1, 1] range of (x,y) values:
h = 0.1; % mesh size
scale = 2.0; % make vectors longer
[x,y] = meshgrid ( 0:h:2*pi, -1:h:1 );
px = ones ( size ( x ) );
py = stiff4_ode ( x, y );
quiver ( x, y, px, py, scale )
axis equal %this command makes equal x and y scaling
(e) Finally, to see how the direction field relates to the approximate solution, plot the function sin x
on the same frame.
hold on
x1=(0:h:2*pi);
y1=stiff4_solution(x1);
plot(x1,y1,’r’) % solution will come out red.
hold off
Send me a copy of your plot (print -djpeg ex1.jpg, or File→Export and choose jpeg).
Look at your direction field full-screen. You can see the solution in red and all the arrows point toward
it. Basically, no matter where you start in the rectangle, you head very rapidly (long arrows) toward
sin x, and then follow that curve as it varies slowly (short arrows). Some numerical methods overshoot
the solution curve in their enthusiasm to reach it, as you will see in the following exercise, and avoiding
this overshoot characterizes methods for stiff systems.
Exercise 2: In this exercise, you will be seeing a numerical illustration of why an ODE is “stiff.”
(a) Using LAMBDA=10000 to represent a stiff equation, how many points would be needed in the interval
[0,2*pi] to make a pleasing plot of stiff10000 solution(x)?
x=linspace(0,2*pi,10); % try 10, 20, 40, etc.
plot(x,stiff10000_solution(x))
Try it, but I think you will agree with me that it takes about 40 evenly-spaced points to make a
reasonable curve. Do not send me copies of your plots.
(b) Now, choose either of forward euler.m, or rk3.m from last lab and attempt to solve stiff10000 ode.m
over the interval [0,2*pi], starting from the initial condition y=0 at x=0 and using 40 steps. Your
solutions blow up (are unstable), don’t they? (If you are plotting the solutions, look at the scale!)
Multiply the number of steps by 10 repeatedly until you get something reasonable, i.e., try 40,
400, 4000, etc. steps. How many steps does it take to get a reasonable plot? It takes many more
steps to get a reasonable solution than you would expect, based on solution accuracy, and this
behavior is characteristic of stiff systems.
3
4 The Backward Euler Method
The Backward Euler method is an important variation of Euler’s method. Before we say anything more
about it, let’s take a hard look at the algorithm:
xk+1 = xk + h
yk+1 = yk + hfode (xk+1 , yk+1 ) (2)
You might think there is no difference between this method and Euler’s method. But look carefully–this
is not a “recipe,” the way some formulas are. Since yk+1 appears both on the left side and the right side,
it is an equation that must be solved for yk+1 , i.e., the equation defining yk+1 is implicit. It turns out that
implicit methods are much better suited to stiff ODE’s than explicit methods.
If we plan to use Backward Euler to solve our stiff ode equation, we need to address the method of
solution of the implicit equation that arises. Before addressing this issue in general, we can treat the special
case:
xk+1 = xk + h
yk+1 = yk + hλ(−yk+1 + sin xk+1 ) (3)
This equation can be solved for yk+1 easily enough! The solution is
xk+1 = xk + h
yk+1 = (yk + hλ sin xk+1 )/(1 + hλ)
In the following exercise, we will write a version of the backward Euler method that implements this solution,
and only this solution. Later, we will look at more general cases.
Exercise 3:
(a) Write a function m-file called back euler lam.m with signature line
function [x,y]=back_euler_lam(lambda,xRange,yInitial,numSteps)
% [x,y]=back_euler_lam(lambda,xRange,yInitial,numSteps)
% comments
4
In the next exercise, we will compare backward Euler with forward Euler for accuracy. Of course,
backward Euler gives results when the stepsize is large and Euler does not, but we are curious about the
case that there are enough steps to get answers. Because it would require too many steps to run this test
with λ =1.e4, you will be using λ =55.
Exercise 4:
(a) Fill in the table below, using the value lambda=55. Compute the error in the solution versus the
exact solution as
abs( y(end) - stiff55_solution(x(end)) )
Compute the ratios of the errors for each value of numSteps divided by the error for the succeeding
value of numSteps. Use back euler lam to fill in the following table, over the interval [0,2*pi]
and starting from yInit=0.
lambda=55
numSteps back_euler_lam error ratio
40 ____________________ __________
80 ____________________ __________
160 ____________________ __________
320 ____________________ __________
640 ____________________ __________
1280 ____________________ __________
2560 ____________________
(b) Based on the ratios in the table, estimate the order of accuracy of the method, i.e., estimate the
exponent p in the error estimate Chp . p is an integer in this case.
(c) Repeat this experiment using forward euler, and fill in the following table. Note that the errors
using euler end up being about the same size as those using back euler lam, once euler starts
to behave.
lambda=55 Error comparison
numSteps back_euler_lam forward_euler
40 ___________________ _________________
80 ___________________ _________________
160 ___________________ _________________
320 ___________________ _________________
640 ___________________ _________________
1280 ___________________ _________________
2560 ___________________ _________________
(In filling out this table, please include at least four significant figures in your numbers. You can
use format long or format short e to get the desired precision.)
(d) Compare the order of accuracy using forward euler with that using back euler lam.
5 Newton’s method
You should be convinced that implicit methods are worth while. How, then, can the resulting implicit equa-
tion (usually it is nonlinear) be solved? The Newton (or Newton-Raphson) method is a good choice. (We
saw Newton’s method last semester in Math 2070, Lab 4, you can also find discussions of Newton’s method by
Quarteroni, Sacco and Saleri in Chapter 7.1 or in a Wikipedia article http://en.wikipedia.org/wiki/Newton’s method.)
Briefly, Newton’s method is a way to solve equations by successive iterations. It requires a reasonably good
starting point and it requires that the equation to be solved be differentiable. Newton’s method can fail,
5
however, and care must be taken so that you do not attempt to use the result of a failed iteration. When
Newton’s method does fail, it is mostly because the provided Jacobian matrix is not, in fact, the correct
Jacobian for the provided function.
Suppose you want to solve the nonlinear (vector) equation
F(Y) = 0 (4)
and you know a good starting place Y0 . The following iteration is called Newton iteration.
Y(n+1) − Y(n) = ∆Y(n) = −(J(n) )−1 (F(Y(n) )) (5)
where J(n) denotes the partial derivative of F evaluated at Y(n) . If F is a scalar function of a scalar variable,
J = ∂F/∂Y. In the case that Y is a vector, the partial derivative must be interpeted as the Jacobian matrix,
with components defined as
∂Fi
Jij = .
∂Yj
The superscript (n) is used here to distinguish the iteration number n from the step number, k. Newton’s
method (usually) converges quite rapidly, so that only a few iterations are required.
There is an easy way to remember the formula for Newton’s method. Write the finite difference formula
for the derivative as
F(Y(n+1) ) − F(Y(n) ) ∂F(Y(n) )
=
Y (n+1) −Y (n) ∂Y
or, for vectors and matrices,
( )
∂F(Y(n) ) ( (n+1) )
F(Y (n+1)
) − F(Y ) =
(n)
Y − Y(n)
∂Y
and then take F(Y(n+1) ) = 0, because that is what you are wishing for, and solve for Y(n+1) .
On each step, the backward Euler method requires a solution of the equation
yk+1 = yk + hfode (xk+1 , yk+1 )
so that we can take yk+1 to be the solution, Y, of the system F(Y) = 0, where
F(Y) = yk + hfode (xk+1 , Y) − Y. (6)
When we have found a satisfactorily approximate solution Y(n) to (6), then we take yk+1 = Y(n) and proceed
with the backward Euler method. We think of each of the values Y(n) as successive corrections to yk+1 .
I call your attention to a difficulty that has arisen. For the Euler, Adams-Bashforth and Runge-Kutta
methods, we only needed a function that computed the right side of the differential equation. In order to
carry out the Newton iteration, however, we will also a function that computes the partial derivative of the
right side with respect to y. In 2070 we assumed that the derivative of the function was returned as a second
variable, and we will use the same convention here.
To summarize, on each time step, start off the iteration by predicting the solution using an explict method.
Forward Euler is appropriate in this case. Then use Newton’s method to generate successive correction steps.
In the following exercise, you will be implementing this method.
Exercise 5:
(a) Change your stiff10000 ode.m so that it has the signature
function [fValue, fPartial]=stiff10000_ode(x,y)
% [fValue, fPartial]=stiff10000_ode(x,y)
% comments
6
and computes the partial derivative of stiff10000 ode with respect to y, fPartial. To do this,
write the derivative of the formula for stiff10000 ode out by hand, then program it. attempt to
use symbolic differentiation inside the function stiff1000 ode. Do not attempt to use symbolic
differentiation inside the function stiff1000 ode.
(b) Similarly, change the signature for stiff55 ode. There is no need to change stiff1 ode because
you won’t be using it again.
(c) Copy the following function to a file named back euler.m using cut-and-paste.
function [x,y]=back_euler(f_ode,xRange,yInitial,numSteps)
% [x,y]=back_euler(f_ode,xRange,yInitial,numSteps) computes
% the solution to an ODE by the backward Euler method
%
% xRange is a two dimensional vector of beginning and
% final values for x
% yInitial is a column vector for the initial value of y
% numSteps is the number of evenly-spaced steps to divide
% up the interval xRange
% x is a row vector of selected values for the
% independent variable
% y is a matrix. The k-th column of y is
% the approximate solution at x(k)
if ~ isConverged
error([’back_euler failed to converge at step ’, ...
num2str(k)])
end
y(:,k+1) = Y;
end
(d) Copy the following function to a file named newton4euler.m (Read this name as “Newton for
Euler.”)
function [Y,isConverged]=newton4euler(f_ode,xkp1,yk,Y,h)
% [Y,isConverged]=newton4euler(f_ode,xkp1,yk,Y,h)
% special function to evaluate Newton’s method for back_euler
TOL = 1.e-6;
7
MAXITS = 500;
8
One simple nonlinear equation with quite complicated behavior is van der Pol’s equation. This equation
is written
z ′′ + a(z 2 − 1)z ′ + z = e−x
where e−x has been chosen as a forcing term that dies out as x gets large. When z > 1, this equation behaves
somewhat as an oscillator with negative feedback (“damped” or “stable”), but when z is small then it looks
like an oscillator with positive feedback (“negatively damped” or “positive feedback” or “unstable”). When z
is small, it grows. When z is large, it dies off. The result is a non-linear oscillator. Physical systems that be-
have somewhat as a van der Pol oscillator include electrical circuits with semiconductor devices such as tunnel
diodes in them (see this web page http://www.math.duke.edu/education/ccp/materials/diffeq/vander/vand1.html)
and some biological systems, such as the beating of a heart, or the firing of neurons. In fact, van der Pol’s orig-
inal paper was, “The Heartbeat considered as a Relaxation oscillation, and an Electrical Model of the Heart,”
Balth. van der Pol and J van der Mark, Phil. Mag. Suppl. 6(1928) pp 763—775. More information is avail-
able in an interesting Scholarpedia article http://www.scholarpedia.org/article/Van der Pol oscillator.
As you know, van der Pol’s equation can be written as a system of first-order ODEs in the following way.
y1′ = f1 (x, y1 , y2 ) = y2
y2′ = f2 (x, y1 , y2 ) = −a(y12 − 1)y2 − y1 + e−x (7)
where z = y1 and z ′ = y2 .
The matrix generalization of the partial derivative in Newton’s method is the Jacobian matrix:
∂f ∂f
1 1
∂y1 ∂y2
J = (8)
∂f2 ∂f2
∂y1 ∂y2
Exercise 6: In this exercise you will be solving van der Pol’s equation with a = 11 using back euler.
(a) Write an m-file to compute the right side of the van der Pol system (7) and its Jacobian (8). The
m-file should be named vanderpol ode.m and should have the signature
function [fValue, J]=vanderpol_ode(x,y)
% [fValue, J]=vanderpol_ode(x,y)
% more comments
% your name and the date
if numel(y) ~=2
error(’vanderpol_ode: y must be a vector of length 2!’)
end
a=11;
fValue = ???
df1dy1 = 0;
df1dy2 = ???
df2dy1 = ???
df2dy2 = -a*(y(1)^2-1);
J=[df1dy1 df1dy2
df2dy1 df2dy2];
be sure that fValue is a column and that the value of the parameter a = 11.
9
(b) Solve the van der Pol system twice, once using forward euler and once using back euler. Solve
over the interval from x=0 to x=2, starting from y=[0;0] and using 40 intervals. You can see by
plotting the solutions that forward euler does not give a good solution (it has some up/down
oscillations) while back euler does. Send me plots of both solutions with your summary. (Note:
If you get a message saying that convergence failed, you probably have an error in your calculation
of the derivatives in J. Fix it.)
(c) Solve the system again, this time using 640 intervals. This time both methods should remain
stable and the answers should be close. Please include plots of both solutions on a single frame
with your summary. (Use hold on and hold off.)
xk+1 = xk + h
h
yk+1 = yk + (fode (xk , yk ) + fode (xk+1 , yk+1 )) (9)
2
from which you can see that this is also an “implicit” formula. The backward Euler and Trapezoid methods
are the first two members of the “Adams-Moulton” family of ODE solvers.
In the exercise below, you will write a version of the trapezoid method using Newton’s method to solve
the per-timestep equation, just as with back euler. As you will see in later exercises, the trapezoid method
is not so appropriate when the equation gets very stiff, and Newton’s method is overkill when the system is
not stiff. The method can be successfully implemented using an approximate Jacobian or by computing the
Jacobian only occasionally, resulting in greater computational efficiency. We are not going to pursue these
alternatives in this lab, however.
Exercise 7:
(a) Make a copy of your back euler.m file, naming the copy trapezoid.m. Modify it to have the
signature:
function [ x, y ] = trapezoid ( f_ode, xRange, yInitial, numSteps )
% comments
(b) Add appropriate comments below the signature line.
(c) Replace the line
[Y,isConverged]= newton4euler(f_ode,x(k+1),y(:,k),Y,h);
with the new line
[Y,isConverged]=newton4trapezoid(f_ode,x(1,k),x(1,k+1),y(:,k),Y,h);
(d) Make a copy of your newton4euler.m, naming the copy newton4trapezoid.m.
(e) Change the signature of newton4trapezoid to
function [Y,isConverged]=newton4trapezoid(f_ode,xk,xkp1,yk,Y,h)
10
(f) In order to implement the trapezoid method, you need to write the function F (Y ) that appears
in (5) and (6) so it is valid for the trapezoid rule. In the code you copied from mewtpm4ei;er,
it is written for the backward Euler method (6) and must be changed for use in the trapezoid
method. To do this, consider (9), repeated here
h
yk+1 = yk + (fode (xk , yk ) + fode (xk+1 , yk+1 ))
2
and replace yk+1 with Y and bring everything to the right. Then write
F (Y ) = 0 = right side.
Modify newton4trapezoid.m to solve this function. Do not forget to modify the Jacobian J
∂Fi
Jij = ∂Y j
to reflect the new function F. Remember, if you have the Jacobian wrong, the Newton
iteration may fail.
(g) Test your newton4trapezoid code for the case f_ode=@stiff55_ode, xk=1.9, xkp1=2.0, yk=1.0,
h=0.1, and the initial guess for Y=1.
(h) Write out by hand the (linear) equation that newton4trapezoid solves in the case f_ode=@stiff55_ode,
xk=1.9, xkp1=2.0, yk=1.0, and h=0.1. Be sure your solution to this equation agrees with the
one from newton4trapezoid. If not, find the mistake and fix it.
(i) Test the accuracy of the trapezoid method by computing the numerical solution of stiff55 ode.m
(starting from y = 0 over the interval x ∈ [0, 2π], and fill in the following table. Compute “error”
as the difference between the computed and exact values at x = 2π.
stiff55
numSteps trapezoid error ratio
10 ____________________ __________
20 ____________________ __________
40 ____________________ __________
80 ____________________ __________
160 ____________________ __________
320 ____________________
(j) Are your results consistent with the theoretical O(h2 ) convergence rate of the trapezoid rule? If
not, you have a bug you need to fix.
In the following exercise, you are going to see that the difference in accuracy between the trapezoid
method and the backward Euler method for solution of the vander Pol equation.
Exercise 8:
(a) Use trapezoid.m to solve the van der Pol equation (a=11) on the interval [0,10] starting from
the column vector [0,0] using 100, 200, 400, and 800 steps, and plot both components of all four
solutions on one plot. You should see that the solution for 400 steps is pretty much the same as
the one for 800 steps, and the others are different, especially in the second component (derivative).
We will assume that the final two approximate solutions represent the correct solution. Please
send me this plot with your summary. Hint: you can generate this plot easily with the following
sequence of commands.
hold off
[x,y]=trapezoid(@vanderpol_ode,[0,10],[0;0],100);plot(x,y)
hold on
[x,y]=trapezoid(@vanderpol_ode,[0,10],[0;0],200);plot(x,y)
[x,y]=trapezoid(@vanderpol_ode,[0,10],[0;0],400);plot(x,y)
[x,y]=trapezoid(@vanderpol_ode,[0,10],[0;0],800);plot(x,y)
hold off
11
(b) Use back euler.m to solve the van der Pol equation on the interval [0,10] starting from the
value [0;0] using 400, 800, 3200, and 12800 steps, and plot all four solutions on one plot. The
larger number of points may take a minute. You should see a progression of increasing accuracy
in the second “pulse.” Please send me this plot with your summary.
(c) Plot the 12800-step back euler solution and the 800-step trapezoid solution on the same plot.
You should see they are close. Please send me this plot with your summary.
The trapezoid method is unconditionally stable, but this fact does not mean that it is good for very stiff
systems. In the following exercise, you will apply the trapezoid method to a very stiff system so you will see
that numerical errors arising from the initial rapid transient persist when using the trapezoid rule but not
for backwards Euler.
Exercise 9:
(a) Solve the stiff10000 ode system twice on the interval [0,10] starting from yInitial=0.1 using
100 steps. Note that the initial condition is not zero. Use both the trapezoid method and
backwards Euler, and plot both solutions on the same plot. Send me this plot with your summary
file.
(b) Use the trapezoid method to solve the same case using 200, 400, 800, and 1600 steps. Plot each
solution on its own plot. You should see that the effect of the initial condition is not easily
eliminated. Please include the 1600 step case when you send your summary file.
Exercise 10: For this exercise, Set a=55 in vanderpol ode.m to exhibit some stiffness.
12
(a) You can watch a solution evolve if you call the solver without any output variables. Use ode45 to
solve the van der Pol problem with a=55 and solve on the interval [0,70] starting from [0;0].
Use the following Matlab command:
ode45(@vanderpol_ode,[0,70],[0;0])
Please include this plot with your summary.
(b) You can see the difference between a stiff solver and a non-stiff solver on a stiff equation such as
stiff10000 ode by comparing ode45 with ode15s. (These are my personal favorites.) You can
see both plots with the commands
figure(2)
ode45(@stiff10000,[0,8],1);
title(’ode45’)
figure(3)
ode15s(@stiff10000,[0,8],1);
title(’ode15s’)
You should be able to see that the step density (represented by the little circles on the curves)
is less for ode15s. This density difference is most apparent in the smooth portions of the curve
where the solution derivative is small. You do not need to send me these plots.
(c) If you wish to examine the solution of vanderpol ode in more detail, or manipulate it, you need
the solution values, not a picture. You can get the solution values with commands similar to the
following:
[x,y]=ode15s(@vanderpol_ode,[0,70],[0;0]);
If you wish, you can plot the solution, compute its error, etc. For this solution, what is the value
of y1 at x=70 (please give at least six significant digits)? How many steps did it take? (The length
of x is one more than the number of steps.)
(d) Suppose you want a very accurate solution. The default tolerance is .001 relative accuracy in
Matlab, but suppose you want a relative accuracy of 1.e-8? There is an extra variable to provide
options to the solver. It works in the following manner:
myoptions=odeset(’RelTol’,1.e-8);
[x,y]=ode15s(@vanderpol_ode,[0,70],[0;0],myoptions);
Use help odeset for more detail about options, and use the command odeset alone to see the
default options. How many steps did it take this time? What is the value of y1 at x=70 (to at
least six significant digits)?
Remark: The functionality of the Matlab ODE solvers is available in external libraries that can be used
in Fortran, C, C++ or Java programs. One of the best is the Sundials package from Lawrence Livermore
https://computation.llnl.gov/casc/sundials/main.html or odepack from Netlib http://www.netlib.org/odepack/.
13
The backward difference method of second order can be written as
4 1 2
yk+1 = yk − yk−1 + hfode (xk+1 , yk+1 ). (10)
3 3 3
It is an easy exercise using Taylor series to show that the truncation error is O(h3 ) per step, or O(h2 ) over
the interval [0, T ].
Exercise 11: In this exercise, you will write a function m-file named bdm2.m to implement the second
order backward difference method (10).
14