06class Mathematica

download 06class Mathematica

of 32

Transcript of 06class Mathematica

  • 5/24/2018 06class Mathematica

    1/32

    How to solve ODEs

    using MATHEMATICA

    Plasma ApplicationModeling POSTECH

    Gan-Young Park and Jae-Koo LeeDepartment of Electronic and Electrical Engineering,POSTECH

    2006. 03. 22

    EECE490O

  • 5/24/2018 06class Mathematica

    2/32

    Plasma ApplicationModeling POSTECH

    Contents

    Basic usages of MATHEMATICA

    Solving ODEs

    - Eulers method

    - Predictor-Corrector method ( second-order )

    - Forth-order Runge-Kutta method

    - Tridiagonal matrix method

  • 5/24/2018 06class Mathematica

    3/32

    Plasma ApplicationModeling POSTECH

    References

    Textbook

    - Numerical and Analytical Methods for Scientists and

    Engineers Using Mathematica, Daniel Dubin, Wiley, 2003

    Web Lecture

    : http://www.mathought.com/main.htm

    http://www.mathought.com/main.htmhttp://www.mathought.com/main.htm
  • 5/24/2018 06class Mathematica

    4/32

    Plasma ApplicationModeling POSTECH

    Standard Screen

    Notebook :working window

    ( *.nb ) Palettes

    ( FilePalettes - )

    : a collection of numerical expressions or characters

  • 5/24/2018 06class Mathematica

    5/32

    Plasma ApplicationModeling POSTECH

    Basic Usages (1)

    Shift + enter: Execution

    (shift + enter)

    (shift + enter)

    In[1]indicates input contents in the line.

    Out[1]indicates the result of In[1]

    %: a symbol indicating the result obtained right before.

  • 5/24/2018 06class Mathematica

    6/32

    Plasma ApplicationModeling POSTECH

    Basic Usages (2)

    application of Palettes

    Font size: Format - Size -

  • 5/24/2018 06class Mathematica

    7/32

    Plasma ApplicationModeling POSTECH

    Basic Usages (3)

    The names of most of functions included start with a capital letter.

    included constants

    - Pi- Infinite

    - eE

    - i = I1

    ?(function) or ??(function)

    : shows the usage of function or options

  • 5/24/2018 06class Mathematica

    8/32

    Plasma ApplicationModeling POSTECH

    Basic Usages (4)

    symbol calculation

    The symbol * for a product can be replaced by blank.

  • 5/24/2018 06class Mathematica

    9/32

    Plasma ApplicationModeling POSTECH

    Basic Usages (5)

    ==( equality ), =( substitution ), :=( definition )

    Common feature

    Different feature

  • 5/24/2018 06class Mathematica

    10/32

    Plasma Application

    Modeling POSTECH

    Basic Usages (6)

    Common feature

    Different feature

  • 5/24/2018 06class Mathematica

    11/32

    Plasma Application

    Modeling POSTECH

    Basic Usages (7)

    Plot[ function, {variable, a, b}, options] : drawing 2-D graph between a and b

  • 5/24/2018 06class Mathematica

    12/32

    Plasma Application

    Modeling POSTECH

    Basic Usages (8)

    PlotStyle : a option for coloring

    Input - Color Selector

  • 5/24/2018 06class Mathematica

    13/32

    Basic Usages (9)

    PlotLabel : a option for labeling at top of graph

    AxesLabel : a option for labeling at axes

    AspectRatio : a option for adjusting the ratio of vertical to horizontal

    PlotRange : a option for restricting range to plot

  • 5/24/2018 06class Mathematica

    14/32

    Plasma Application

    Modeling POSTECH

    Basic Usages (10)

    DisplayFunction Identity : a option for not showing a graph, just memorizing it.

    DisplayFunction -> $DisplayFunction : a option for showing a graph memorized.

    Show : a function to display graphs which have been shown or memorized

  • 5/24/2018 06class Mathematica

    15/32

    Plasma Application

    Modeling POSTECH

    Basic Usages (11)

    Package

    Since path is assigned up to

    StandardPackages, just sub-paths

    should be written.

    The symbol ` is used in

    Mathematica instead of \.

  • 5/24/2018 06class Mathematica

    16/32

    Plasma Application

    Modeling POSTECH

    Basic Usages (12)

    Table [contents, {range of loop}]

  • 5/24/2018 06class Mathematica

    17/32

    Plasma Application

    Modeling POSTECH

    Basic Usages (13)

    Do [exp, {i,min,max,d}]

    For [start, test, i++, body]

    While [test, body]

    Since Do, For, While dont

    show results and save results,

    functions such as Print[] should

    be used for checking results.

    Thats a difference among

    Table and above functions.

  • 5/24/2018 06class Mathematica

    18/32

    Plasma Application

    Modeling POSTECH

    Basic Usages (14)

    Module [ {local variables}, contents ]

    : The variables written at {} in Module[ ] are used as local variables which doesnt

    affect global variables with same characters and cant be used out of Module[ ].

  • 5/24/2018 06class Mathematica

    19/32

    Plasma Application

    Modeling POSTECH

    Solving ODEs

    - Eulers method

    - Predictor-Corrector method

    ( second-order )

    - Forth-order Runge-Kutta method

    - Solving tridiagonal matrix

  • 5/24/2018 06class Mathematica

    20/32

    Plasma Application

    Modeling POSTECH

    Eulers method (1)

    0

    1 1 1

    [ , ], (0)

    ( ) ( ) ( , ( ))n n n n

    dvf t v v v

    dtv t v t t f t v t

  • 5/24/2018 06class Mathematica

    21/32

    Eulers method (2)

    Plasma Application

    Modeling POSTECH

  • 5/24/2018 06class Mathematica

    22/32

    Predictor-Corrector method ( 2nd-order )

    0

    1 1 1

    1 11

    [ , ], (0)

    ( ) ( ) ( , ( ))

    ( , ( )) ( , ( ))( ) ( )

    2

    n n n n

    n n n nn n

    dvf t v v v

    dtv t v t t f t v t

    f t v t f t v tv t v t t

  • 5/24/2018 06class Mathematica

    23/32

    Plasma Application

    Modeling POSTECH

    Forth-order Runge-Kutta method

    0

    1 1 1

    12 1 1

    23 1 1

    4 1 1 3

    1 1 2 3 4

    [ , ], (0)

    ( , ( ))

    ( , ( ) )2 2

    ( , ( ) )2 2

    ( , ( ) )

    1( ) ( ) [ 2 2 ]

    6

    n n

    n n

    n n

    n n

    n n

    dvf t v v v

    dt

    k t f t v t

    ktk t f t v t

    ktk t f t v t

    k t f t t v t k

    v t v t k k k k

    ( based on the Simpsons 1/3 rule)

  • 5/24/2018 06class Mathematica

    24/32

    Example 9.1 ( Nakamura )

    20 0.5 20

    ' 20 7exp( 0.5 ), y(0) 5,

    . : ( ) 5 (7 19.5)( )t t t

    y y t

    exact sol y t e e e

  • 5/24/2018 06class Mathematica

    25/32

    Program 9-1 ( Nakamura )

    0)()()(2

    2

    tkytydt

    dBtydt

    dM , y(0)=1, y(0)=0

    Changing 2ndorder ODE to 1storder ODE,

    )(),,()( tztzyftydt

    d

    0)()()( tkytBztzdt

    dM

    )()(),,()( tyM

    ktzM

    Btzygtzdt

    d

    (1)

    (2)

    y(0)=1

    z(0)=0

    Plasma Application

    Modeling POSTECH

  • 5/24/2018 06class Mathematica

    26/32

    Program 9-1 ( Nakamura )

    Plasma Application

    Modeling POSTECH

    To solve 2nd-order ODE using second-order Runge-Kutta method

  • 5/24/2018 06class Mathematica

    27/32

    Program 9-2 ( Nakamura )

    Plasma Application

    Modeling POSTECH

    1)(*)(' tytty y(0)=0)(

    )()('

    22tyt

    tyty

    y(0)=1

    To solve ODE using fourth-order Runge-Kutta method

    P 10 1 ( N k )

  • 5/24/2018 06class Mathematica

    28/32

    )2.0exp()()(2 xxyxy

    )10()10(,1)0( yyy

    Solve difference equation,

    With the boundary conditions,

    x = 0 1 2i = 0 1 2

    9 109 10

    112

    11 22

    )(

    iii

    iii yyyh

    yyyxy

    )2.0exp()2(2 11 iyyyy iiii )2.0exp()()(2 xxyxy

    1)0( yEspecially for i = 1, 2)2.0exp(25 21 yy

    known

    y(0)=1

    )10()10( yy

    Plasma Application

    Modeling POSTECH

    Program 10-1 ( Nakamura )

    P 10 1 ( N k )

  • 5/24/2018 06class Mathematica

    29/32

    For i = 10,

    1091110

    911

    10 22| yyyy

    yy

    y

    )10()10( yy

    )2exp(252 11109 yyy )2exp(5.05.42 109 yy

    Summarizing the difference equations obtained, we write

    )2.0exp(252 11 iiii xyyy 2)2.0exp(25 21

    yy

    )2exp(5.05.42 109 yy

    )2exp(5.0

    )32.0exp(

    )22.0exp(

    2)2.0exp(

    5.42

    252

    252

    25

    10

    3

    2

    1

    y

    y

    y

    y

    Tridiagonal matrix

    Program 10-1 ( Nakamura )

  • 5/24/2018 06class Mathematica

    30/32

    Solution Algorithm for Tridiagonal Equations (1)

    3

    2

    1

    3

    2

    1

    33

    222

    11

    0

    0

    D

    D

    D

    BA

    CBA

    CB

    3

    2

    1

    1

    2

    3

    2

    1

    33

    222

    1

    1

    21

    1

    2

    0

    0

    D

    D

    DB

    A

    BA

    CBA

    CB

    AB

    B

    A

    R2

    3

    2

    12

    3

    2

    1

    33

    222

    122

    0

    0

    D

    D

    DR

    BA

    CBA

    CRA

    3

    122

    12

    3

    2

    1

    33

    2122

    122

    0

    0

    0

    D

    DRD

    DR

    BA

    CCRB

    CRA

    2B 2D

    3

    2

    2

    3

    12

    3

    2

    1

    33

    2

    2

    32

    2

    3

    122

    0

    0

    0

    D

    DB

    A

    DR

    BA

    CB

    AB

    B

    A

    CRA

    R3

    233

    23

    12

    3

    2

    1

    233

    233

    122

    00

    0

    0

    DRD

    DR

    DR

    CRB

    CRA

    CRA

    3B 3D

    Based on Gauss elimination

    Plasma Application

    Modeling POSTECH

  • 5/24/2018 06class Mathematica

    31/32

    3

    23

    12

    3

    2

    1

    3

    233

    122

    00

    0

    0

    D

    DR

    DR

    B

    CRA

    CRA

    3

    33

    B

    D

    2

    33322

    3

    322332323 ,)(

    B

    ARCD

    A

    RDRCRA

    )(1 3222

    2 CDB

    1

    22211

    2

    211221212 ,)(

    B

    ARCD

    A

    RDRCRA

    )(1

    )(1

    211

    1

    211

    1

    1 CDB

    CDB

    Solution Algorithm for Tridiagonal Equations (2)

    Plasma Application

    Modeling POSTECH

    ( k )

  • 5/24/2018 06class Mathematica

    32/32

    Plasma Application

    Modeling POSTECH

    Program 10-1 ( Nakamura )

    To solve boundary-value problem

    using tridiagonal method