Sunteți pe pagina 1din 32

How to solve ODEs

using MATHEMATICA
Plasma Application
Modeling POSTECH
Gan-Young Park and Jae-Koo Lee
Department of Electronic and Electrical Engineering,
POSTECH
2006. 03. 22
EECE490O
Plasma Application
Modeling POSTECH
Contents
Basic usages of MATHEMATICA

Solving ODEs
- Eulers method
- Predictor-Corrector method ( second-order )
- Forth-order Runge-Kutta method
- Tridiagonal matrix method
Plasma Application
Modeling 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
Plasma Application
Modeling POSTECH
Standard Screen
Notebook :
working window
( *.nb )
Palettes
( File Palettes - )
: a collection of numerical expressions or characters
Plasma Application
Modeling 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.
Plasma Application
Modeling POSTECH
Basic Usages (2)
application of Palettes
Font size
: Format - Size -
Plasma Application
Modeling POSTECH
Basic Usages (3)
The names of most of functions included start with a capital letter.
included constants
- Pi
- Infinite
- e E
- i = I
1
? (function) or ?? (function)
: shows the usage of function or options
Plasma Application
Modeling POSTECH
Basic Usages (4)
symbol calculation
The symbol * for a product can be replaced by blank.
Plasma Application
Modeling POSTECH
Basic Usages (5)
== ( equality ), = ( substitution ), := ( definition )
< g[x] = eq. & g[x] := eq. >
Common feature
Different feature
Plasma Application
Modeling POSTECH
Basic Usages (6)
< g[x_] = eq. & f[x_] := eq. >
Common feature
Different feature
Plasma Application
Modeling POSTECH
Basic Usages (7)
Plot[ function, {variable, a, b}, options] : drawing 2-D graph between a and b
Plasma Application
Modeling POSTECH
Basic Usages (8)
PlotStyle : a option for coloring
Input - Color Selector
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
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
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 \.
Plasma Application
Modeling POSTECH
Basic Usages (12)
Table [contents, {range of loop}]
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.
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[ ].
Plasma Application
Modeling POSTECH
Solving ODEs
- Eulers method
- Predictor-Corrector method
( second-order )
- Forth-order Runge-Kutta method
- Solving tridiagonal matrix
Plasma Application
Modeling POSTECH
Eulers method (1)
0
1 1 1
[ , ], (0)
( ) ( ) ( , ( ))
n n n n
dv
f t v v v
dt
v t v t t f t v t

= =
= + A
Eulers method (2)
Plasma Application
Modeling POSTECH
Predictor-Corrector method ( 2
nd
-order )
0
1 1 1
1 1
1
[ , ], (0)
( ) ( ) ( , ( ))
( , ( )) ( , ( ))
( ) ( )
2
n n n n
n n n n
n n
dv
f t v v v
dt
v t v t t f t v t
f t v t f t v t
v t v t t

= =
= + A
+
= + A
Plasma Application
Modeling POSTECH
Forth-order Runge-Kutta method
0
1 1 1
1
2 1 1
2
3 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
dv
f t v v v
dt
k t f t v t
k t
k t f t v t
k t
k t f t v t
k t f t t v t k
v t v t k k k k



= =
= A
A
= A + +
A
= A + +
= A + A +
= + + + +
( based on the Simpsons 1/3 rule )
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

