24
MATH2114 Lecture Notes Numerical Methods and Statistics for Engineers Numerical Solution of Ordinary Differential Equations Dr Eddie Ly School of Mathematical & Geospatial Sciences (SMGS) College of Sciences, Engineering & Health (SEH) RMIT University, Melbourne, Australia July 2014 Contents 1 Introduction 2 2 Inherent and Induced Instability 3 3 Taylor Series Methods 3 3.1 Euler’s Method (First-Order Taylor Method) .................... 4 3.2 Error in Euler’s Method ................................ 6 3.3 Second-Order Taylor Method ............................. 6 4 Runge-Kutta Methods 9 4.1 Second-Order Explicit Methods ............................ 9 4.1.1 Modified Euler’s Method ........................... 10 4.1.2 Scheme with c 2 =2/3 ............................. 10 4.1.3 Ralston’s Method ................................ 10 4.1.4 Improved Euler’s Method ........................... 11 4.2 Fourth-Order Explicit Runge-Kutta Methods .................... 12 5 Higher-Order Differential Equations 16 6 Linear Shooting Methods 18 7 Self Assessment Exercises 23 1

MATH2114 Numerical LN2

Embed Size (px)

DESCRIPTION

Maths help

Citation preview

  • MATH2114 Lecture Notes

    Numerical Methods and Statistics for Engineers

    Numerical Solution ofOrdinary Dierential Equations

    Dr Eddie Ly

    School of Mathematical & Geospatial Sciences (SMGS)

    College of Sciences, Engineering & Health (SEH)

    RMIT University, Melbourne, Australia

    July 2014

    Contents

    1 Introduction 2

    2 Inherent and Induced Instability 3

    3 Taylor Series Methods 3

    3.1 Euler's Method (First-Order Taylor Method) . . . . . . . . . . . . . . . . . . . . 4

    3.2 Error in Euler's Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

    3.3 Second-Order Taylor Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

    4 Runge-Kutta Methods 9

    4.1 Second-Order Explicit Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    4.1.1 Modied Euler's Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    4.1.2 Scheme with c2 = 2=3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    4.1.3 Ralston's Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    4.1.4 Improved Euler's Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    4.2 Fourth-Order Explicit Runge-Kutta Methods . . . . . . . . . . . . . . . . . . . . 12

    5 Higher-Order Dierential Equations 16

    6 Linear Shooting Methods 18

    7 Self Assessment Exercises 23

    1

  • 2 Numerical Solution of Ordinary Dierential Equations

    1 Introduction

    In practice, many dierential equations do not possess a closed-form solution; that is, a

    specic expression for the dependent variable cannot be found in terms of known functions.

    However, a wide variety of numerical methods have been devised to obtain approximate

    solutions to such equations. This document aims to introduce you to some of the numerical

    methods used to solve the initial value problem,

    y0 = f(x; y) subject to y(x0) = y0;

    and discusses some of the diculties associated with them. As usual, y0 denotes dy=dx. At

    rst sight, this equation may seem too elementary to be of general use, but it is possible to

    extend its use to higher-order dierential equations by a simple extension of mathematical

    notation. For example, consider the following second-order ordinary dierential equation,

    d2y

    dx2+ 2x

    dy

    dx+ x2y = cos x subject to y(0) = 1 and y0(0) = 2:

    If we put u(x) = y(x) and v(x) = y0(x), the above higher-order dierential equation can

    be rewritten as a system of rst-order dierential equations,

    du

    dx=

    dy

    dx= v; u(0) = 1;

    dv

    dx=

    d2y

    dx2= cos x 2xv x2u; v(0) = 2:

    When we use a numerical technique to solve the initial value problem, we obtain

    approximations to the solution y(x) at a discrete set of points. If the domain of interest is

    divided into N equal subintervals of length h, then for a solution in the interval a x b,where a = x0, we have

    h =b aN

    =b x0N

    :

    We obtain approximations to the solution at the points x1 = x0 + h, x2 = x0 + 2h,

    x3 = x0 + 3h, etc.,

    xn = x0 + nh for n = 1; 2; : : : ; N:

    It is important to realise the distinction between the true (exact) solution at x = xn,

    given by y(xn), and the approximation to the solution at x = xn denotes by yn. In the

    rst case, we have a function dened for all values of x (x0 x b), and in the secondcase we have a nite set of values yn for n = 0; 1; 2; : : : ; N .

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 3

    2 Inherent and Induced Instability

    When using a numerical method with a small local truncation error, the accumulated error

    can magnify to such an extent that the computed results are completely unacceptable

    as solutions to the dierential equation. In such cases, we say that instability occurs.

    Instability can occur when small variations in the conditions of the problem cause large

    variations in the solution. This instability is inherent in the formulation of the problem,

    and hence, is referred to as inherent instability. For instance, consider solving the following

    initial value problem,

    dy

    dx= 3(y x) subject to y(0) = 1

    3and x 0:

    This equation is solved exactly to give

    y(x) = x+1

    3:

    Suppose now that the initial condition is altered slightly to y(0) = 1=3+, where jj 1.The solution then becomes

    y(x) = e3x + x+1

    3:

    A small variation in the initial condition causes a change of e3x in the solution, which

    for large x values, is large. For example, if = 108 and x = 10, the error is in the order

    of 105. This problem is said to exhibit inherent instability.

    Instability can arise from the numerical method being used to solve the dierential

    equation. In some cases, the methods will exhibit instability for step sizes which are

    greater than some critical value. For some dierential equations, some methods exhibit

    instability for all step sizes. Instability due to the numerical method is known as induced

    instability.

    3 Taylor Series Methods

    Numerical methods for approximating solutions of

    y0 = f(x; y) subject to y(x0) = y0;

    can be obtained from the Taylor series expansion of y = y(x) about x = xn :

    y(xn + h) = y(xn) + hy0(xn) +

    h2

    2!y00(xn) +

    h3

    3!y000(xn) + + h

    m

    m!y(m)(xn) +Rm(xn);

    where Rm is the remainder when the Taylor series is truncated after the (m+ 1)th term,

    Rm(xn) =hm+1

    (m+ 1)!y(m+1)(n); n 2 (xn; xn+1):

  • 4 Numerical Solution of Ordinary Dierential Equations

    3.1 Euler's Method (First-Order Taylor Method)

    If we truncate the Taylor series expansion of y = y(x) about x = xn after the second

    term, we obtain a rst-order Taylor polynomial,

    y(xn + h) = y(xn) + hy0(xn) +

    h2

    2y00(n)| {z }

    = R1(xn)

    ; n 2 (xn; xn+1):

    We can then generate approximations in the following manner, with y0 = f(x; y),

    yn+1 = yn + hy0n

    = yn + hf(xn; yn) for n = 0; 1; 2; : : : ; N 1:

    This algorithm is known as the Euler's method or rst-order Taylor method. It is an

    explicit formula as it expresses yn+1 explicitly in terms of yn, and a single-step formula

    as it relates two successive terms of the sequence, yn and yn+1.

    EXAMPLE

    Using Euler's method with h = 0:1, lets nd approximate solutions on the interval

    [1; 2] of the following initial value problem,

    dy

    dx= 1 +

    y

    xsubject to y(1) = 2:

    We will compare the approximate solutions with the exact analytic solution,

    y(x) = x(lnx+ 2):

    Euler's formula:

    yn+1 = yn + hy0n with y

    0n = 1 +

    ynxn

    :

    With (x0; y0) = (1; 2), and putting n = 0, gives

    y00 = y0(x0; y0)

    = 1 + 2=1

    = 3;

    x1 = x0 + h

    = 1 + 0:1

    = 1:1;

    y1 = y0 + hy00

    = 2 + (0:1)(3)

    = 2:3 :

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 5

    The exact solution is y(1:1) 2:304841, so the error in the approximate solution isError 2:304841 2:3 0:004841:Putting n = 1 gives

    y01 = y0(x1; y1)

    3:0909;x2 = x1 + h

    = 1:2;

    y2 = y1 + hy01

    2:6091:

    The error in y2 is about 0.009695, which is almost exactly twice the error in y1.

    Proceeding in this manner, we can obtain y3; y4; : : : ; y10, as shown in the table

    below. Note that the error increases as n increases. The accuracy could have been

    improved by taking a smaller value of h, but this involves more computation and a

    possible accumulation of rounding-o errors.

    approx soln exact soln

    n xn yn y0n xn+1 yn+1 y(xn+1) jErrorj

    0 1.0 2.0000 3.0000 1.1 2.3000 2.3048 0.0048

    1 1.1 2.3000 3.0909 1.2 2.6091 2.6188 0.0097

    2 1.2 2.6091 3.1743 1.3 2.9265 2.9411 0.0146

    3 1.3 2.9265 3.2512 1.4 3.2516 3.2711 0.0195

    4 1.4 3.2516 3.3226 1.5 3.5839 3.6082 0.0243

    5 1.5 3.5839 3.3893 1.6 3.9228 3.9520 0.0292

    6 1.6 3.9229 3.4518 1.7 4.2680 4.3021 0.0341

    7 1.7 4.2680 3.5106 1.8 4.6191 4.6580 0.0389

    8 1.8 4.6191 3.5662 1.9 4.9757 5.0195 0.0438

    9 1.9 4.9757 3.6188 2.0 5.3376 5.3863 0.0487

  • 6 Numerical Solution of Ordinary Dierential Equations

    3.2 Error in Euler's Method

    To analyse the error in Euler's method, consider the dierence between the relations for

    the exact solution,

    y(xn+1) = y(xn + h)

    = y(xn) + hy0(xn) +

    h2

    2y00(n)

    = y(xn) + hfxn; y(xn)

    +

    h2

    2y00(n); n 2 (xn; xn+1);

    and the approximate solution,

    yn+1 = yn + hy0n

    = yn + hf(xn; yn):

    Thus, the error in yn+1 is

    y(xn+1) yn+1 = y(xn) yn + hhfxn; y(xn)

    f(xn; yn)i| {z }propagated error

    +h2

    2y00(n)| {z }

    local truncation error

    :

    Using the Mean Value Theorem, and letting en = y(xn) yn represents the error in thenth step, the above relation becomes

    en+1 1 + h

    @f

    @x

    en +

    h2

    2y00(n) ; n 2 (xn; xn+1);

    illustrating the relationship between the errors and the step sizes. After performing nu-

    merous simplications, it can be shown that the error after n steps is given by

    jenj = Mh2

    (xn x0); where M = maxx0xxn

    y00(x) :The error is O(h) (order of h) and Euler's method is said to converge with order one (dueto the power of h in the error bound).

    3.3 Second-Order Taylor Method

    We can obtain greater accuracy than in the Euler's method by truncating the Taylor

    series after a greater number of terms. For example, the second-order Taylor method is

    yn+1 = yn + hy0n +

    h2

    2y00n

    = yn + hfn +h2

    2

    hfx + ffy

    in

    for n = 0; 1; 2; : : : ; N 1;

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 7

    since y0 = f(x; y) and applying the chain rule of dierentiation,

    d2y

    dx2=

    d

    dxf(x; y)

    =@f

    @x+

    @f

    @y

    @y

    @x

    =@f

    @x+ f

    @f

    @y:

    Note that fx and fy denote @f=@x and @f=@y, respectively. The second-order Taylor

    method is a single-step explicit formula like the Euler's method. In each step, the trun-

    cation error ish3

    6y000(n) for n 2 (xn; xn+1):

    The truncation error is Oh3, and the global truncation error is Oh2. Therefore, thesecond-order Taylor method converges with order two. The truncation error is sometimes

    referred to as local truncation error, and the cumulative error is called the global trun-

    cation error. This is certainly more accurate than the Euler's method, but it does have

    some drawbacks. For instance, the second derivative may, in some situations, be dicult

    to determine, and more computation is required in each step.

    EXAMPLE

    Using the second-order Taylor method with h = 0:1, lets nd approximate solutions

    of the following initial value problem,

    dy

    dx= 1 +

    y

    xsubject to y(1) = 2;

    on the interval [1; 2].

    Here y0n = 1 +ynxn

    with x0 = 1 and y0 = 2.

    We obtain y00n by dierentiating y0 with respect to x :

    d2y

    dx2=

    d

    dx

    h1 +

    y

    x

    i=

    1

    x

    dy

    dx+ y

    d

    dx

    1

    x

    =

    y0

    x+ y

    1x2

    =

    xy0 yx2

    :

    Thus, y00n =xny

    0n ynx2n

    provided xn 6= 0.

  • 8 Numerical Solution of Ordinary Dierential Equations

    Putting n = 0 (with x0 = 1 and y0 = 2) generates

    y00 = y0(x0; y0)

    = y0(1; 2)

    = 1 + 2=1

    = 3;

    y000 = y00(x0; y0; y00)

    = y00(1; 2; 3)

    =(1)(3) 2

    12

    = 1;

    y1 = y0 + hy00 +

    12h2y000

    = 2 + (0:1)(3) + (0:5)(0:1)2(1)

    = 2:305:

    The exact solution is y(1:1) 2:304841, and the error is aboutError 2:304841 2:305 0:000159;which is certainly much less than the error obtained with the Euler's method where

    the error is about 0.004841 (from the last example). The table below presents the

    approximate solutions and errors for y1 to y10. As with the Euler's method, the

    error is seen to increase as n increases, but is much less than that obtained with the

    Euler's method.

    approx soln exact soln

    n xn yn y0n y

    00n xn+1 yn+1 y(xn+1) jErrorj

    0 1.0 2.0000 3.0000 1.0000 1.1 2.3050 2.3048 0.0002

    1 1.1 2.3050 3.0955 0.90909 1.2 2.6191 2.6188 0.0003

    2 1.2 2.6191 3.1826 0.83333 1.3 2.9415 2.9411 0.0004

    3 1.3 2.9415 3.2627 0.76923 1.4 3.2716 3.2711 0.0005

    4 1.4 3.2716 3.3369 0.71429 1.5 3.6089 3.6082 0.0007

    5 1.5 3.6089 3.4059 0.66667 1.6 3.9528 3.9520 0.0008

    6 1.6 3.9528 3.4705 0.62500 1.7 4.3030 4.3021 0.0009

    7 1.7 4.3030 3.5312 0.58824 1.8 4.6591 4.6580 0.0011

    8 1.8 4.6591 3.5884 0.55556 1.9 5.0207 5.0195 0.0012

    9 1.9 5.0207 3.6425 0.52632 2.0 5.3876 5.3863 0.0013

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 9

    4 Runge-Kutta Methods

    To avoid the need to calculate high-order derivatives as in the Taylor series methods,

    the Runge-Kutta methods attain the same accuracy as the Taylor approximations by

    evaluating the slope f(x; y) at more points. The Runge-Kutta methods have the general

    form,

    yn+1 = yn + h(xn; yn; h) for n 0;where (xn; yn; h) is called an increment function, which can be though of as some kind

    of average slope of the solution on the interval [xn; xn+1]. For an R-stage Runge-Kutta

    formula to have order p , the local truncation error must have order (p+1). It is possible

    to obtain explicit R-stage Runge-Kutta formula of order R for R = 1; 2; 3 and 4. A 6-

    stage method is required for a fth-order explicit formula. If we do not restrict ourselves

    to explicit formula, then it is possible to obtain R-stage implicit Runge-Kutta formula of

    order 2R. The only explicit rst-order Runge-Kutta method is Euler's method.

    4.1 Second-Order Explicit Methods

    For an explicit Runge-Kutta method of order two (R = 2), we have

    yn+1 = yn +2X

    r=1

    brkr = yn + b1k1 + b2k2 for n = 0; 1; 2; : : : ;

    where

    k1 = hf(xn + c1h; yn + a11k1 + a12k2);

    k2 = hf(xn + c2h; yn + a21k1 + a22k2);

    c1 = a11 + a12;

    c2 = a21 + a22:

    Since this is an explicit scheme, aji = 0 for i j. That is, a11 = a12 = a22 = 0 anda21 6= 0. The above expressions reduce to

    c1 = 0;

    c2 = a21;

    k1 = hf(xn; yn);

    k2 = hf(xn + c2h; yn + c2k1):

    Clearly, this is a one-step scheme, requiring only the given initial value y0. The constants

    c2, b1 and b2, are to be determined such that the truncation error is Oh3and global

    error is Oh2, so that the method is of order two.

  • 10 Numerical Solution of Ordinary Dierential Equations

    Because we can choose an innite number of values for c2 to obtain an accuracy of

    order two, there are an innite number of second-order explicit Runge-Kutta methods,

    depending on the value of c2. Usually, c2 = 1=2; 2=3; 3=4; 1 values are preferred.

    4.1.1 Modied Euler's Method

    If we choose c2 = 1=2, we obtain the modied Euler's method :

    k1 = hf(xn; yn);

    k2 = hf(xn +12h; yn +

    12k1) for n = 0; 1; 2; : : : ;

    yn+1 = yn + k2 :

    The number yn+12k1 = yn+

    12hf(xn; yn) is the Euler solution at xn+1=2 = xn+

    12h. Using

    it, we obtain an approximation to the slope at xn+1=2, namely f(xn+1=2; yn +12k1). This

    is then used to nd a rst-order straight line approximation to y at xn+1. Notice that the

    k's are recurrence relationships, i.e. k1 appears in the equation for k2. Because each k is a

    functional evaluation, this recurrence makes Runge-Kutta methods ecient for computer

    calculations.

    4.1.2 Scheme with c2 = 2=3

    Choosing c2 = 2=3, we obtain the following second-order explicit Runge-Kutta scheme,

    k1 = hf(xn; yn);

    k2 = hf(xn +23h; yn +

    23k1) for n = 0; 1; 2; : : : ;

    yn+1 = yn +14(k1 + 3k2):

    4.1.3 Ralston's Method

    Choosing c2 = 3=4 provides a minimum bound on the truncation error for the second-order

    explicit Runge-Kutta methods. Thus, with c2 = 3=4, we have

    k1 = hf(xn; yn);

    k2 = hf(xn +34h; yn +

    34k1) for n = 0; 1; 2; : : : ;

    yn+1 = yn +13(k1 + 2k2):

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 11

    4.1.4 Improved Euler's Method

    With c2 = 1, we obtain the improved Euler's method :

    k1 = hf(xn; yn);

    k2 = hf(xn + h; yn + k1) for n = 0; 1; 2; : : : ;

    yn+1 = yn +12(k1 + k2):

    The number yn + k1 = yn + hf(xn; yn) is the Euler solution at x = xn+1. Using it, we

    obtain an approximation to the derivative at xn+1, namely fxn+1; yn+ hf(xn; yn)

    . The

    two slopes are then averaged (12(k1 + k2)) and added to yn to produce yn+1.

    EXAMPLE

    We will use the improved Euler's method with h = 0:1 to nd the approximate

    solutions to

    y0 = x y subject to y(0) = 1;at the points x = 0:1 and x = 0:2.

    Exact solution: y(x) = x 1 + 2ex.Improved Euler's method (with f(x; y) = x y and h = 0:1):

    k1 = hf(xn; yn);

    k2 = hf(xn + h; yn + k1) for n = 0; 1; 2; : : : ;

    yn+1 = yn +12(k1 + k2):

    First iteration (n = 0) :

    (x0; y0) = (0; 1);

    x1 = x0 + h

    = 0:1;

    f(x0; y0) = f(0; 1)

    = 1;k1 = hf(x0; y0)

    = (0:1)f(0; 1)

    = 0:1;k2 = hf(x1; y0 + k1)

    = (0:1)f(0:1; 0:9)

    = 0:08;

  • 12 Numerical Solution of Ordinary Dierential Equations

    y1 = y0 +12(k1 + k2)

    = 1 + 12(0:1 +0:08)

    = 0:91;Error = jy(x1) y1j 0:00032516:

    Second iteration (n = 1) :

    (x1; y1) = (0:1; 0:91);

    x2 = x1 + h

    = 0:2;

    f(x1; y1) = f(0:1; 0:91)

    = 0:81;k1 = hf(x1; y1)

    = (0:1)f(0:1; 0:91)

    = 0:081;k2 = hf(x2; y1 + k1)

    = (0:1)f(0:2; 0:829)

    = 0:0629;y2 = y1 +

    12(k1 + k2)

    = 0:91 + 12(0:081 0:0629)

    = 0:83805;Error = jy(x2) y2j 0:00058849:

    The approximate solutions are (x1; y1) = (0:1; 0:910) and (x2; y2) = (0:2; 0:838).

    4.2 Fourth-Order Explicit Runge-Kutta Methods

    The general explicit Runge-Kutta method of order four has the form,

    k1 = hf(xn; yn);

    k2 = hf(xn + c2h; yn + c2k1);

    k3 = hfxn + c3h; yn + a32k2 + (c3 a32)k1

    ;

    k4 = hfxn + c4h; yn + a43k3 + a42k2 + (c4 a43 a42)k1

    ;

    yn+1 = yn + b1k1 + b2k2 + b3k3 + b4k4 for n = 0; 1; 2; : : : :

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 13

    The ten unknows (b1; b2; b3; b4; c2; c3; c4; a32; a42; a43) are determined such that the trunca-

    tion error is Oh5, and the global error is Oh4. After expanding using Taylor seriesexpansion, we would obtain eight equations in ten unknowns. Two of the most commonly

    used fourth-order Runge-Kutta methods are Kutta-Simpson 1/3 rule and Kutta-Simpson

    3/8 rule.

    kutta-simpson 1/3 rule

    The scheme for the Kutta-Simpson 1/3 rule, using Runge's coecients, is given by

    k1 = hf(xn; yn);

    k2 = hf(xn +12h; yn +

    12k1);

    k3 = hf(xn +12h; yn +

    12k2);

    k4 = hf(xn + h; yn + k3);

    yn+1 = yn +16(k1 + 2k2 + 2k3 + k4) for n = 0; 1; 2; : : : :

    If f(x; y) does not depend on y, then this formula reduces to the usual Simpson's inte-

    gration formula for interval [xn; xn+1] :

    yn+1 = yn +1

    6

    hk1 + 2k2 + 2k3 + k4

    i= yn +

    h

    6

    hf(xn; yn) + 2f(xn +

    12h; yn +

    12k1) + 2f(xn +

    12h; yn +

    12k2) + f(xn + h; yn + k3)

    i= yn +

    h

    6

    hf(xn) + 2f(xn +

    12h) + 2f(xn +

    12h) + f(xn + h)

    i= yn +

    h

    6

    hf(xn) + 4f(xn +

    12h) + f(xn + h)

    i:

    kutta-simpson 3/8 rule

    The scheme for the Kutta-Simpson 3/8 rule, using Runge's coecients, is given by

    k1 = hf(xn; yn);

    k2 = hf(xn +13h; yn +

    13k1);

    k3 = hf(xn +23h; yn + k2 13k1);

    k4 = hf(xn + h; yn + k3 k2 + k1);yn+1 = yn +

    18(k1 + 3k2 + 3k3 + k4) for n = 0; 1; 2; : : : :

  • 14 Numerical Solution of Ordinary Dierential Equations

    EXAMPLE

    We will use the Kutta-Simpson 1/3 rule with h = 0:1 to nd approximate solutions

    to y0 = x y, with y(0) = 1, at the points x = 0:1 and x = 0:2.Exact solution: y(x) = x 1 + 2ex

    Kutta-Simpson 1/3 rule (with f(x; y) = x y and h = 0:1) :

    k1 = hf(xn; yn);

    k2 = hf(xn +12h; yn +

    12k1);

    k3 = hf(xn +12h; yn +

    12k2);

    k4 = hf(xn + h; yn + k3);

    yn+1 = yn +16(k1 + 2k2 + 2k3 + k4) for n = 0; 1; 2; : : : :

    First iteration (n = 0) :

    (x0; y0) = (0; 1);

    x1=2 = x0 + h=2

    = 0:05;

    x1 = x0 + h

    = 0:1;

    k1 = hf(x0; y0)

    = (0:1)f(0; 1)

    = 0:1;k2 = hf(x1=2; y0 +

    12k1)

    = (0:1)f(0:05; 0:95)

    = 0:09;k3 = hf(x1=2; y0 +

    12k2)

    = (0:1)f(0:05; 0:955)

    = 0:0905;k4 = hf(x1; y0 + k3)

    = (0:1)f(0:1; 0:9095)

    = 0:08095;y1 = y0 + (k1 + 2k2 + 2k3 + k4)=6

    = 1 + (0:1 + 2(0:09) + 2(0:0905) 0:08095)=6= 0:909675;

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 15

    Error = jy(x1) y1j 1:639281107 :

    Second iteration (n = 1) :

    (x1; y1) = (0:1; 0:909675);

    x3=2 = x1 + h=2

    = 0:15;

    x2 = x1 + h

    = 0:2;

    k1 = hf(x1; y1)

    = (0:1)f(0:1; 0:909675)

    = 0:0809675;k2 = hf(x3=2; y1 +

    12k1)

    = (0:1)f(0:15; 0:869191)

    = 0:07191913;k3 = hf(x3=2; y1 +

    12k2)

    = (0:1)f(0:15; 0:87371544)

    = 0:07237154;k4 = hf(x2; y1 + k3)

    = (0:1)f(0:2; 0:83730346)

    = 0:06373035;y2 = y1 + (k1 + 2k2 + 2k3 + k4)=6

    = 0:909675 + (0:0809675 + 2(0:07191913) + 2(0:07237154) 0:06373035)=6

    = 0:83746180;Error = jy(x2) y2j 2:966565107 :

    Approximate solutions : (x1; y1) = (0:1; 0:9097), (x2; y2) = (0:2; 0:8375).

  • 16 Numerical Solution of Ordinary Dierential Equations

    5 Higher-Order Dierential Equations

    Consider nding approximate solutions of the system of rst-order equations,

    du

    dx= f(x; u; v); u(x0) = u0 ;

    dv

    dx= g(x; u; v); v(x0) = v0 :

    Any of the methods presented here can be modied to solve the above system of dierential

    equations. For example, if we employ the improved Euler's method to solve the equations,

    we modify the scheme as follows :

    k1 = hf(xn; un; vn);

    `1 = hg(xn; un; vn);

    k2 = hf(xn + h; un + k1; vn + `1);

    `2 = hg(xn + h; un + k1; vn + `1);

    xn+1 = xn + h;

    un+1 = un +12(k1 + k2);

    vn+1 = vn +12(`1 + `2) for n = 0; 1; 2; : : : :

    EXAMPLE

    Using the above method for the following system of rst-order dierential equations,

    du

    dx= v ; u(0) = 1;

    dv

    dx= x+ u2 ; v(0) = 0;

    compute the approximate solutions at x = 0:1 and x = 0:2 using step size h = 0:1.

    Here f(xn; un; vn) = vn and g(xn; un; vn) = xn + u2n.

    Initial data :

    x0 = 0;

    u0 = u(x0) = 1;

    v0 = v(x0) = 0;

    f(x0; u0; v0) = v0 = 0;

    g(x0; u0; v0) = x0 + u20 = 1:

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 17

    First iteration (n = 0) :

    x1 = x0 + h

    = 0:1;

    k1 = hf(x0; u0; v0)

    = (0:1)f(0; 1; 0)

    = 0;

    `1 = hg(x0; u0; v0)

    = (0:1)g(0; 1; 0)

    = 0:1;

    k2 = hf(x1; u0 + k1; v0 + `1)

    = (0:1)f(0:1; 1; 0:1)

    = 0:01;

    `2 = hg(x1; u0 + k1; v0 + `1)

    = (0:1)g(0:1; 1; 0:1)

    = 0:11;

    u1 = u0 + (k1 + k2)=2

    = 1 + (0 + 0:01)=2

    = 1:005;

    v1 = v0 + (`1 + `2)=2

    = 0 + (0:1 + 0:11)=2

    = 0:105:

    Second iteration (n = 1) :

    x2 = x1 + h

    = 0:2;

    k1 = hf(x1; u1; v1)

    = (0:1)f(0:1; 1:005; 0:105)

    = 0:0105;

    `1 = hg(x1; u1; v1)

    = (0:1)g(0:1; 1:005; 0:105)

    = 0:1110025;

  • 18 Numerical Solution of Ordinary Dierential Equations

    k2 = hf(x2; u1 + k1; v1 + `1)

    = (0:1)f(0:2; 1:0155; 0:2160025)

    = 0:02160025;

    `2 = hg(x2; u1 + k1; v1 + `1)

    = (0:1)g(0:2; 1:0155; 0:2160025)

    = 0:12312403;

    u2 = u1 + (k1 + k2)=2

    = 1:005 + (0:0105 + 0:02160025)=2

    = 1:02105013;

    v2 = v1 + (`1 + `2)=2

    = 0:105 + (0:1110025 + 0:12312403)=2

    = 0:22206326:

    Approximate solutions are

    (x1; u1; v1) = (0:1; 1:005; 0:105) and (x2; u2; v2) = (0:2; 1:021; 0:222):

    6 Linear Shooting Methods

    Considering nding the solution y(x) to the following ordinary dierential equation for

    a x b :d2y

    dx2+ 2

    dy

    dx+ y = x ; a x b:

    When we apply the Runge-Kutta methods to obtain the approximate solution to the

    above equation, the methods require all conditions to be specied at some initial point x0

    within the interval, usually at x0 = a. For example, if both the initial conditions y and

    y0 are given at the same initial point, x0 = a, we have

    d2y

    dx2+ 2

    dy

    dx+ y = x subject to y(a) = ya and y

    0(a) = y0a:

    The above dierential equation, together with the conditions specied at the same initial

    point, forms an initial value problem. Note that n number of conditions for y(x) are

    required for an nth-order ordinary dierential equation. Suppose now that we want to

    solve the same ordinary dierential equation, but subject to the conditions specied at

    two dierent points within the interval :

    d2y

    dx2+ 2

    dy

    dx+ y = x subject to y(a) = ya and y(b) = yb :

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 19

    In this case, we have a boundary value problem, because the conditions are given at the

    boundaries of the interval, that is, at x = a and x = b. In the following discussion, we

    assume that the boundary value problem under consideration has a unique solution.

    We can still use the Runge-Kutta methods to solve this boundary value problem even

    though we only have one piece of information at x = a, that is, y(a) = ya . Since we have

    y(a) but not y0(a), we need to guess a value for y0(a), and then solve the problem by the

    Runge-Kutta method for up to x = b. We then compare the value of y(x) we get at x = b,

    that is y(b), with what we need at x = b (the specied boundary condition). If y(b) does

    not match the specied boundary condition there, then we try and guess another value

    for y0(a), and solve the problem again. This method of solution is known as the linear

    shooting methods for boundary value problems.

    EXAMPLE

    Consider solving the following boundary value problem for 0 x 1,d2y

    dx2+ 2

    dy

    dx+ y = x subject to y(0) = 1 and y(1) = 2:

    In the linear shooting methods, we need to convert the above boundary value prob-

    lem into an initial value problem (which is appropriate for the numerical methods

    such as the Runge-Kutte methods) by guessing y0(0) = , so that y(1) = 2. That

    is, we consider

    d2y

    dx2+ 2

    dy

    dx+ y = x subject to y(0) = 1 and y0(0) = :

    Rewritting the above second-order ordinary dierential equation as a couple rst-

    order dierential equations by letting u(x) = y(x) and v(x) = y0(x) :

    du

    dx= f(x; u; v) = v ; u(0) = 1 ;

    dv

    dx= g(x; u; v) = x 2v u ; v(0) = :

    Suppose we employ the improved Euler's method (explicit second-order Runge-

    Kutta scheme) to solve the above system of dierential equations, using step size

    h = 0:1 :

    k1 = hf(xn; un; vn);

    `1 = hg(xn; un; vn);

    k2 = hf(xn+1; un + k1; vn + `1);

    `2 = hg(xn+1; un + k1; vn + `1);

  • 20 Numerical Solution of Ordinary Dierential Equations

    xn+1 = xn + h ;

    un+1 = un +12(k1 + k2);

    vn+1 = vn +12(`1 + `2) for n = 0; 1; 2; : : : ; n:

    First guess y0(0) = = 1 :

    n xn un vn k1 `1 k2 `2

    0 0 1 1 0.100000 0:300000 0.070000 0:2400001 0.1 1.085000 0.730000 0.073000 0:244500 0.048550 0:1929002 0.2 1.145775 0.511300 0.051130 0:196838 0.031446 0:1525833 0.3 1.187063 0.336590 0.033659 0:156024 0.018057 0:1181854 0.4 1.212921 0.199485 0.019948 0:121189 0.007830 0:0889465 0.5 1.226810 0.094417 0.009442 0:019564 0.000285 0:0641966 0.6 1.231673 0.016537 0.001654 0:066475 0:004994 0:0433457 0.7 1.230003 0:038373 0:003837 0:045326 0:008370 0:0258778 0.8 1.223900 0:073974 0:007397 0:027595 0:010157 0:0113369 0.9 1.215123 0:093440 0:009344 0:012824 0:010626 0.00067510 1 1.205137 0:099515

    Second guess y0(0) = = 3 :

    n xn un vn k1 `1 k2 `2

    0 0 1 3 0.300000 0:700000 0.230000 0:5800001 0.1 1.265000 2.360000 0.236000 0:588500 0.177150 0:4844002 0.2 1.471575 1.823550 0.182355 0:491868 0.133168 0:4017303 0.3 1.629337 1.376752 0.137675 0:408284 0.096847 0:3303954 0.4 1.746598 1.007412 0.100741 0:336142 0.067127 0:2689885 0.5 1.830532 0.704847 0.070485 0:274023 0.043082 0:2162676 0.6 1.887315 0.459703 0.045970 0:220672 0.023903 0:1711357 0.7 1.922252 0.263799 0.026380 0:174985 0.008881 0:1326268 0.8 1.939883 0.109994 0.010999 0:135987 0:002599 0:0998909 0.9 1.944083 0:007945 0:000794 0:102819 0:011076 0:07217610 1 1.938147 0:095442

    Thus, for = 1, y(1) u10 = 1:205137 and for = 3, y(1) u10 = 1:938147. But,the specic condition is y(1) = 2. We can use these two results to give us a better

    estimate of to obtain y(1) = 2 by line of best ts between these results :

    = m u10 + c;

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 21

    wherem and c are constants. When u10 = 1:205137, = 1 and when u10 = 1:938147,

    = 3 :

    1 = m(1:205137) + c and 3 = m(1:938147) + c :

    Solving them provides

    m = 2:728476 and c = 2:288187 :

    That is,

    = 2:728476u10 2:288187 :Using the above relation, for y(1) = u10 = 2, we require = 3:168765.

    Third guess y0(0) = = 3:168765 :

    n xn un vn k1 `1 k2 `2

    0 0 1 3.168765 0.316877 0:733753 0.243501 0:6086901 0.1 1.280189 2.497543 0.249754 0:617528 0.188002 0:5089972 0.2 1.499067 1.934281 0.193428 0:516763 0.141752 0:4227533 0.3 1.666657 1.464523 0.146452 0:429570 0.103495 0:3483014 0.4 1.791631 1.075587 0.107559 0:354280 0.072131 0:2841805 0.5 1.881475 0.756357 0.075636 0:289419 0.046694 0:2290996 0.6 1.942640 0.497098 0.049710 0:233684 0.026341 0:1819187 0.7 1.980666 0.289297 0.028930 0:185926 0.010337 0:1416348 0.8 2.000299 0.125517 0.012552 0:145133 0:001962 0:1073629 0.9 2.005594 0:000730 0:000073 0:110413 0:011114 0:07832310 1 2.000000 0:095099

    EXAMPLE

    A particular boundary value problem has the following boundary conditions:

    y(0) = 1 and y(2) = 1:

    If two applications of the shooting method, with y0(0) = 1 and y0(0) = 3, produce

    values y(2) = 2 and y(2) = 7=2, respectively, nd an improved estimate for the

    value of y0(0).

    solution :

    Let y0(0) = . Line of best ts :

    = m y(2) + c :

  • 22 Numerical Solution of Ordinary Dierential Equations

    To nd constants m and c, we use = 1 when y(2) = 2 and = 3 when y(2) = 7=2,

    resulting m = 4=3 and c = 5=3 :

    =4

    3 y(2) 5

    3:

    Thus, for y(2) = 1, we require y0(0) = = 1=3.

  • Lecture Notes { MATH2114 Numerical Methods & Statistics for Engineers 23

    7 Self Assessment Exercises

    [1] Putting u(x) = y(x) and v(x) = y0(x), reformulate the following initial value

    problem as a coupled system of rst-order dierential equations,

    x2d2y

    dx2+ x

    dy

    dx+4 x2y = 0 subject to y(0) = y0(0) = 1:

    [2] Redene the third-order nonhomogeneous ordinary dierential equation,

    d3x

    dt3+

    d2x

    dt2+ 2x = t2 subject to x(0) = 1; _x(0) = 0 and x(0) = 1;

    as a system of rst-order dierential equations.

    [3] For the initial value problem y0 = x + y2 with y(0) = 1, nd an approximation at

    x = 0:2 using two steps of the Euler's method.

    [4] For the initial value problem y0 = x+ y2 subject to y(0) = 1, nd an approximation

    at x = 0:2 using two steps of the second-order Taylor method.

    [5] Consider the initial value problem,

    dy

    dx=

    yy + x

    subject to y(0) = 2:

    (a) Find an approximation at x = 0:2 using two steps of Euler's method.

    (b) Find an approximation at x = 0:2 using four steps of Euler's method.

    (c) Compare your results in part (a) and (b) with the exact answer given by

    y(x) =px2 + 4 x.

    [6] With step size h = 0:1, use the improved Euler's method to nd approximate

    solutions tody

    dx= 1 +

    y

    xsubject to y(1) = 2

    at the points x = 1:1 and x = 1:2.

    [7] Use the modied Euler's method with (a) h = 0:1 and (b) h = 0:05 to nd an

    approximation to the solution of

    dy

    dx= xy subject to y(0) = 1

    at the point x = 0:2.

  • 24 Numerical Solution of Ordinary Dierential Equations

    [8] Use the third-order Runge-Kutta method,

    k1 = hf(xn; yn);

    k2 = hf(xn +12h; yn +

    12k1);

    k3 = hf(xn + h; yn + 2k2 k1) for n = 0; 1; 2; : : : ;yn+1 = yn +

    16(k1 + 4k2 + k3);

    to nd approximate solutions to

    dy

    dx= x+ y2 subject to y(0) = 1;

    at the points x = 0:1, 0.2 and 0.3.

    [9] Use the Kutta-Simpson 1/3 rule with h = 0:1 to nd approximate solutions to

    dy

    dx= 1 +

    y

    xsubject to y(1) = 2;

    at the points x = 1:1 and x = 1:2.

    [10] For the initial value problem,

    dy

    dx=

    y

    y x with y(1) = 1:5;

    nd an approximation to the solution at x = 1:4 using two steps of the Kutta-

    Simpson 3/8 rule.

    [11] (a) Express the second-order initial value problem,

    d2y

    dx2+

    y2 110

    dy

    dx+ y = 0 subject to y(0) = 1 and y0(0) = 0;

    as a coupled pair of rst-order dierential equations.

    (b) Use two steps of the Kutta-Simpson 1/3 rule to nd an approximation to the

    solution at x = 0:2.

    (c) Use two steps of the Kutta-Simpson 3/8 rule to nd an approximation to the

    solution at x = 0:2.