슬라이드 1 - Pohang University of Science and Technology
Download
Report
Transcript 슬라이드 1 - Pohang University of Science and Technology
EECE490O
How to solve ODEs
using MATHEMATICA
2006. 03. 22
Gan-Young Park and Jae-Koo Lee
Department of Electronic and Electrical Engineering,
POSTECH
Plasma Application
Modeling POSTECH
Contents
• Basic usages of MATHEMATICA
• Solving ODEs
- Euler’s 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 = 1 → I
• ? (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
Plasma Application
Modeling POSTECH
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
Basic Usages (10)
• DisplayFunction → Identity : a option for not showing a graph, just memorizing it.
• Show : a function to display graphs which have been shown or memorized
• DisplayFunction -> $DisplayFunction : a option for showing a graph 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 don’t
show results and save results,
functions such as Print[] should
be used for checking results.
• That’s 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 doesn’t
affect global variables with same characters and can’t be used out of Module[ ].
Plasma Application
Modeling POSTECH
Solving ODEs
- Euler’s method
- Predictor-Corrector method
( second-order )
- Forth-order Runge-Kutta method
- Solving tridiagonal matrix
Plasma Application
Modeling POSTECH
Euler’s method (1)
dv
f [t , v], v(0) v0
dt
v(tn ) v(tn 1 ) t f (tn 1 , v(tn 1 ))
Plasma Application
Modeling POSTECH
Euler’s method (2)
Plasma Application
Modeling POSTECH
Predictor-Corrector method ( 2nd-order )
dv
f [t , v], v(0) v0
dt
v (tn ) v(tn 1 ) t f (tn 1 , v(tn 1 ))
v(tn ) v(tn 1 ) t
f (tn 1 , v(tn 1 )) f (tn , v (tn ))
2
Forth-order Runge-Kutta method
dv
f [t , v], v(0) v0
dt
k1 t f (tn 1 , v(tn 1 ))
k
t
, v(tn 1 ) 1 )
2
2
k
t
k3 t f (tn 1 , v(tn 1 ) 2 )
2
2
k4 t f (tn 1 t , v(tn 1 ) k3 )
k2 t f (tn 1
1
v(tn ) v(tn 1 ) [k1 2k2 2k3 k4 ]
6
( based on the Simpson’s 1/3 rule )
Plasma Application
Modeling POSTECH
Example 9.1 ( Nakamura )
y ' 20 y 7 exp(0.5t ),
y(0) 5,
exact sol.: y(t ) 5e20t (7 19.5)(e0.5t e20t )
Program 9-1 ( Nakamura )
d2
d
M 2 y (t ) B y (t ) ky(t ) 0 ,
dt
dt
y(0)=1, y’(0)=0
Changing 2nd order ODE to 1st order ODE,
d
y (t ) f ( y, z , t ) z (t )
dt
M
(1)
y(0)=1
d
z (t ) Bz (t ) ky (t ) 0
dt
d
B
k
z (t ) g ( y, z , t )
z (t )
y (t )
dt
M
M
(2) z(0)=0
Plasma Application
Modeling POSTECH
Program 9-1 ( Nakamura )
To solve 2nd-order ODE using second-order Runge-Kutta method
Plasma Application
Modeling POSTECH
Program 9-2 ( Nakamura )
To solve ODE using fourth-order Runge-Kutta method
y' (t ) t * y(t ) 1 y(0)=0
y(t )
y' (t ) 2 2
t y (t )
y(0)=1
Plasma Application
Modeling POSTECH
Program 10-1 ( Nakamura )
Solve difference equation,
2 y( x) y( x) exp(0.2 x)
With the boundary conditions,
y(0) 1, y(10) y(10)
x=0
i=0
1
1
2
2
known
y(0)=1
9
9
10
10
y(10) y(10)
y( x)
yi 1 2 yi yi 1
yi 1 2 yi yi 1
2
h
2 y( x) y( x) exp(0.2 x)
Especially for i = 1, y(0) 1
2( yi 1 2 yi yi 1 ) yi exp(0.2i)
5 y1 2 y2 exp(0.2) 2
Plasma Application
Modeling POSTECH
Program 10-1 ( Nakamura )
For i = 10, y(10) y(10)
y |10
y11 y9
y10 y11 y9 2 y10
2
2 y9 5 y10 2 y11 exp(2)
2 y9 4.5 y10 0.5 exp(2)
Summarizing the difference equations obtained, we write
5 y1 2 y2
exp(0.2) 2
2 yi 1 5 yi 2 yi 1 exp(0.2xi )
2 y9 4.5 y10 0.5 exp(2)
Tridiagonal matrix
5 2
y1 exp(0.2) 2
2 5 2
y exp(0.2 2)
2
y3 exp(0.2 3)
2 5 2
2 4.5 y10 0.5 exp(2)
Solution Algorithm for Tridiagonal Equations (1)
Based on Gauss elimination
B1
A
2
0
A2
A
2
0
C1
B2
A3
R2C1
B2
A3
A2 R2C1
R3 A
3
B2
0
B2
A3
0
0 1 D1
C2 2 D2
B3 3 D3
0 1 R2 D1
C2 2 D2
B3 3 D3
0 R2 D1
1
A3 A3
C2 2 D2
B2
B2
B3 3 D3
R2
A2
B B1
1
A2
0
A2
A2
C1 0
D1
1
B1
B
1
B2
C2 2 D2
A3
B3 3 D3
A2
0
0
0 1 R2 D1
B2
D2
B2 R2C1 C2 2 D2 R2 D1
A3
B3 3
D3
A2
0
0
R2C1
R2C1
A3
0
1 R2 D1
R3C2 2 R3 D2
B3 R3C2 3 D3 R3 D2
B3
D3
0
Plasma Application
Modeling POSTECH
Solution Algorithm for Tridiagonal Equations (2)
A2
0
0
R2C1
A3
0
0 1 R2 D1
R3C2 2 R3 D2
B3 3 D3
A32 R3C23 R3 D2 2
2
D3
3
B3
R3
A
( D2 C23 ) , R3 3
A3
B2
1
( D2 C23 )
B2
R2
A2
A21 R2C12 R2 D1 1
( D1 C12 ) , R2
A2
B1
1
1
1 ( D1 C12 ) ( D1 C12 )
B1
B1
Plasma Application
Modeling POSTECH
Program 10-1 ( Nakamura )
To solve boundary-value problem
using tridiagonal method
Plasma Application
Modeling POSTECH