Analysis of Runge Kutta Method

download Analysis of Runge Kutta Method

of 40

Transcript of Analysis of Runge Kutta Method

  • 7/24/2019 Analysis of Runge Kutta Method

    1/40

    ENGR90024

    Computational Fluid Dynamics

    Lecture 09

    Analysis of the Runge-Kutta method &

    the Crank-Nicolson method

  • 7/24/2019 Analysis of Runge Kutta Method

    2/40

    Stability analysis of 2nd-order Runge-Kutta (page 39 of printedlecture notes):

    l+1 =l +t

    1

    2k1 +

    1

    2k2

    k1 = f(l, tl)

    k2 = f(l +tk1, t

    l +t)

    to model problem

    d

    dt ==f()

    Apply 2nd-order Runge-Kutta

  • 7/24/2019 Analysis of Runge Kutta Method

    3/40

    l+1 =l +t

    1

    2k1 +

    1

    2k2

    k1 = f(

    l, tl)

    k2 = f(l +tk1, t

    l +t)

    dt ==f()

    k1 = f(l, tl) =l f(l, tl) = l

    l l

    k2 =

    f(l

    +tk1, t

    l

    +t

    )

    tl

    f( +tk1, t +t) =( +tk1)

    l +tk1 l+tk1

    f(l +tk1, tl +t) =(l +t(l))

    = k1

    = k2

    tl+t

  • 7/24/2019 Analysis of Runge Kutta Method

    4/40

    l+1 =l +t

    1

    2k1 +

    1

    2k2

    k2k1

    l

    (l

    +t

    (l))

    =

    =

    l+1 =l +t12

    l +12(l +tl)

    l+1 =l +tl +1

    2t

    22l

    l+1 = (1 +t +t22/2)l

    where = 1 +t +t22/2l+1 = l = l1

  • 7/24/2019 Analysis of Runge Kutta Method

    5/40

    1 = Given

    2 = 1

    3=

    2=

    2

    1

    = 314= 3

    .

    .

    . =

    .

    .

    .

    l+1 = l=

    .

    .

    .

    = l1

    Prove that l+1 = l = l1

    Hence, for stability

    || =|1 +t +t2

    2

    /2| 1

  • 7/24/2019 Analysis of Runge Kutta Method

    6/40

    || =|1 +t +t22/2| 1

    The stability boundary is can be found by solving

    |1 +t +t22/2| = 1

    A complex number

    Need to find!!t where the above complex number has amagnitude of 1. Any complex number that has a magnitude 1 canbe represented by

    Hence we want to find values of !!t that make

    |1 +t +t22/2| = 1

    1 +t +t22/2 =ei

    1 +t +t22/2

    ei = 0

    |ei| = 1

  • 7/24/2019 Analysis of Runge Kutta Method

    7/40

    You can follow the steps above and show that the stability regionfor the 4th order Runge-Kutta method can be found by solving

    t+

    2t2

    2

    +

    3t3

    6

    +

    4t4

    24

    + 1 ei

    = 0

    where

    0 8 = 0

    1 +t +t22/2 ei = 0

    Note that0 2n

    where n is the order of the polynomial.

  • 7/24/2019 Analysis of Runge Kutta Method

    8/40

    Example O09.1:Write MATLAB program to find stability

    boundary for the 2nd-order Runge-Kutta method solving the

    model problem

    dt

    ==f()

  • 7/24/2019 Analysis of Runge Kutta Method

    9/40

    1 +t +

    t

    22

    /2

    e

    i= 0

    To be consistent with notation introduced previously, we let

    1

    t = p

    Hence

    ei = 0+p +p2/2

    This is a 2nd order polynomial in terms of p. We can use Newton-Raphson polynomial to solve this equation. Note that

    0 2n

    where n is the order of the polynomial.

    1 +p +p2/2 ei = 0

    From the theory presented above, we need to solve

  • 7/24/2019 Analysis of Runge Kutta Method

    10/40

    functionMPO09p1a()

    clear all;

    close all;

    theta=0:0.1:4*pi;

    %initial guess value of p

    p=0;

    forn=1:length(theta)

    gp=1+p+p^2/2.0-exp(i*theta(n));

    dgdp=1+p;

    whileabs(gp)>1.0e-6

    p=p-gp/dgdp

    gp=1+p+p^2/2.0-exp(i*theta(n));

    dgdp=1+p;

    end

    lamdt(n)=p;

    end

    plot(real(lamdt),imag(lamdt),'b-','linewidth',5);

    xlabel('\lambda_{Re}\Delta t');

    ylabel('\lambda_{Im}\Delta t');

    axis([-4 2 -3 3]);

    daspect([1 1 1]);

    !4 !3 !2 !1 0 1 2!3

    !2

    !1

    0

    1

    2

    3

    !Re"t

    !Im

    "

    t

    (p) = 1 +p+p2/2 ei = 0

    dg

    dp= 1 +p

    = 0

    =

    = 2

    = 3

    = 4

    pnew= pold g(pold)dg

    dp(pold)

    Newton Raphson

  • 7/24/2019 Analysis of Runge Kutta Method

    11/40

    Alternatively, we can get MATLAB to plot contours of

    || =|1 +t +t22/2|

    We can do that using the contour()command in MATLAB

    (4 4)( 4 4)

  • 7/24/2019 Analysis of Runge Kutta Method

    12/40

    functionMPO09p1b()

    clear all;

    close all;

    relamdt=-4:0.1:4;

    imlamdt=-4:0.1:4;

    [x,y]=meshgrid(relamdt,imlamdt);

    axis square;

    lamdt=x+i*y;

    sig=1+lamdt+lamdt.^2/2;contour_levels=[1 2 3 4];

    contour(x,y,abs(sig),contour_levels,'linewidth',4);

    xlabel('Re \lambda \Delta t','Fontsize',14);ylabel('Im \lambda \Delta t','Fontsize',14);

    hold on;

    plot([-4 4],[0 0],'k');

    plot([0 0],[-4 4],'k');

    title('contour |\sigma|','Fontsize',14);

    Re !"t

    Im!

    "

    t

    contour |#|

    !4 !3 !2 !1 0 1 2 3 4!4

    !3

    !2

    !1

    0

    1

    2

    3

    4

    t= Re(t) + iIm(t)

    = 1 +t +t22/2

    || = 4

    || = 3

    || = 2

    || = 1

    (-4,-4) (4,-4)

    (4,4)(-4,4)

    ||

  • 7/24/2019 Analysis of Runge Kutta Method

    13/40

    h+

    2h2

    2+

    3h3

    6+

    4h4

    24+ 1 e

    I= 0

    1 + h+2h2

    2 e

    I= 0

    -2.79

    2.83

    -2.83

    -2.0

    4th order Runge Kutta

    2nd order Runge Kutta

    Euler

  • 7/24/2019 Analysis of Runge Kutta Method

    14/40

    h+

    2h2

    2+

    3h3

    6+

    4h4

    24+ 1 e

    I= 0

    1 + h+

    2h2

    2e

    I

    = 0

    -2.79

    Stability region for RK4 is bigger than RK2

    In order for your code to be stable,you need to pick such that it falls withinthe stability region of your numerical scheme.

    t

    For example, if you want to solve

    d

    dt = 10

    using Euler, the maximum value of !t youcan use is

    tmax,Euler =2

    10=

    1

    5= 0.2

    If you were to use RK4, then the maximumvalue of!t you can use is

    tmax,RK4 =2.79

    10= 0.279

  • 7/24/2019 Analysis of Runge Kutta Method

    15/40

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    !

    1!exp(!t)

    Explicit Euler

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    !

    1!exp(!t)

    2nd-orderRunge-Kutta

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    !

    1!exp(!t)

    4th-orderRunge-Kutta

    Back to this slide again: stability analysis predicts this behaviour

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    !

    1!exp(!t)

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    !

    1!exp(!t)

    0 1 2 3 4 5 6 7 8!30

    !20

    !10

    0

    10

    20

    30

    t

    !

    1!exp(!t)

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    !

    1!exp(!t)

    0 1 2 3 4 5 6 7 8!30

    !20

    !10

    0

    10

    20

    30

    t

    !

    1!exp(!t)

    0 1 2 3 4 5 6 7 8!30

    !20

    !10

    0

    10

    20

    30

    t

    !

    1!exp(!t)

  • 7/24/2019 Analysis of Runge Kutta Method

    16/40

    End of Example O09.1

  • 7/24/2019 Analysis of Runge Kutta Method

    17/40

    Runge-Kutta methods for aSystem of ODEs

  • 7/24/2019 Analysis of Runge Kutta Method

    18/40

    In general, a system of M ODEs can be written as

    d1

    dt =f1(1,2,3, . . . . . . , M, t)

    d2dt

    =f2(1,2,3, . . . . . . , M, t)

    d3

    dt =f3(1,2,3, . . . . . . , M, t)

    ... =...

    dM

    dt =fM(1,2,3, . . . . . . , M, t)

    Runge-Kutta method can easily be extended to solve a system of Mequations

  • 7/24/2019 Analysis of Runge Kutta Method

    19/40

    d1

    dt =f1(1, t)

    RK-2

    k1 = f(l

    1, tl

    )k2 = f(

    l

    1+tk1, t

    l +t)

    l+11 =l

    1 +t

    1

    2k1 +

    1

    2k2

  • 7/24/2019 Analysis of Runge Kutta Method

    20/40

    d1

    dt =f1(1, t) d1

    dt =f1(1,2, t)

    d2

    dt =f2

    (1,2, t

    )

    RK-2

    RK-2

    k1 = f(l

    1, tl)

    k2 = f(l

    1+tk1, t

    l +t)

    l+11 =l

    1 + t12k1 +

    12k2

    l+11 = l

    1 +t

    1

    2k11 +

    1

    2k21

    l+12 = l

    2 +t

    1

    2k12 +

    1

    2k22

    k11 = f1(l

    1,l

    2, tl

    )k12 = f2(

    l

    1,l

    2, tl)

    k21 = f1l1+tk11,

    l

    2+tk12, t

    l+t

    k22 = f2

    l

    1 +tk11,l

    2 +tk12, tl

    +t

    k1associated with

    "1

    k2associated with

    "1

    k2associated with

    "2

    k1associated with

    "2

  • 7/24/2019 Analysis of Runge Kutta Method

    21/40

    d1

    dt =f1(1, t)

    RK-2

    k1 = f(l

    1, tl)

    k2 = f(l

    1+tk1, t

    l +t)

    l+11 =l

    1 + t12k1 +

    12k2

    d1

    dt =f1(1,2, t)

    d2

    dt =f2(1,2, t)

    RK-2

    l+11 = l

    1 + t

    1

    2k11 +

    1

    2k21

    l+12 = l

    2 + t

    1

    2k12 +

    1

    2k22

    k11 = f1(l

    1,l

    2, tl)

    k12 = f2(l

    1,l

    2, tl)

    k21 = f1l1+tk11,

    l

    2+tk12, t

    l+t

    k22 = f2l1+tk11,

    l

    2+tk12, t

    l+t

    d1

    dt =f1(1,2,3, t)

    d2

    dt =f2(1,2,3, t)

    d3

    dt =f3(1,2,3, t)

    l+11 = l

    1 + t

    1

    2k11 +

    1

    2k21

    l+12 = l2 + t12k12 + 1

    2k22

    l+13 =

    l

    3 + t

    1

    2k13 +

    1

    2k23

    k11 = f1(l

    1,l

    2,l

    3, tl)

    k12 = f2(l

    1,l

    2,l

    3, tl

    )k13 = f3(

    l

    1,l

    2,l

    3, tl)

    k21 = f1l1 +tk11,

    l

    2 +tk12,l

    3 +tk13, tl+t

    k22 = f2l1 +tk11,

    l

    2 +tk12,l

    3 +tk13, tl+t

    k23 = f3

    l

    1 +tk11,l

    2 +tk12,l

    3 +tk13, tl

    +t

    RK-2

    k1associated with"1

    k2associated with

    "1

    k2associated with

    "3

    k1associated with

    "2

    k1associated with

    "3

    k2associated with

    "2

  • 7/24/2019 Analysis of Runge Kutta Method

    22/40

    Example O09.2:Write a MATLAB program to solve the

    nonlinear ODE

    with "(0)=0, (d"/dt)(0)=0, (d2"/dt2)(0)=0.332 using the 4th-order Runge-Kutta method. Plot d"/dt for 0

  • 7/24/2019 Analysis of Runge Kutta Method

    23/40

    d3

    dt3 +

    1

    2

    d2

    dt2 = 0

    Let

    1 = , 2 = d/dt, 3 = d2/dt2

    (0) = 0.

    0 d

    dt(0) = 0.0 d

    2

    dt2(0) = 0.332 0 t 25

    (9.1)

    sod1

    dt =

    d

    dt = 2

    d2

    dt =

    d2

    dt2 = 3

    and

    1(0) = 0.

    0 2(0) = 0.

    0 3(0) = 0.

    332

    Equation (9.1) can be rewritten as

    d3

    dt

    = 1

    2

    d

    dt2 =

    1

    23

    131

  • 7/24/2019 Analysis of Runge Kutta Method

    24/40

    functionMPO9p2()

    clear all;

    close all;

    Delta_t=0.1;t=0:Delta_t:25;

    phi=zeros(length(t),3);phi(1,:)=[0.0 0.0 0.332];

    forn=1:length(t)-1

    k1=f(phi(n,:));

    k2=f(phi(n,:)+Delta_t*k1);

    phi(n+1,:)=phi(n,:)+Delta_t*(k1/2+k2/2);

    end

    plot(t,phi(:,2),'ko-')hold on

    xlabel('t');

    ylabel('d\phi/dt');

    functiondphidt=f(phi)

    dphidt=[phi(2) phi(3) -0.5*phi(3)*phi(1)];

    d3dt

    =

    1

    231

    d1

    dt

    d2

    dt

    = 2

    = 3

    1(0) = 0.0 2(0) = 0.0 3(0) = 0.332

    t 5

    d1

    dt =f1(1,2,3, t)

    d2

    dt =f2(1,2,3, t)d3

    dt =f3(1,2,3, t)

    l+11 = l

    1 + t

    1

    2k11 +

    1

    2k21

    l+12 = l

    2 + t

    1

    2k12 +

    1

    2k22

    l+13 = l

    3 + t

    1

    2k13 +

    1

    2k23

    k11 = f1(l

    1,l

    2,l

    3, tl)

    k12 = f2(l

    1,l

    2,l

    3, tl)

    k13 = f3(l

    1,

    l

    2,

    l

    3, t

    l

    )k21 = f1

    l1+tk11,

    l

    2+tk12,

    l

    3+tk13, t

    l+t

    k22 = f2l1+tk11,

    l

    2+tk12,

    l

    3+tk13, t

    l+t

    k23 = f3l1+tk11,

    l

    2+tk12,

    l

    3+tk13, t

    l+t

    RK-2

  • 7/24/2019 Analysis of Runge Kutta Method

    25/40

    functionMPO9p2()

    clear all;

    close all;

    Delta_t=0.1;

    t=0:Delta_t:25;

    phi=zeros(length(t),3);phi(1,:)=[0.0 0.0 0.332];

    forn=1:length(t)-1

    k1=f(phi(n,:));

    k2=f(phi(n,:)+Delta_t*k1);

    phi(n+1,:)=phi(n,:)+Delta_t*(k1/2+k2/2);

    end

    plot(t,phi(:,2),'ko-')

    hold on

    xlabel('t');

    ylabel('d\phi/dt');

    functiondphidt=f(phi)

    dphidt=[phi(2) phi(3) -0.5*phi(3)*phi(1)];

    0 5 10 15 20 250

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    t

    d!/dt

  • 7/24/2019 Analysis of Runge Kutta Method

    26/40

    End of Example O09.2

  • 7/24/2019 Analysis of Runge Kutta Method

    27/40

    CRANK-NICOLSON FORMULA

    l+1 =l +t1

    2

    f(l, tl) +f(l+1, tl+1)

    Apply Crank-Nicolson:

    dt(t) =f(, t)

    ODE to solve:

  • 7/24/2019 Analysis of Runge Kutta Method

    28/40

    GRAPHICAL COMPARISON

    tl+1

    t

    tl

    ll+1

    !El+1

    local

    tl+1)

    t

    !

    Explicit Euler:take slope at tl

    tl+1

    t

    tl

    l

    l+1

    ! El+1local

    tl+1)

    t

    !

    Implicit Euler:take slope at tl+1

    tl+1

    t

    tl

    l

    l+1

    ! El+1localtl+1)

    t

    !

    Crank-Nicolson:take average of slopesat tl and tl+1

    +1 = +tf( , t) +1 = +tf( +1, t +1) l+1 =l +t1

    2

    f(l, tl) +f(l+1, tl+1)

    (t) Illustration of explicit Eulers method

  • 7/24/2019 Analysis of Runge Kutta Method

    29/40

    ddt

    =f(l, tl)

    (t)

    t

    t

    l

    t

    l+1

    Error

    Predicted value of #l+1

    True value of #l+1

    Illustration of explicit Euler s method

    l+1 =l +tf(l, tl)

    (t)(tl+1)

    (tl)Eulers formula

    (t) Ill i f h i li i E l h d

  • 7/24/2019 Analysis of Runge Kutta Method

    30/40

    (t)

    t

    l

    t +1

    l

    t

    Predicted value of #l+1

    True value of #l+1

    Error

    +1 = +tf( +1, t +1)

    Illustration of the implicit Euler method

    Summary

  • 7/24/2019 Analysis of Runge Kutta Method

    31/40

    Error

    (t)

    tl

    tl+1

    l

    t

    Predicted value of !l+1

    True value of !l+1

    Error

    +1 = +tf( +1 , t+1)

    Illustration of the implicit Euler method

    d

    dt =f(l, tl)

    (t)

    t

    tl

    tl+1

    Error

    Predicted value of !l+1

    True value of !l+1

    Illustration of explicit Eulers method

    l+1 =l +tf(l, tl)

    (t)(tl+1

    )

    (tl)Eulers formula

    Summary

    (t)

    t

    t t+1

    l

    l+1 =l +t1

    2

    f(l, tl) +f(l+1, tl+1)

    Illustration of Crank Nicolson method

    Predicted value of #l+1

  • 7/24/2019 Analysis of Runge Kutta Method

    32/40

    Example O09.3:

    Using Crank-Nicolson method, solve

    For 0

  • 7/24/2019 Analysis of Runge Kutta Method

    33/40

    l+1 =l +t1

    2

    f(l, tl) +f(l+1, tl+1)

    d

    dt = 1 = f(, t)

    Crank Nicolson formula

    l+1 =l +t 12

    f(l, tl) +f(l+1, tl+1)

    d

    dt = 1

    1l 1

    l+1

    1 +

    t

    2

    l+1 =

    1

    t

    2

    l + t

    l+1 =

    1

    t

    2

    l +t

    1 +

    t

    2

    function MPO09p3()

  • 7/24/2019 Analysis of Runge Kutta Method

    34/40

    functionMPO09p3()

    close allclear all

    tmin=0.0;tmax=8.0;

    [t1,phi1]=MyCrankNicolson([tmin tmax],0.0,2.0);

    plot(t1,phi1,'ko-');hold onezplot(@(t)1-exp(-t),[0,8,0,2])xlabel('t');ylabel('\phi');legend('Crank-Nicolson','True');

    function[t,phi]=MyCrankNicolson(tspan,phi0,Delta_t)t=tspan(1):Delta_t:tspan(2);

    phi=zeros(length(t),1);

    phi(1)=phi0;forn=1:length(t)-1

    phi(n+1)=((1-Delta_t/2)*phi(n)+Delta_t)/(1+Delta_t/2);end

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    1!

    exp(!

    t)

    !

    Euler

    True

    l+1 =

    1

    t

    2

    l +t

    1 + t

    2

    Output

    Crank-Nicolson

  • 7/24/2019 Analysis of Runge Kutta Method

    35/40

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    1!exp(!t)

    !

    Euler

    True

    !t=2.0

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    !

    EulerTrue!t=2.0

    Compare implicit Euler, explicit Euler and Crank Nicolson method for !t=2.0

    0 1 2 3 4 5 6 7 80

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    1.6

    1.8

    2

    t

    1!exp(!t)

    !

    Euler

    True

    !t=2.0

  • 7/24/2019 Analysis of Runge Kutta Method

    36/40

    End of Example O09.3

  • 7/24/2019 Analysis of Runge Kutta Method

    37/40

    Example O09.4:Write a MATLAB program that uses the Crank-

    Nicolson method to solve

    for 0 < t < 10 with !(t=0)=1.

    Applying the Crank-Nicolson method, we obtain

    Analytical solution:

    d

    dt

    = i2

    (t) = ei2t

    l+1 =l +t1

    2

    i2l + i2l+1

    (1 it)l+1 = (1 + it)l

    l+1 = 1 + it

    1 itl

  • 7/24/2019 Analysis of Runge Kutta Method

    38/40

    functionMPO9p4()

    % Solve dphi/dt = i*2*phi, phi(0) = 1, 0 < t