Professional Documents
Culture Documents
Simulation
Simulation
&
(chmpechl@auth.gr)
&
2007
1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Runge-Kutta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 - . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2 ode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.1 ode . . . . . . . . . . . . . . . . . . . . 15
2.2.2 Van der Pol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2.3 options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
. . . . . . . . . . . . . . . . . . . . . . . . 22
. . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.1
.
. .
. MATLAB, SIMNON, IMSL, NAG
SIMULINK.
.
Cauchy
x 1 = f1 (t, x)
..
.
x n = fn (t, x)
x = f (t, x)
n = 1
. , ,
2
1.1
= f (t, )
, f ,
x0 = x(t0 ) (t0 , x0 )
(t) , (t0 ) = x0 (
).
. , Cauchy
[x0 , x1 , . . . , xn ] [t0 , t1 , . . . , tn ], xi (ti ). ti
, . ,
.
x0 t0 ,
x1 = x(t1 ) t1 = t0 + t.
,
t.
,
, , ,
, .
, x1
t1 x2 ,
t2 = t1 + t ( t ,
, , ).
- Cauchy .
x2 , ,
: [t1 , t2 ]
(t1 ), , x1 ,
(t1 ). , ,
.
x(t + t)
Taylor . , 0 (t) = f (t, x)
.
d
f (t, x)
dt
f (t, x) dx
f (t, x)
f (t, x) +
= f (t, x) +
f (t, x)
=
t
x dt
t
x
00 (t) =
1.2 Runge-Kutta
, f ,
. (t0 + t) n Taylor .
(t)n+1 . ,
Taylor , . ,
Taylor f ,
1 f
f
x1 (t0 + t) x0 + f + (
+
f )t t
2 t
x
(t)3 .
[t0 , t0 + t]
Taylor .
,
19o
.
Runge-Kutta , -
.
1.2 Runge-Kutta
20o Karl Runge & Wilhelm Kutta
, ,
f (t, x) t0 ,
[t0 , t0 + t] (t0 , t0 + t)
Taylor .
, f (t, x) t x,
x(t0 + t)
(t0 + t) (t)3 ,
Taylor .
1 = f (t0 , x0 )
2 = f (t0 + at, x0 + 1 t)
1.2 Runge-Kutta
= 1 1 + 2 2
Taylor
Runge-Kutta . Runge-Kutta
x(t0 + t)
x(t0 + t) = x0 + t
1 , 2 ,
1 + 2 = 1
1
2 =
2
Runge-Kutta , , Taylor ,
Taylor .
= 1 1 + 2 2
= 1 f + 2 (f + ft)
= (1 + 2 )f + 2 ft
1
= f + ft
2
. ,
Runge-Kutta f (t, x)
(t)5 ,
Taylor .
Runge-Kutta
, , ,
, . .
. 200%
, , ,
, , t.
1950 -. Runge-Kutta , ,
. ,
1.3 -
(1.1)
Runge-Kutta ,
f (t, x). ,
20%
.
,
1.3 -
xk+1 ' (tn+1 ), t x,
1.3 -
i ).
(t
Z
tk+1
xk+1 = xk +
)d
(
(1.2)
tk
, ,
Simpson . ,
.
xk+1 ' xk +
x k + x k+1
x k + f (tk+1 , xk+1 )
t = xk +
(tk+1 tk )
2
2
xk+1
xk+1 = xk +
x k + f (tk+1 , xk+1 )
t
2
(1.3)
xk+1
. -
(p)
P ( ) = x k +
x k x k1
( tk )
tk tk1
(1.4)
(1.3)
, , xk+1 ,
x k + x k+1
(tk+1 tk )
2
x k + f (p) (tk+1 , xk+1 )
= xk +
(tk+1 tk )
2
xk+1 = xk +
-
,
Runge-Kutta Fehlberg .
, (, .) .
1.4
MATLAB ode113,
- (Adams) .
1.4
, Runge-Kutta
-, .
.
,
.
, , ,
.
Runge-Kutta
.
Runge-Kutta . , Runge-Kutta
.
-.
x1 (0) = 1
x2 (0) = 0
1.4
.
eat ,
, a, .
.
, .
. RungeKutta -
,
.
. William Gear .
(1.3) ,
Newton-Raphson .
(x) = 0
1 (x) = 0
2 (x) = 0
..
.
n (x) = 0
,
(i, j)
i
)i,j =
x
xj
xk+1 x,
1.4
10
(x) = x + xk +
x k + f (tk+1 , x)
(tk+1 tk )
2
tk+1 tk
= 1 +
f (tk+1 , x)
x
2
x
Newton-Raphson xi+1 ,
0 = (xi ) +
(xi )(xi+1 xi )
x
,
, , -. ,
.
,
.
,
, , Runge-Kutta .
.
.
MATLAB
ode23s, ode15s, ode23t ode23tb. , MATLAB
,
11
f (t, x) .
.
,
. .
.
ode15s . (Backward Differentiation Formulas , BDFs) Gear .
ode113 , .
ode45
.
ode23s Rosenbrock 2.
ode15s
.
ode15s.
1.6
12
ode23t .
.
ode23tb TR-BDF2 , Runge-Kutta
2. ode23s
ode15s .
1.6
.
-.
, .
.
. ,
.
-
Runge-Kutta ,
. , , -
Runge-Kutta .
. ,
. MATLAB ,
.
, ode .
MATLAB (event) .
(event detection) zero-crossing
1.6
13
g(x, t)
g(x, t) = 0
xn
g(xn , tn ) g(xn+1 , tn+1 ).
a
CHAPTER 2
MATLAB-ODE Functions
2.1
MATLAB . (Initial Value Problems, IVPs )
(Ordinary Differential Equations, ODEs ) -
(Differential-Algebraic Equations, DAEs ) .
2.2 ode
MATLAB . ,
.
ode45
..
Runge-Kutta
ode23
..
Runge-Kutta
ode113
..
Adams
ode15s
.. DAEs
NDFs(BDFs)
ode23s
..
Rosenbrock
ode23t
.. DAEs
Trapezoidal Rule
ode23tb
..
TR-BDF2
14
2.2 ode
15
ode . x
t . x x1 , x2 , . . . , xn . , x(t)
.
x0 t0 .
x = f (x, t)
x(t0 ) = x0
f (x, t) ,
. , x(t) ode. ode
. t0 x0 ,
.
, . ,
.
2.2.1 ode
, ode ,
[t,x]=ode_solver(@odefun,tspan,x0 )
ode_solver .
odefun .
dx=odefun(t,x) t - dx, x
.
tspan .
.
x0 .
2.2 ode
16
t
.
x .
.
Van der Pol .
x = f (x, t)
..
x 1 x2 x + x = 0
x 1 = x2
x 2 = 1 x21 x2 x1
2. MATLAB .
function dx=odefun(t,x)
mi=1;
dx=[x(2);mi*(1-x(1) 2)*x(2)-x(1)];
3. .
. , .
[t,x]=ode45(@odefun,[0 20],[2;0]);
t x
.
2.2 ode
17
4. . plot
.
2.2.3 options
options
options
. ode options
[t,x]=ode_solver(odefun,tspan,x0 ,options);
.
.
options.
Refine, Stats
Jacobian, JPattern
Events
odeset
options=odeset(name1,value1,name2,value2,...)
.
e(i) .
2.2 ode
18
, RelTol, , AbsTol.
(2.1)
,
.
.
RelTol . |x(i)|
AbsTol(i) x(i) .
RelTol
AbsTol(i) . x(i)
AbsTol(i)
x(i)
.
.
RelTol [Positive scalar {103 }]
x.
. ,
AbsTol(i) .
AbsTol [Positive scalar or vector {106 }]
. AbsTol(i)
, .
AbsTol
.
NormControl [on-{off}] .
|e| max{RelT ol|x|, AbsT ol}
on . off
2.1
.
Refine [Positive Integer]
Refine . Refine=1
. Refine>1
2.2 ode
19
Refine
. Refine ode
ode45 1 ode45 4. Refine
, ode
.
Stats [on-{off}]
. on
.
.
f (t, x).
f
.
x
LU .
.
f
,
x
f (t, x).
=
x
f1
x1
f2
x1
..
.
f1
x2
f2
x2
..
.
..
(ode15s, ode23s, ode23t, ode23tb)
. , .
Jacobian [Function-Constant matrix] .
.
JPattern [Sparse matrix of {0,1}]
.
. JP (i, j) = 1
fi (t, x) xj JP (i, j) = 0 .
2.2 ode
20
.
.
DAEs
MATLAB
(2.2)
2.3
21
. (event
detection) zero-crossing
g(x, t)
g(x, t) = 0
xn
g(xn , tn ) g(xn+1 , tn+1 ).
a
[value,isterminal,direction]=events(t,x)
value , isterminal direction i
i .
value(i) i .
isterminal(i)=1 i
isterminal(i)=0 .
direction(i)=1 value(i)
(), direction(i)=-1
value(i) () direction(i)=0
.
2.3
ode .
.
2.3
22
function Van_der_Pol
global mi
mi=1;
tspan=[0 20];
x0=[2;0];
[t,x]=ode45(@vdp,tspan,x0);
plot(t,x(:,1),b-,t,x(:,2),g-.)
title([Solution of Van der Pol equation, mi = ,num2str(mi)])
xlabel(t(sec))
ylabel(solution)
legend(x_1,x_2)
legend boxoff
grid
function dx=vdp(t,x)
global mi
dx=[x(2);mi*(1-x(1) 2)*x(2)-x(1)];
function Van_der_Pol
global mi
mi=1000;
options=odeset(Jacobian,@J);
tspan=[0 2000];
x0=[2;0];
[t,x]=ode15s(@vdp,tspan,x0,options);
2.3
23
x2
solution
10
t(sec)
15
20
plot(t,x(:,1),b-)
title([Solution of Van der Pol equation, mi =,num2str(mi)])
xlabel(t(sec))
ylabel(solution x_1)
legend boxoff
grid
function dx=vdp(t,x)
global mi
dx=[x(2);mi*(1-x(1) 2)*x(2)-x(1)];
function dfdx=J(t,x)
global mi
dfdx=[0 1;-2*mi*x(1)*x(2)-1 mi*(1-x(1) 2)];
function Robertson
2.3
24
solution x1
0.5
0
0.5
1
1.5
2
2.5
500
1000
t(sec)
1500
2000
2.3
25
0.9
1e4*x2
x3
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
10
10
10
10
10
10
10
function bouncing_ball
global g
g=10;
refine=12;
options=odeset(Events,@events,Refine,refine);
tstart=0;
tfinal=20;
x0=[0;20];
tout=[];
xout=[];
teout=[];
xeout=[];
ieout=[];
2.3
while tstart<tfinal
[t,x,te,xe,ie]=ode23(@f,[tstart,tfinal],x0,options);
tstart=t(end);
x0=[0;-0.9*xe(2)];
tout=[tout;t];
xout=[xout;x];
teout=[teout;te];
xeout=[xeout;xe];
ieout=[ieout;ie];
end
plot(tout,xout(:,1),b,teout,xeout(:,1),bo)
title(Ball trajectory and events)
xlabel(t(sec))
ylabel(height)
grid
function dx=f(t,x)
global g
dx=[x(2);-g];
function [value,isterminal,direction]=events(t,x)
value=x(1);
isterminal=1;
direction=-1;
26
2.3
27
15
height
10
10
t(sec)
15
Figure 2.4:
20
28