numerical method heun's method

Embed Size (px)

DESCRIPTION

heun methodmid point methodeuler method

Citation preview

  • Lecture 2: Introduction to Ordinary Differential Equation (ODE) and Euler Method KKEK214:2 Numerical Methods for Engineers 2

    Department of Chemical Engineering

  • Differential Equation

    Definition: a mathematical equation that consists of an unknown function and its derivatives.

    It expresses the relation between a function and its derivatives.

    Differential equations play a fundamental role in engineering because many physical phenomena are best formulated mathematically in terms of their rate of change.

    Examples: reaction rate, mass flow, heat transfer rate, acceleration, velocity, etc.

    vm

    cg

    dt

    dv

    MDH-KKEK2142 (Sem 2 2014/2015)

    2

    E.g.: Newtons Second Law (1)

    Where: = velocity g = gravitational constant t = time m = mass c = drag coefficient

    - dependent variable

    t- independent variable

  • Differential Equation

    When a function involves one independent variable, the equation is called an ordinary differential equation (or ODE).

    A partial differential equation (or PDE) involves two or more independent variables.

    MDH-KKEK2142 (Sem 2 2014/2015) 3

  • 4 MDH-KKEK2142 (Sem 2 2014/2015)

    Example: ODE in chemical reaction problem

    A chemical reaction of the type of

    (2)

    takes place in a reactor, the material balance can be applied as:

    Input = Output +

    + Generation Accumulation

    (3)

    For a batch reactor,

    Input = Output +

    + Generation Accumulation

    CBAk

    k

    1

    2

    Hence,

    ODE

    CBAC

    CBAB

    CBAA

    CkCCkdt

    dC

    CkCCkdt

    dC

    CkCCkdt

    dC

    21

    21

    21

  • Classification of ODEs

    ODEs are classified according to their order and their linearity.

    The order of an ODE is the order of the highest derivative present in that equation.

    1st order

    2nd order

    3rd order kx

    dx

    dyb

    dx

    yda

    dx

    yd

    kxdx

    dyy

    dx

    yd

    kxydx

    dy

    2

    2

    2

    3

    3

    2

    2

    MDH-KKEK2142 (Sem 2 2014/2015) 5

  • Classification of ODEs

    6 MDH-KKEK2142 (Sem 2 2014/2015)

    ODEs also can be classified according to its linearity, i.e. linear and nonlinear ODE.

    An ODE is nonlinear if it contains product of the dependent variable or its derivatives or both.

    Linear

    Nonlinear

    Nonlinear

    2

    2

    23 2

    3 2

    dyy kx

    dx

    d y dyy kx

    dx dx

    d y d y dya b kx

    dx dx dx

  • Degree of ODEs

    The degree of an ODE is the largest power to which the highest order derivative is raised.

    Has degree of 5

    Has degree of 2

    kxdx

    dyb

    dx

    yda

    dx

    yd

    kxdx

    dyb

    dx

    yda

    dx

    yd

    105

    2

    22

    3

    3

    2

    2

    25

    3

    3

    MDH-KKEK2142 (Sem 2 2014/2015)

    7

  • Runge-Kutta Methods

    However not all ODEs can be solved using analytical methods of calculus.

    Need to use numerical methods.

    Methods used to solve ODEs using iterative methods.

    Developed by German mathematicians C. Runge and M.W. Kutta.

    ),( yxf

    dx

    dy

    MDH-KKEK2142 (Sem 2 2014/2015) 8

  • Runge-Kutta Methods

    Eulers method

    Heuns method

    Midpoint method

    Higher Order Runge-Kutta (RK) method

    9 MDH-KKEK2142 (Sem 2 2014/2015)

  • Runge-Kutta Methods

    Solve ordinary differential equations of the form

    Numerical method in the general form of

    = +

    or in mathematical terms

    +1 = +

    Applied step by step to compute out the value in the future (trajectory of the solution).

    10 MDH-KKEK2142 (Sem 2 2014/2015)

    ),( yxfdx

    dy

  • Runge-Kutta Methods

    The type of RK methods can be differentiated by the manner in which the slope is estimated.

    The easiest one-step method: Eulers method.

    However, Eulers method has the least accurate predictions compared to other RK methods.

    11 MDH-KKEK2142 (Sem 2 2014/2015)

  • Eulers Method

    Named after Leonhard Euler (1707-1783).

    Swiss mathematician and physicist.

    He introduced several mathematical notations such as f(x), e (also known as Eulers number),

    for summation and i.

    He also had introduce the Eulers method to solve ordinary differential equation.

    It is the simplest method to solve an ODE with a given initial value, yo.

    MDH-KKEK2142 (Sem 2 2014/2015) 12

  • Eulers Method

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

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

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

    MDH-KKEK2142 (Sem 2 2014/2015) 13

    Figure 25.2 Eulers method

    ),( ii yxf

    hyxfyy iiii ),(1

  • 14 MDH-KKEK2142 (Sem 2 2014/2015)

    Example (pg. 708 in Text Book):

    Use Eulers method to numerically integrate

    From x=0 to x=4 with a step size of 0.5. The initial

    condition at x=0 is y=1.

    5.820122 23 xxxdx

    dy

  • Exercise (homework)

    Calculate the values from previous example for the subsequent steps, i.e. y(1.0), y(1.5), y(2.0), y(2.5), y(3.0), y(3.5) and y(4.0).

    Compare the value that you obtained using Eulers method with those obtained using exact solution of

    15 MDH-KKEK2142 (Sem 2 2014/2015)

    = 0.54 + 43 102 + 8.5 + 1

  • Error Analysis of the RK Method

    Numerical solutions of ODEs involves two types of error:

    i) Truncation/discretisation errors

    Caused by the nature of the techniques employed to approximate the value of y.

    ii) Round off errors

    Caused by the limited numbers of significant digits

    16 MDH-KKEK2142 (Sem 2 2014/2015)

  • Error Analysis of the RK Method

    Truncation errors:

    i) Local truncation error that results from an application of the method in question over single step.

    ii) Propagated truncation error that results from the approximation produced during the previous steps.

    17 MDH-KKEK2142 (Sem 2 2014/2015)

  • Error Analysis of the Eulers Method (Cont.)

    Total error = Local truncation error + Propagated truncation error

    18 MDH-KKEK2142 (Sem 2 2014/2015)

    Global truncation error

  • Error Analysis of the RK Method (Cont.)

    Conclusions:

    i) The global error can be reduced by decreasing h.

    ii) The method will provide error-free predictions if the underlying function is linear.

    19 MDH-KKEK2142 (Sem 2 2014/2015)

  • MATLAB

    Short for MATrix LABoratory

    Optimized to perform engineering and scientific calculations.

    Initially designed to perform matrix mathematics

    Provides a very extensive library of predefined functions to make technical programming tasks easier and more efficient.

    20 MDH-KKEK2142 (Sem 2 2014/2015)

    Advantages:

    1. Ease of use

    2. Platform independence

    3. Pre-defined functions

    4. Device-independent plotting

    5. Graphical User Interface

    6. MATLAB Compiler

    Disadvantages:

    1. Interpreted language execute more slowly

    2. Cost

  • MATLAB Environment: MATLAB Desktop

    21 MDH-KKEK2142 (Sem 2 2014/2015)

    The Command Window: User can enter interactive commands at the command prompt (>>) and it will be executed on the spot. It is easier to use script files / M-files

    The Command History Window

    Displays the commands that user has entered

    The MATLAB Workspace

    Stores the variable name and value

  • Getting Help

    Help Browser

    Type help on the command window

    Using lookfor command

    Other Important Functions

    Type demo to run MATLABs built-in demonstrations.

    clc : clear the Command Windows content.

    clear : clear the variables in the Workspace

    abort command by typing control-c

    22 MDH-KKEK2142 (Sem 2 2014/2015)

  • Important Programming Pitfalls

    Never use a variable with the same name as a MATLAB function or command. If you do so, that function or command will become inaccessible.

    If there is more than one function or command with the same name, the first one found on the search path will be executed.

    Never create an M-file with the same name as a MATLAB function or command.

    23 MDH-KKEK2142 (Sem 2 2014/2015)