Professional Documents
Culture Documents
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)
Lambert, J. 1973, Computational Methods in Ordinary Differential Equations (New York: Wiley).
Lapidus, L., and Seinfeld, J. 1971, Numerical Solution of Ordinary Differential Equations (New
York: Academic Press).
k1 = hf(xn , yn )
k2 = hf xn + 12 h, yn + 12 k1 (16.1.2)
yn+1 = yn + k2 + O(h3 )
As indicated in the error term, this symmetrization cancels out the first-order error
term, making the method second order. [A method is conventionally called nth
order if its error term is O(hn+1 ).] In fact, (16.1.2) is called the second-order
Runge-Kutta or midpoint method.
We needn’t stop there. There are many ways to evaluate the right-hand side
f(x, y) that all agree to first order, but that have different coefficients of higher-order
error terms. Adding up the right combination of these, we can eliminate the error
terms order by order. That is the basic idea of the Runge-Kutta method. Abramowitz
and Stegun [1], and Gear [2], give various specific formulas that derive from this basic
16.1 Runge-Kutta Method 705
y(x) 2
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)
x1 x2 x3 x
Figure 16.1.1. Euler’s method. In this simplest (and least accurate) method for integrating an ODE,
the derivative at the starting point of each interval is extrapolated to find the next function value. The
method has first-order accuracy.
y(x)
4
2
1 3 5
x1 x2 x3 x
Figure 16.1.2. Midpoint method. Second-order accuracy is obtained by using the initial derivative at
each step to find a point halfway across the interval, then using the midpoint derivative across the full
width of the interval. In the figure, filled dots represent final function values, while open dots represent
function values that are discarded once their derivatives have been calculated and used.
idea. By far the most often used is the classical fourth-order Runge-Kutta formula,
which has a certain sleekness of organization about it:
k1 = hf(xn , yn )
h k1
k2 = hf(xn + , yn + )
2 2
h k2
k3 = hf(xn + , yn + )
2 2
k4 = hf(xn + h, yn + k3 )
k1 k2 k3 k4
yn+1 = yn + + + + + O(h5 ) (16.1.3)
6 3 3 6
The fourth-order Runge-Kutta method requires four evaluations of the right-
hand side per step h (see Figure 16.1.3). This will be superior to the midpoint method
(16.1.2) if at least twice as large a step is possible with (16.1.3) for the same accuracy.
Is that so? The answer is: often, perhaps even usually, but surely not always! This
takes us back to a central theme, namely that high order does not always mean
high accuracy. The statement “fourth-order Runge-Kutta is generally superior to
second-order” is a true one, but you should recognize it as a statement about the
706 Chapter 16. Integration of Ordinary Differential Equations
1
yn
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)
3 yn + 1
4
Figure 16.1.3. Fourth-order Runge-Kutta method. In each step the derivative is evaluated four times:
once at the initial point, twice at trial midpoints, and once at a trial endpoint. From these derivatives the
final function value (shown as a filled dot) is calculated. (See text for details.)
SUBROUTINE rk4(y,dydx,n,x,h,yout,derivs)
INTEGER n,NMAX
REAL h,x,dydx(n),y(n),yout(n)
EXTERNAL derivs
PARAMETER (NMAX=50) Set to the maximum number of functions.
Given values for the variables y(1:n) and their derivatives dydx(1:n) known at x, use
the fourth-order Runge-Kutta method to advance the solution over an interval h and return
the incremented variables as yout(1:n), which need not be a distinct array from y. The
user supplies the subroutine derivs(x,y,dydx), which returns derivatives dydx at x.
INTEGER i
REAL h6,hh,xh,dym(NMAX),dyt(NMAX),yt(NMAX)
hh=h*0.5
h6=h/6.
xh=x+hh
16.1 Runge-Kutta Method 707
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)
do 13 i=1,n
yt(i)=y(i)+h*dym(i)
dym(i)=dyt(i)+dym(i)
enddo 13
call derivs(x+h,yt,dyt) Fourth step.
do 14 i=1,n Accumulate increments with proper weights.
yout(i)=y(i)+h6*(dydx(i)+dyt(i)+2.*dym(i))
enddo 14
return
END
SUBROUTINE rkdumb(vstart,nvar,x1,x2,nstep,derivs)
INTEGER nstep,nvar,NMAX,NSTPMX
PARAMETER (NMAX=50,NSTPMX=200) Maximum number of functions and
REAL x1,x2,vstart(nvar),xx(NSTPMX),y(NMAX,NSTPMX) maximum number of values to
EXTERNAL derivs be stored.
COMMON /path/ xx,y Storage of results.
C USES rk4
Starting from initial values vstart(1:nvar) known at x1 use fourth-order Runge-Kutta to
advance nstep equal increments to x2. The user-supplied subroutine derivs(x,v,dvdx)
evaluates derivatives. Results are stored in the common block path. Be sure to dimension
the common block appropriately.
INTEGER i,k
REAL h,x,dv(NMAX),v(NMAX)
do 11 i=1,nvar Load starting values.
v(i)=vstart(i)
y(i,1)=v(i)
enddo 11
xx(1)=x1
x=x1
h=(x2-x1)/nstep
do 13 k=1,nstep Take nstep steps.
call derivs(x,v,dv)
708 Chapter 16. Integration of Ordinary Differential Equations
call rk4(v,dv,nvar,x,h,v,derivs)
if(x+h.eq.x)pause ’stepsize not significant in rkdumb’
x=x+h
xx(k+1)=x Store intermediate steps.
do 12 i=1,nvar
y(i,k+1)=v(i)
enddo 12
enddo 13
visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).
readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs
Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine-
Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X)
return
END