k10 ordinary differetial equations

Upload: zeolite

Post on 06-Jul-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/18/2019 K10 Ordinary Differetial Equations

    1/14

    - Basic Ideas

    - Euler’s Method

    - Higher Order One-step Methods

    - Predictor-Corrector Approach

    - Runge-Kutta Methods

    - Adaptive Stepsize Algorithms

    Numerical Methods forNumerical Methods for Civil Civil Engineers Engineers 

    LectureLecture 1010 Ordinary Dif ferential Equations Ordinary Differential Equations 

    Mongkol JIRAVACHARADET 

    S U R A N A R E E INSTITUTE OF ENGINEERING

    UNIVERSITY OF TECHNOLOGY SCHOOL OF CIVIL ENGINEERING

    ( , )dy

     f t ydt 

    =

    where t = independent variable

    y = dependent variable

    Initial Condition   y(t 0) =   y0

    Rate of change of one variable with respect to another.

    2

    0d x dxma cv kx m c kxdt dt  

    + + = + + =

    Example: Mass-spring system m

    c

     x

    Ordinary Differential EquationsOrdinary Differential Equations

  • 8/18/2019 K10 Ordinary Differetial Equations

    2/14

     New value = Old value + slope x step size

    From Taylor series:

    2

    00 0 0 0

    ( )( ) ( ) ( ) ( ) ( )

    2!

    t t  y t y t t t y t y t 

    −′ ′′= + − + +L

    Retaining only first derivative: 0 0 0( ) ( , )t y h f t y= +

    where h = (t - t 0), y0 = y(t 0), and f (t 0, y0) = y’ (t 0)

     y

    t 0   t 1

    h

    Predict

    Trueerror 

    1 0 0 0( , ) y h f t y= +

    2 1 1 1( , ) y y h f t y= +1 1 1( , )i i i i y y h f t y− − −= +

    EULEREULER’’S METHODS METHOD

    Example: Manual Calculation with Euler’s Method

    2 , (0) 1dy

    t y ydt 

    = − =

    Exact solution:   ( )21

    2 1 54

    t  y t e−= − +

    t i   f (t i-1, yi-1)Euler 

     yi= yi-1+h f (t i-1, yi-1)Exact Error  

    Set h = 0.2

    0.0

    0.2

    0.4

    0.6

    NA

    0-2(1) = -2.0

    0.2-2(0.6) = -1.0

    0.4-2(0.4) = -0.4

    initial cond. = 1.0

    1.0+0.2(-2.0) = 0.6

    0.6+0.2(-1.0) = 0.4

    0.4+0.2(-0.4) = 0.32

    1.0000

    0.6879

    0.5117

    0.4265

    0

    -0.0879

    -0.1117

    -0.1065

  • 8/18/2019 K10 Ordinary Differetial Equations

    3/14

    Implementing Euler’s Method

    function [t,y] = odeEuler(diffeq,tn,h,y0)% Input: diffeq = (string) name of m-file that% evaluate right hand side of ODE% tn = stopping value of independent variable% h = stepsize% y0 = initial condition at t=0

    t = (0:h:tn)’;n = length(t);y = y0*ones(n,1);

    for i=2:ny(i) = y(i-1)+h*feval(diffeq,t(i-1),y(i-1));

    end 

    odeEuler.m

    Example: 2 , (0) 1, 0.2dy

    t y y hdt 

    = − = =

    function dydt = rhs1(t,y)dydt = t-2*y;

    rhs1.m

    >> [t,y] = odeEuler(‘rhs1’,0.6,0.2,1.0)

    t =0

    0.20000.40000.6000

    y =

    1.00000.60000.40000.3200

  • 8/18/2019 K10 Ordinary Differetial Equations

    4/14

    >> y0 = (1/4)*(2*t-ones(size(t))+5*exp(-2*t))

    y0 =

    1.0000

    0.68790.51170.4265

    Exact Solution :

    >> t0=0:0.01:0.6;>> y0 = (1/4)*(2*t0-ones(size(t0))+5*exp(-2*t0));>> plot(t0,y0,t,y)

    HeunHeun’’ss MethodMethod:: estimate slope from derivatives at the beginning

    and at the end of the interval.

    Improvements ofImprovements of EEuler uler ’’ss MMethodethod

    t i -1

    slope:

    k 1 = f (t i -1, y i -1)

    t i 

    Euler:

    y 0i = y i -1 + hk 1

    h

    slope:

    k 2 = f (t i , y 0

    i )

  • 8/18/2019 K10 Ordinary Differetial Equations

    5/14

    t i -1 t i 

    h

    2

    ),(),(

    2 slope Average

    0

    1121 i i i i  y t f y t f k k    +=+

    =   −−

       

         ++=   −

    221

    1

    k k hy y  i i 

    Use slope:

    (k 1 + k 2)/2

    1 1 1

    2 1 1 1

    1 21

    ( , )

    ( , )

    2

    i i

    i i

    i i

    k f t y

    k f t h y hk  

    k k  y y h

    − −

    − −

    =

    = + +

    += +

     yi-1

    t i-1

    h

    t i

    True

    Euler 

    Heun

    HeunHeun’’ss MMethodethod

    ( , )dy

     f t ydt 

    = 0i y 

  • 8/18/2019 K10 Ordinary Differetial Equations

    6/14

    Heun’s Method:

    1 1 1

    2 1 1 1

    1 21

    ( , )

    ( , )

    2

    i i

    i i

    i i

    k f t y

    k f t h y hk  

    k k  y y h

    − −

    − −

    =

    = + +

    += +

    1 1 1( , )i i i i y h f t y− − −= +Euler’s Method:

    0

    1 1 1Predictor: ( , )i i i i y y h f t y− − −= +

    0

    1 11

    ( , ) ( , )Corrector:

    2

    i i i ii i

     f t y f t y y y h   − −−

    += +

    Predictor Predictor --Corrector ApproachCorrector Approach

    Iterating the Corrector ofIterating the Corrector of HeunHeun’’ss MethodMethod

    To improve the estimation

    0

    1 11

    ( , ) ( , )

    2

    i i i ii

     f t y f t y y h   − −−

    ++

    i y

  • 8/18/2019 K10 Ordinary Differetial Equations

    7/14

    Example: Use Heun’s method to integrate

    0.84 0.5 , (0) 2 x y e y y′ = − =

    From x = 0 to 4, step size = 1

    Analytical solution:   ( )0.8 0.5 0.54

    21.3

     x x x y e e e− −= − +

    0

    1 1 1

    0 0

    1

    Predictor, ( , )

    2 1 (4 0.5(2)) 5

    i i i i y y h f t y

     y e

    − − −= +

    = + − =

    01 1

    1

    0 0.8(1)

    1

    ( , ) ( , )Corrector,

    2

    (4 0.5(2)) (4 0.5(5))2 1 6.7011

    2

    i i i ii i

     f t y f t y y y h

    e e y

    − −−

    += +

    − + −= + =

    True value:   ( )0.8(1) 0.5(1) 0.5(1)4

    2 6.19461.3

     y e e e− −= − + =

    6.1946 6.7011Relative error, 100% 8.18%

    6.1946t  E 

      −= × =

    0 0.8(1)nd

    1

    (4 0.5(2)) (4 0.5(6.7011))2 Corrector, 2 1

    2

    6.2758, 1.31%t 

    e e y

     E 

    − + −= +

    = =

    0 0.8(1)rd

    1

    (4 0.5(2)) (4 0.5(6.2758))3 Corrector, 2 1

    2

    6.3821, 3.03%t 

    e e y

     E 

    − + −= +

    = =Error may increases for large step sizes.

  • 8/18/2019 K10 Ordinary Differetial Equations

    8/14

    Iteration using up-arrow in MATLAB

    >> y = 2+1*(4*exp(0)-0.5*2)   % Predictor of y1

    y =  5

    >> y = 2+(1/2)*((4*exp(0)-0.5*2)+(4*exp(0.8*1)-0.5*y))y = 6.7011   % 1st Corrector of y1

    Enter Press and

    y = 6.7011   % 2nd Corrector of y1

    y = 6.2758   % 3rd Corrector of y1

    y = 6.3821   % 4th Corrector of y1

    y = 6.3555   % 5th Corrector of y1

    y = 6.3609   % until no change

    MATLAB’s Implementation

    function dydx = func1(x,y)dydx = 4*exp(0.8*x)-0.5*y;

    func1.m

    function [x,y] = odeHeun(diffeq,xn,h,y0,iter)

    % Input: iter = number of corrector iterations

    x = (0:h:xn)’;n = length(x);y = y0*ones(n,1);for i=2:n

    y(i) = y(i-1)+h*feval(diffeq,x(i-1),y(i-1));for j=1:iter

    y(i) = y(i-1)+h*(feval(diffeq,x(i-1),y(i-1)) ...+ feval(diffeq,x(i),y(i)))/2;

    end end 

    odeHeun.m

  • 8/18/2019 K10 Ordinary Differetial Equations

    9/14

    Et =0.002.683.093.173.18

    y_true =2.00006.194614.843933.677275.3390

    For x = 0 to 4, step size = 1, y(0) = 2, Iteration = 15

    >> [x,y] = odeHeun(‘func1’,4,1,2,15)

    x =

    01234

    y =2.00006.360915.302234.743377.7351

    Compare with Euler’s Method:

    >> [x,y] = odeEuler(‘func1’,4,1,2)

    x =01234

    y =2.00005.000011.402225.513256.8493

    Et =0.0019.2823.1924.2424.54

  • 8/18/2019 K10 Ordinary Differetial Equations

    10/14

    Comparison of True Solution with Euler’s and Heun’s Method

    >> xtrue = (0:0.1:4)’;>> ytrue =(4/1.3)*(exp(0.8*xtrue)-exp(-0.5*xtrue))...

    + 2*exp(-0.5*xtrue);

    >> [xeuler,yeuler] = odeEuler(‘func1’,4,1,2);>> [xheun,yheun] = odeHeun(‘func1’,4,1,2,15);>> plot(xtrue,ytrue,xeuler,yeuler,’o’,xheun,yheun,’+’)

    0 1 2 3 40

    20

    40

    60

    80

    x

          y

    True

    Euler

    Heun 15

    1

    1

    m

    i i l l  

     y y k γ −=

    = + ∑General Formula using Weighted Average of slopes

    For Heun’s method γ 1 =   γ 2 = 0.5

    The weights must satisfy1

    1

    m

    γ 

    ==∑

    Second-Order RK (Ralston’s) Method:

    1 1 2

    1 1 1

    2 1 1 1

    1 2

    3 3

    ( , )3 3

    ,4 4

    i i

    i i

    i i

     y y h k k 

    k f x y

    k f x h y k h

    − −

    − −

     = + +    

    =  = + +    

    RRUNGEUNGE--KUTTA (RK)KUTTA (RK) METHODSMETHODS

  • 8/18/2019 K10 Ordinary Differetial Equations

    11/14

    Third-Order Runge-Kutta Methods

    ( )1 1 2 346

    i i

    h y y k k k −= + + +

    where

    ( )

    1 1 1

    2 1 1 1

    3 1 1 1 2

    ( , )

    1 1,

    2 2

    , 2

    i i

    i i

    i i

    k f x y

    k f x h y k h

    k f x h y k h k h

    − −

    − −

    − −

    =

     = + +  

    = + − +

    Fouth-Order Runge-Kutta Methods

    where

    ( )

    1 1 1

    2 1 1 1

    3 1 1 2

    4 1 1 3

    ( , )

    1 1,

    2 2

    1 1,

    2 2

    ,

    i i

    i i

    i i

    i i

    k f x y

    k f x h y k h

    k f x h y k h

    k f x h y k h

    − −

    − −

    − −

    − −

    =

     = + +  

    = + + 

    = + +

    The most popular RK methods

    ( )1 1 2 3 42 26

    i i

    h y y k k k k −= + + + +Classical 4th-order RK:

  • 8/18/2019 K10 Ordinary Differetial Equations

    12/14

    Example: Use 4th-order RK method to integrate

    0.84 0.5 , (0) 2 x y e y y′ = − =

    From x = 0 to 4, step size = 1

    Step 1: x0 = 0, y0 = 2

    0

    1

    0.8(0.5)

    2

    0.8(0.5)

    3

    0.8(1)

    4

    4 0.5(2) 3

    14 0.5(2 3 1) 4.2173

    2

    1

    4 0.5(2 4.2173 1) 3.91302

    4 0.5(2 3.9130 1) 5.9457

    k e

    k e

    k e

    k e

    = − =

    = − + × × =

    = − + × × == − + × =

    ( )11

    2 3 2 4.2173 2 3.9130 5.94576

    6.2011

     y   = + + × + × +

    =

    True value:   ( )0.8(1) 0.5(1) 0.5(1)4

    2 6.1946

    1.3

     y e e e− −= − + =

    6.1946 6.2010Relative error, 100% 0.1038%

    6.1946t  E 

      −= × =

  • 8/18/2019 K10 Ordinary Differetial Equations

    13/14

    MATLAB’s Implementation

    function [x,y] = odeRK4(diffeq,xn,h,y0)

    x = (0:h:xn)’;n = length(x);y = y0*ones(n,1);for i=2:n

    k1 = feval(diffeq,x(i-1),y(i-1));k2 = feval(diffeq,x(i-1)+h/2,y(i-1)+k1*h/2);k3 = feval(diffeq,x(i-1)+h/2,y(i-1)+k2*h/2);k4 = feval(diffeq,x(i-1)+h,y(i-1)+k3*h);y(i) = y(i-1)+h/6*(k1+2*k2+2*k3+k4);

    end 

    odeRK4.m

    For x = 0 to 4, step size = 1, y(0) = 2, Iteration = 15

    >> [x,y] = odeRK4(‘func1’,4,1,2)

    x =01234

    y =2.00006.201014.862533.721375.4392

    y_true =2.00006.194614.843933.677275.3390

    Et(%) =0.000.10380.12500.13090.1328

  • 8/18/2019 K10 Ordinary Differetial Equations

    14/14

    MATLAB’s ode23 and ode45 Routines

    ode23 use second- and third-order RK simultaneously

    ode45 use fourth- and fifth-order RK simultaneously

    >> x0 = 0; xn = 4; y0 = 2;>> [x45,y45] = ode45(‘func1’,[x0,xn],y0);

    >> xtrue = (0:0.1:4)’;>> ytrue =(4/1.3)*(exp(0.8*xtrue)...

    - exp(-0.5*xtrue))+ 2*exp(-0.5*xtrue);>> plot(xtrue,ytrue,x45,y45)

    For x = 0 to 4, y(0) = 2

    Example:0.8

    4 0.5 , (0) 2 x

     y e y y′ = − =   func1.m 

     Adaptive Adaptive StepsizeStepsize Algorithms Algorithms