Skip to content

Numerical Methods for Solving ODEs

Ordinary Differential Equations (ODEs):

  • Equations which are composed of an unknown function and its derivatives are called differential equations.
  • Differential equations play a fundamental role in engineering because many physical phenomena are best formulated mathematically in terms of their rate of change.

For example: the pendulum

F=madvdt=gcmv
  • Analytical:
v=gmc(1c(c/m)t)
  • Numberical:
vi+1=vi+(gcmvi)Δt

Use of Taylor series

This lecture is devoted to solving ordinary differential equations of the form

dydx=f(x,y).
  • Suppose that the solution passes through the point (x0,y0)
  • we attempt, by a step-by-step method, to calculate successively approximate values of y at the points x1=x0+h,x2=x0+2h,,xk=x0+kh, where h is a suitably chosen spacing along the X axis
  • Use the Taylor series representation in the form of function y at x point:
y(x+h)=y(x)+y(x)h1!+y(x)h22!+

setting x=xk, we obtain the formula:

yk+1=yk+ykh1!+ykh22!+

where

y=F(x,y),yk=F(xk,yk)y=Fx+Fydydx,yk=F(xk,yk)x+F(xk,yk)yyk

Example1

dydx=yx.
  • with the initial condition that y=2 at x=0.
dydxy=xy=3exp(x)x1.
  • Choose an interval h=0.1,
  • calculate successively the approximate values of y at x=0.1,0.2,0.3, and so on:
y=yx,y=y1,y=y,
  • At x=0, with k=0
y0=2,y0=1,y0=1,,y1=y|x=0.1=y0+2h+h22+h36+2+0.2000+0.0050+0.0002+=2.2052.

y|x=0.1=3exp(0.1)0.112.2155

  • At x=0.1, with k=1
y12.1052,y11.1052,y11.1052,,y2y|x=0.2=y1+2.1052h+0.5526h2+0.1842h3+2.2052+0.2105+0.0055+0.0002+=2.4214.

y|x=0.2=3exp(0.2)0.212.4642

Euler’s Methods

euler's method to solving ODEs

The first derivative provides a direct estimate of the slope at xi:

ϕ=f(xi,yi).

where f(xi,yi) is the differential equation evaluated at xi and yi. This estimate can be substituted into the equation:

yi+1=yi+f(xi,yi)h

A new value of y is predicted using the slope to extrapolate linearly over the step size h.

euler's method with different steps

Error Analysis for Euler’s Method

Numerical solutions of ODEs involves two types of error:

  • Truncation error
    • Local truncation error
    Ea=f(xi,yi)2!h2+=y(xi,yi)2!h2+O(h3)=O(h2)
    • Propagated truncation error
    • The sum of the two is the total or global truncation error
  • Round-off errors

Improvements of Euler’s method

A fundamental source of error in Euler’s method is that the derivative at the beginning of the interval is assumed to apply across the entire interval.

Two simple modifications are available to circumvent this shortcoming:

  • Heun’s Method
  • The Midpoint (or Improved Polygon) Method

Heun’s Method

One method to improve the estimate of the slope involves the determination of two derivatives for the interval:

  • At the initial point
  • At the end point

The two derivatives are then averaged to obtain an improved estimate of the slope for the entire interval.

  • Predictor:
yi+10=yi+f(xi,yi)h
  • Corrector:
yi+1=yi+f(xi,yi)+f(xi+1,yi+10)2hheun's method to help euler'sresult about heun's method

The Midpoint / Improved Polygon Method

Uses Euler’s method to predict a value of y at the midpoint of the interval:

yi+1=yi+f(xi+1/2,yi+1/2)h

where

xi+1/2=xi+12hyi+1/2=yi+f(xi,yi)h2midpoint method to help euler's

Runge-Kutta (RK) Methods

Runge-Kutta methods achieve the accuracy of a Taylor series approach without requiring the calculation of higher derivatives.

yi+1=yi+ϕ(xi,yi,h)hϕ=a1k1+a2k2++ankn Increment function a’s=constants; k’s: slopes in the intervalk1=f(xi,yi)k2=f(xi+p1h,yi+q11k1h) p’s and q’s are constants k3=f(xi+p2h,yi+q21k1h+q22k2h)kn=f(xi+pn1h,yi+qn1,1k1h+qn1,2k2h++qn1,n1kn1h)
  • k’s are recurrence functions. Because each k is a functional evaluation, this recurrence makes RK methods efficient for computer calculations.
  • Various types of RK methods can be devised by employing different number of terms in the increment function as specified by n.
  • First order RK method with n=1 is in fact the Euler’s method.
  • Once n is chosen, values of a’s, p’s, and q’s are evaluated by setting general equation equal to terms in a Taylor series expansion.

2nd-Order RK

For n=2:

yi+1=yi+(a1k1+a2k2)hk1=f(xi,yi)k2=f(xi+p1h,yi+q11k1h)

Values of a1,a2,p1, and q11 are evaluated by setting the second order equation to Taylor series expansion to the second order term. Three equations to evaluate four unknowns constants are derived.

While for a general case, the Taylor expansion gives

yi+1yi+f(xi,yi)h+f(xi,yi)h22

Since

f(xi,yi)=f(xi,yi)x+f(xi,yi)ydydx

Therefore

yi+1yi+f(xi,yi)h+(fx+fydydx)h22

On the other hand

f(xi+p1h,yi+q11k1h)=f(xi,yi)+p1hfx+q11k1hfy+O(h2).

Then