= + =
= +
Program 9-1 ( Nakamura )
0 ) ( ) ( ) (
2
2
= + + t ky t y
dt
d
B t y
dt
d
M
, y(0)=1, y(0)=0
Changing 2
nd
order ODE to 1
st
order ODE,
) ( ) , , ( ) ( t z t z y f t y
dt
d
= =
0 ) ( ) ( ) ( = + + t ky t Bz t z
dt
d
M
) ( ) ( ) , , ( ) ( t y
M
k
t z
M
B
t z y g t z
dt
d
= =
(1)
(2)
y(0)=1
z(0)=0
Plasma Application
Modeling POSTECH
Program 9-1 ( Nakamura )
Plasma Application
Modeling POSTECH
To solve 2
nd
-order ODE using second-order Runge-Kutta method
Program 9-2 ( Nakamura )
Plasma Application
Modeling POSTECH
1 ) ( * ) ( ' + = t y t t y
y(0)=0
) (
) (
) ( '
2 2
t y t
t y
t y
+
= y(0)=1
To solve ODE using fourth-order Runge-Kutta method
) 2 . 0 exp( ) ( ) ( 2 x x y x y = +
' '

) 10 ( ) 10 ( , 1 ) 0 ( y y y =
'
=
Solve difference equation,
With the boundary conditions,
- - - - - - - - -
x = 0 1 2
i = 0 1 2
9 10
9 10
1 1
2
1 1
2
2
) (
+
+
+ =
+
=
' '
i i i
i i i
y y y
h
y y y
x y
) 2 . 0 exp( ) 2 ( 2
1 1
i y y y y
i i i i
~ + +
+
) 2 . 0 exp( ) ( ) ( 2 x x y x y = +
' '

1 ) 0 ( = y
Especially for i = 1, 2 ) 2 . 0 exp( 2 5
2 1
+ = y y
known
y(0)=1
) 10 ( ) 10 ( y y = '
Plasma Application
Modeling POSTECH
Program 10-1 ( Nakamura )
For i = 10,
10 9 11 10
9 11
10
2
2
| y y y y
y y
y = =

~
'
) 10 ( ) 10 ( y y = '
) 2 exp( 2 5 2
11 10 9
= + y y y ) 2 exp( 5 . 0 5 . 4 2
10 9
= + y y
Summarizing the difference equations obtained, we write
) 2 . 0 exp( 2 5 2
1 1 i i i i
x y y y = +
+
2 ) 2 . 0 exp( 2 5
2 1
+ = y y
) 2 exp( 5 . 0 5 . 4 2
10 9
= + y y
(
(
(
(
(
(



+
=
(
(
(
(
(
(

(
(
(
(
(
(




) 2 exp( 5 . 0
) 3 2 . 0 exp(
) 2 2 . 0 exp(
2 ) 2 . 0 exp(
5 . 4 2
2 5 2
2 5 2
2 5
10
3
2
1

y
y
y
y
Tridiagonal matrix
Program 10-1 ( Nakamura )
Solution Algorithm for Tridiagonal Equations (1)
(
(
(

=
(
(
(

(
(
(

3
2
1
3
2
1
3 3
2 2 2
1 1
0
0
D
D
D
B A
C B A
C B
|
|
|
(
(
(
(
(

=
(
(
(

(
(
(
(
(

3
2
1
1
2
3
2
1
3 3
2 2 2
1
1
2
1
1
2
0
0
D
D
D
B
A
B A
C B A
C
B
A
B
B
A
|
|
|
R
2
(
(
(

=
(
(
(

(
(
(

3
2
1 2
3
2
1
3 3
2 2 2
1 2 2
0
0
D
D
D R
B A
C B A
C R A
|
|
|
(
(
(

=
(
(
(

(
(
(

3
1 2 2
1 2
3
2
1
3 3
2 1 2 2
1 2 2
0
0
0
D
D R D
D R
B A
C C R B
C R A
|
|
|
2
B
'
2
D
'
(
(
(
(

'
'
=
(
(
(

(
(
(
(

'
'
'
3
2
2
3
1 2
3
2
1
3 3
2
2
3
2
2
3
1 2 2
0
0
0
D
D
B
A
D R
B A
C
B
A
B
B
A
C R A
|
|
|
R
3
(
(
(

'

'
=
(
(
(

(
(
(

2 3 3
2 3
1 2
3
2
1
2 3 3
2 3 3
1 2 2
0 0
0
0
D R D
D R
D R
C R B
C R A
C R A
|
|
|
3
B
'
3
D
'
Based on Gauss elimination
Plasma Application
Modeling POSTECH
(
(
(

'
'
=
(
(
(

(
(
(

'
3
2 3
1 2
3
2
1
3
2 3 3
1 2 2
0 0
0
0
D
D R
D R
B
C R A
C R A
|
|
|
3
3
3
B
D
'
'
= |
2
3
3 3 2 2
3
3
2 2 3 3 2 3 2 3
, ) (
B
A
R C D
A
R
D R C R A
'
=
'
=
'
= + | | | |
) (
1
3 2 2
2
2
| | C D
B

'
'
=
1
2
2 2 1 1
2
2
1 1 2 2 1 2 1 2
, ) (
B
A
R C D
A
R
D R C R A = = = + | | | |
) (
1
) (
1
2 1 1
1
2 1 1
1
1
| | | C D
B
C D
B

'
'
= =
Solution Algorithm for Tridiagonal Equations (2)
Plasma Application
Modeling POSTECH
Plasma Application
Modeling POSTECH
Program 10-1 ( Nakamura )
To solve boundary-value problem
using tridiagonal method

S-ar putea să vă placă și