numerical methodslecture 9: numerical solution of ode’s math259 numerical analysis. 20. the final...

39
Numerical Methods Mustafa YILMAZ School of Mechanical Engineering University of Marmara Lecture 9 – Numerical Solution of ODE’s

Upload: others

Post on 08-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

  • Numerical Methods

    Mustafa YILMAZSchool of Mechanical Engineering

    University of Marmara

    Lecture 9 – Numerical Solution of ODE’s

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 2

    Example: the falling parachutist

    v: dependent variable (function)t: independent variable

    Basics

    dtdx

    =y

    vmcg

    dtdv

    −=

    0kxdtdxc

    dtxdm 2

    2=++

    Differential equation: an equation composed of an unknown function and its derivatives

    Ordinary differential equation: if there is only one independent variable

    Partial differential equation: if there are two or more independent variables

    Order of ODE: the order of the highest derivative in the equation

    Example: second order ODE

    Reduction of order: higher-order ODE can be reduced to a system of 1st-order ODE

    0kxdtdxc

    dtxdm 2

    2=++

    =++

    =

    0kxcydtdym

    dtdxy

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 3

    Why Study Differential Equations?

    0sinθlg

    dtθd2

    2=+

    θ of change of rate : dtdθ

    Many physical phenomena are best formulated mathematically in terms of their rate of change (which is derivative)!

    Example: motion of a swinging pendulum

    ( )θ of change of rate of change of rate dtdθ of change of rate :

    dtθd2

    2

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 4

    ODE and Engineering Practice

    Fundamental lawsEmpirical observations ODE SolutionsAnalytical/numerical

    methods

    Sequence of the application of ODEs for engineering problems

    Independent variable: spatial and temporal

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 5

    Ordinary Differential Equations

    0,

    0

    2/1

    2

    2

    2

    =++=

    =++

    −=

    Kxcydtdymy

    dtdx

    Kxdtdxc

    dtxdm

    vm

    Kgdtdv

    ODEs order 1st of Set

    Order

    damper mass Spring

    is derivative highest since Order

    brick falling Exampleρ

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 6

    Noncomputer Methods for Solving ODEs

    vmcg

    dtdv

    −= dtvmcgv ∫

    −=

    0=+ sinθlg

    dtθd2

    2

    One particular useful analytical integration technique: linearization

    an(x)y(n) + an-1(x)y(n-1) + … +a1(x)y’+ a0(x)y = f(x)

    Differential equation Integration SolutionconversionAnalytical integration

    techniques

    This can be solved analytically!

    0θlg

    dtd

    2

    2=+

    θ

    ( ) ( )( )tc/me1c

    gmtv −−=

    Sinθ ≈ θ if θ is small

    (non-linear) (linear)

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 7

    Solution by Integration15810450 234 ++−+−= x.xxx.y

    differentiation

    For an nth-order ODE, n conditions are required to obtain a unique solution

    x.xxxdxdy 5820122 23 +−+−=

    ( )dx.xxxy ∫ +−+−= 5820122 23

    Cx.xxx.y ++−+−= 5810450 234

    Multiple solutions

    integration

    Initial-value problem

    All n conditions are specified at a same value of x n conditions occur at different x

    Boundary-value problem

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 8

    1st Order ODEs

    • We are going to look at first order (and first degree) ODEs

    • These take the general form:

    • Some examples:

    ),( yxfdxdy

    =

    xedxdy

    xydxdy

    ydxdy

    yxdxdy

    x 4

    7

    2

    )2sin(3

    +=

    +=

    =

    +=

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 9

    1st Order ODEs

    • The equation is not enough to give us a specific curve– whether we use numerical or analytical methods

    • e.g. has the solution

    • e.g. has the solution

    and we need to be able to also work out the value of C or of A to get the full solution

    ),( yxfdxdy

    =

    xdxdy

    = Cxy +=2

    2

    ydxdy 2=

    xAey 2=

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 10

    So how do we use NM?

    • We see the ‘true’ solution is obtained by integrating

    – which provides a constant of integration necessary for a full solution

    • We need boundary conditions to be able to fully solve the

    problem

    • There are two broad types (for general DEs)

    • Initial value problems

    • Boundary value problems

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 11

    Boundary Conditions

    – Initial value problems

    • We have one known condition for the problem (for

    example, at x=0, y=2)

    • This is what we are going to look at

    – Boundary value problems

    • We know values at two points, for example a metal bar

    with two different temperatures at the two ends

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 12

    Consider

    L=0.1HE=5V

    R=10 Ω

    1st order ODE

    Solution is:

    Steady State Soln. :

    ERtidt

    tdiL =+ )()(

    )1()( / LRtss eIti−−=

    REI ss =

    )(ti

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 13

    • Notice that can be rearranged to be:

    • which we can see is of the same form as our general

    case

    • with

    ERtidt

    tdiL =+ )()(

    LiRE

    dtdi −

    =

    ),( yxfdxdy

    =

    LiREtifyxf

    txiy−

    =↔

    ↔↔

    ),(),(

    We don’t have to use x and y as our symbols!

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 14

    Graphically ( a reminder !)

    i

    Iss

    t0

    )(ti

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 15

    • Notice that we’ve chosen an example that has an

    analytical solution.

    • This means that if we practice finding a numerical

    solution to the problem we have an accurate solution to

    check it against.

    – Obviously if we already have the analytical solution we

    wouldn’t normally bother to do it numerically!

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 16

    First Step

    • Returning to our problem:

    • Closing the switch gives our initial condition of t=0, i=0 so

    – di/dt at t=0 is E/L , the initial gradient

    • So from this we could estimate a value of i for a small

    increment in t (∆t)

    LRtiE

    dttdi )()( −

    =

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 17

    First Step

    i

    ti0 = 0

    i1

    ∆t

    Gradient = E/L

    i1 = (E/L) . ∆t

    0(t0) (t1)

    Solving the general case

    this next point would be y1=y0+f(x0,y0)∆x

    ),( yxfdxdy

    =

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 18

    More Steps

    • Since we now know i1 we can find the gradient at t=∆t :

    – di/dt = (E-i1R) / L

    • Hence we can predict the next value (i2) using the same

    process i2=i1+ ((E-i1R) / L) ∆t and, for all steps, the formula is

    in+1=in+ ((E-inR) / L) ∆t

    • The theory behind this approach comes from Taylor’s

    Theorem (we use Taylor Series taken to the first order) but the

    method just described is known as Euler’s Method

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 19

    Euler’s Method

    • The formula (the recurrence relation) for solving the general case

    of is yn+1=yn+f(xn,yn)∆x

    • We obtain a series of discrete points (x0,y0), (x1,y1),…, (xn,yn),

    (xn+1,yn+1) which can be linked together to give an approximation to

    the desired curve

    • Notice that in general to calculate the next point we need the x and

    y values of the previous point. This means we must also keep track

    of the x values via xn+1=xn+ ∆x

    ),( yxfdxdy

    =

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 20

    The final result

    • So, for our electrical example, since we know i0=0 we can solve & plot!

    • We do this numerically by– Asking the user the limits ( a & b )

    • a=t0=0 in our specific example

    • b is the stopping point

    – Requesting the interval size (called ∆x in our general case but sometimes h is also used)

    • ∆x =∆t in our specific example

    – Start at the first point (a), calculate the value at a+ ∆x, and continue until b is reached

    – We print out the answer

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 21

    The Unified Form of Solving ODE

    ( )dxyx,fdy =

    ( )hh,y,xyy iii1i φ+=+

    Depending on function φ, there are different solutions.

    The Runge-Kutta methods are the most popular.

    00ii xx at y condition initial and xxh =−= +1

    ( )yx,fdxdy

    =

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 22

    The Euler’s Method

    ( )hy,xfyy iiii +=+1

    ( )iixx

    y,xfdxdy

    i

    ===

    φ

    Error estimate:( )

    nn

    ni2i

    iii Rhn!yh

    2!yhyyy +++

    ′′+′+=+ 1

    ( ) ( ) ( )( )( )

    nnii

    1-n3ii2ii

    iiii Rhn!

    y,xfh

    3!y,xfh

    2!y,xfhy,xfyy ++

    ′′+

    ′++=+ 1

    Et = Et,2 + Et,3 + … + Et,n

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 23

    Example of Euler’s Method

    8.520x12x2xdxdy 23 +−+−=

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 24

    Tank mixing problem

    tccV

    Vcc

    ccV

    Vdtdc

    iinii

    in

    −+=

    −=

    + )(

    )(

    1tank

    tank

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 25

    Mixing tank

    ∆t Error Etat t=600

    300 1.4150 0.61100 0.3950 0.1930 0.1115 0.05510 0.0365 0.0183 0.011

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 26

    Quantify the error

    ( ) ( )

    order? is error So

    on centred for series Taylor Write

    ...6

    )(2

    )()()()( 321

    1

    +∆′′′

    +∆′′

    +∆′+=+

    +

    ttcttcttctctc

    c c

    iiiii

    ii

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 27

    Not quite – accumulation of errors

    ( )

    ( ) Ntc

    ttcEEEEE

    N

    i

    Ntotal

    2

    2

    321

    2

    2)(

    ...

    ∆′′

    ∆′′

    =

    ++++=

    error daccumulate total so error, this has step each But

    order 1st waserror the that beforesaw We

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 28

    Euler Error

    Local errorlk=yk-y*k-1(tk)

    Global errorek=yk-y(tk)

    ty

    y’=λy

    Chart1

    0000

    0.10.10.10.1

    0.20.20.20.2

    0.30.30.30.3

    0.40.40.40.4

    0.50.50.50.5

    0.60.60.60.6

    0.70.70.70.7

    0.80.80.80.8

    0.90.90.90.9

    1111

    1.11.11.11.1

    1.21.21.21.2

    1.31.31.31.3

    1.41.41.41.4

    y'

    y'

    y'

    y'

    t

    y

    0.5

    0.6

    0.7

    0.8

    0.552585459

    0.6631025508

    0.7736196427

    0.8841367345

    0.6107013791

    0.7328416549

    0.8549819307

    0.9771222065

    0.6749294038

    0.8099152845

    0.9449011653

    1.0798870461

    0.7459123488

    0.8950948186

    1.0442772883

    1.1934597581

    0.8243606354

    0.9892327624

    1.1541048895

    1.3189770166

    0.9110594002

    1.0932712802

    1.2754831603

    1.4576950403

    1.0068763537

    1.2082516245

    1.4096268952

    1.611002166

    1.1127704642

    1.3353245571

    1.5578786499

    1.7804327428

    1.2298015556

    1.4757618667

    1.7217221778

    1.9676824889

    1.3591409142

    1.6309690971

    1.9027972799

    2.1746254628

    1.502083012

    1.8024996144

    2.1029162168

    2.4033328192

    1.6600584614

    1.9920701536

    2.3240818459

    2.6560935382

    1.8346483338

    2.2015780006

    2.5685076673

    2.9354373341

    2.0275999834

    2.4331199801

    2.8386399768

    3.2441599735

    Sheet1

    y' =y

    dty0y1y2y3

    0.10.50.60.70.8

    ty'y'y'y'

    00.50.60.70.8

    0.10.5525854590.66310255080.77361964270.8841367345

    0.20.61070137910.73284165490.85498193070.9771222065

    0.30.67492940380.80991528450.94490116531.0798870461

    0.40.74591234880.89509481861.04427728831.1934597581

    0.50.82436063540.98923276241.15410488951.3189770166

    0.60.91105940021.09327128021.27548316031.4576950403

    0.71.00687635371.20825162451.40962689521.611002166

    0.81.11277046421.33532455711.55787864991.7804327428

    0.91.22980155561.47576186671.72172217781.9676824889

    11.35914091421.63096909711.90279727992.1746254628

    1.11.5020830121.80249961442.10291621682.4033328192

    1.21.66005846141.99207015362.32408184592.6560935382

    1.31.83464833382.20157800062.56850766732.9354373341

    1.42.02759998342.43311998012.83863997683.2441599735

    Sheet1

    y'

    y'

    y'

    y'

    t

    y

    Sheet2

    Sheet3

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 29

    Euler Stability

    kk

    k

    kkk

    t

    hyy

    hyyhyy

    eytyyy

    )1(

    )1(

    )(

    0

    1

    0

    λ

    λλ

    λ λ

    +=

    +=+=

    ==′

    +

    that So

    Euler

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 30

    Effect of Step Size on Euler’s Method

    8.520x12x2xdxdy 23 +−+−=

    Comparing h = 0.5 and h=0.25 Error of y(5) as a function of number of steps

    h = 0.5

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 31

    However

    • Whilst the methods are OK, the error may be large

    • Error in every step

    – because only using first order of Taylor Series

    • Error accumulates because use approximate (i.e. errored) values

    to generate following step

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 32

    A better solution…

    • Is to use higher orders of the series

    – We can use 2nd or higher orders of the Taylor series (error is

    then related to ∆t2)

    • A more popular approach is to use Runge-Kutta

    methods

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 33

    Systems of ODEs

    ( )

    ( )

    ( )n2nn

    n2

    n2

    y,...,y,yx,fdx

    dy

    y,...,y,yx,fdxdy

    y,...,y,yx,fdxdy

    1

    122

    111

    =

    =

    =

    sinxedxdyc

    dxydb

    dx3yda dx2

    23+=++ −

    Example:

    ( )122

    1

    1

    1 cy-by-sinxeadx

    dy

    ydxdy

    ydxdy

    2dx +=

    =

    =

    Engineering applications can involve thousands of simultaneous ODEs!

    conversion

    (n equations require n initial conditions)

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 34

    Euler’s Method for System of ODEs

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 35

    Example of System of ODEs

    ( )

    −=

    =

    34

    4

    y16.1sindt

    dy

    ydt

    dy3

    −=

    =

    12

    2

    16.1ydt

    dy

    ydtdy1

    y1 = θ, y2 = dθ/dt

    0116 =+ sinθdtθd2

    2.

    (linear approximation when θ is small)

    y3 = θ, y4 = dθ/dt

    (exact conversion)

    small initial θ large initial θ

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 36

    Sets of ODEs

    So ODE problems can be reduced to a set of N first-order ODEs

    Not completely specified –needs boundary conditions• initial value problems

    • boundary value problems

    Niyyyxfdx

    xdyNi

    i ,...,1),...,,()( 21 == equations for

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 37

    General Procedure

    • Re-write the dy and dx terms as Δy and Δx and multiply by Δx

    • Literally doing this is Euler’s method

    Niyyyxfdx

    xdyNi

    i ,...,1),...,,()( 21 == equations for

    xyxfyy

    yxfxxy

    yxfdx

    xdy

    iiii ∆+=

    =∆

    =

    + ),(

    ),()(

    ),()(

    1

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 38

    Improvements to Euler’s Method

    Euler

    Heun’s method (predictor-corrector)

    Procedurecalc yi+1 with Euler (predictor)calc slope at yi+1calc average slopeuse this slope to calc new yi+1 (corrector)

    xyxfyy

    yxfdx

    xdy

    iiii ∆+=

    =

    + ),(

    ),()(

    1

  • MATH259 Numerical AnalysisLecture Numerical Solution of ODE s9: ’ 39

    Home Work of Chapter 25From the text book:

    25.1, 25.2, 25.4, 25.5, 25.7a, 25.10

    Additional:

    Numerical MethodsBasicsSlayt Numarası 3Slayt Numarası 4Ordinary Differential EquationsSlayt Numarası 6Slayt Numarası 71st Order ODEs1st Order ODEsSo how do we use NM?Boundary ConditionsConsiderSlayt Numarası 13Graphically ( a reminder !)Slayt Numarası 15First StepFirst StepMore StepsEuler’s MethodThe final resultThe Unified Form of Solving ODEThe Euler’s MethodExample of Euler’s MethodTank mixing problemMixing tankQuantify the errorNot quite – accumulation of errorsEuler ErrorEuler StabilityEffect of Step Size on Euler’s MethodHoweverA better solution…Systems of ODEsEuler’s Method for System of ODEsExample of System of ODEsSets of ODEsGeneral ProcedureImprovements to Euler’s MethodHome Work of Chapter 25