yi+1=yi+(a1k1+a2k2)hyi+1yi+a1hf(xi,yi)+a2hf(xi,yi)+a2p1h2fx+a2q11k1h2fy+O(h3)=yi+(a1+a2)hf(xi,yi)+(a2p1fx+a2q11k1fy)h2+O(h3)

Thus

{a1+a2=1a2p1=12a2q11=12

For a2:

  • (Midpoint) a2=1a1=0;p1=12;q11=12:
yi+1=yi+f[xi+h2,yi+h2f(xi,yi)]h
  • (Heun) a2=12a1=12;p1=1;q11=1:
yi+1=yi+{f(xi,yi)+f[xi+h,yi+hf(xi,yi)]}h2
  • (Raltson) a2=23a1=13;p1=34;q11=34:
yi+1=yi+{f(xi,yi)+2f[xi+3h4,yi+3hf(xi,yi)4]}h3
  • Because we can choose an infinite number of values for a2, there are an infinite number of second-order RK methods.
  • Every version would yield exactly the same results if the solution to ODE were quadratic, linear, or a constant.
  • However, they yield different results if the solution is more complicated (typically the case).
  • Three of the most commonly used methods are:
    • Huen Method with a Single Corrector (a2=1/2)
    • The Midpoint Method (a2=1)
    • Raltson’s Method (a2=2/3)
RK2's method to solving ODEs

3rd-Order RK

For n=3:

yi+1=yi+(k1+4k2+k3)6hk1=f(xi,yi)k2=f(xi+h2,yi+12k1h)k3=f(xi+h,yik1h+2k2h)

Classical 4th-Order RK

For n=4:

yi+1=yi+(k1+2k2+2k3+k4)6hk1=f(xi,yi)k2=f(xi+h2,yi+12k1h)k3=f(xi+h2,yi+12k2h)k4=f(xi+h,yi+k3h)

Butcher’s 5th-Order RK Method

yi+1=yi+(7k1+32k3+12k4+32k5+7k6)90hk1=f(xi,yi)k2=f(xi+h4,yi+14k1h)k3=f(xi+h4,yi+18k1h+18k2h)k4=f(xi+h2,yi12k2h+k3h)k5=f(xi+3h4,yi+316k1h+916k4h)k6=f(xi+h,yi37k1h+27k2h+127k3h127k4h+87k5h)efforts of different methods

Example2

Solve the following initial-value problem over the interval from x=0 to 2:

dydx=yx21.1y

where y(0)=1. Plot the solution. (a) Analytically. (b) Use Euler’s method with h=0.5 and 0.25. (c) Use Heun’s method with h=0.5. Iterate the corrector to εs=1%. (d) Use the midpoint method with h=0.5 and 0.25. (e) Use the classic fourth-order RK method with h=0.5 (relative error).

(a) The analytical solution can be derived by separation of variables

dyy=x21.1dxlny=x331.1x+C

Substituting the initial conditions yields C=0. Taking the exponential gives the final result

y=ex331.1x

The result can be plotted as

curve of examples

(b) Euler's method with h=0.5 gives

xydy/dx
01-1.1
0.50.45-0.3825
10.25875-0.02588
1.50.2458130.282684
20.3871551.122749

Euler's method with h=0.25 gives

xydy/dx
01-1.1
0.250.725-0.75219
0.50.536953-0.45641
0.750.422851-0.22728
10.36603-0.0366
1.250.3568790.165057
1.50.3981430.457865
1.750.512611.005997
20.7641092.215916

(c) For Heun's method, the value of the slope at x=0 can be computed as 1.1 which can be used to compute the value of y at the end of the interval as

y(0.5)=1+(01.1(1))0.5=0.45

The slope at the end of the interval can be computed as

y(0.5)=0.45(0.5)21.1(0.45)=0.3825

which can be averaged with the initial slope to predict

y(0.5)=1+1.10.382520.5=0.629375

This formula can then be iterated to yield

jyij|εa|
00.45
10.62937528.5%
20.59125786.45%
30.59935771.35%
40.59763650.288%

The remaining steps can be implemented with the result

xiyi
01.0000000
0.50.5976365
10.4590875
1.50.6269127
22.8784358

The results along with the analytical solution are displayed below:

curve of examples in Heun's method

(d) The midpoint method with h=0.5

xydy/dxymdy/dxmid
01-1.10.725-0.75219
0.50.623906-0.530320.491326-0.26409
10.491862-0.049190.4795660.221799
1.50.6027620.6931760.7760561.52301
21.3642673.9563742.353369.32519

with h=0.25 gives

xydy/dxymdy/dxmid
01-1.10.8625-0.93527
0.250.766182-0.794910.666817-0.63973
0.50.60625-0.515310.541836-0.38436
0.750.510158-0.274210.475882-0.15912
10.470378-0.047040.4644980.076932
1.250.4896110.2264450.5179160.409478
1.50.591980.6807770.6770771.043122
1.750.8527611.6735431.0619542.565282
21.4940814.3328362.0356866.953139
curve of examples in Midpoint's method

(d) The 4th -order RK method with h=0.5 gives

xyk1ymk2ymk3yek4ϕ
01-1.10.725-0.752190.811953-0.84240.578799-0.49198-0.79686
0.50.60157-0.511330.473737-0.254630.537912-0.289130.457006-0.0457-0.27409
10.464524-0.046450.4529110.2094710.5168920.2390620.5840550.6716630.253713
1.50.591380.6800870.7614021.4942520.9649431.8937011.5382314.4608691.986144
21.5844524.5949112.7331810.830234.29200817.0070810.0879951.9531718.70378
curve of examples in RK4's method

Released under the MIT License.