You are on page 1of 29

&

&


(chmpechl@auth.gr)
&

2007

1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.2 Runge-Kutta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.3 - . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1.5 ode MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11


1.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2 MATLAB-ODE Functions

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

2.3.1 The Van der Pol equation - 1

. . . . . . . . . . . . . . . . . . . . . . . . 22

2.3.2 The Van der Pol equation - 2 . . . . . . . . . . . . . . . . . . . . . . . . . 22


2.3.3 The Robertson problem

. . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.3.4 The bouncing ball problem . . . . . . . . . . . . . . . . . . . . . . . . . . 25

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 -

1970 Fehlberg Runge-Kutta


. , Runge-Kutta
f (t, x) . Fehlberg
, 50%,
n ,

n(t)3

(1.1)

Runge-Kutta ,
f (t, x). ,
20%
.
,

max(RelT olkxk, AbsT ol)


RelT ol AbsT ol ,
.
(1.1)
, ,
, .
:
RelT ol AbsT ol ,

. ,
( , ) .
Runge-Kutta ode23 ode45
MATLAB .

1.3 -
xk+1 ' (tn+1 ), t x,

[t1 , t2 , . . . , tk ] [x1 , x2 , . . . , xk ]. x i = f (ti , xi ),

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)

xk+1 (1.3) xk+1


(1.2)
. , ,

(tk2 , x k2 ),(tk1 , x k1 ) (tk , x k ).


- .
(tk1 , x k1 ) (tk , x k ),

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
.
-.

x 1 = (a 2)x1 + 2(a 1)x2


x 2 = (1 a)x1 + 2(1 2a)x2
:

x1 (0) = 1
x2 (0) = 0

x1 (t) = 2et eat


x2 (t) = et + eat
, 1 = 1
2 = 1/a. 1 2 ,
ode23.
. 1 2
(1 = 1, 2 = 1/2000)

1.4

.
eat ,
, a, .
.
, .
. RungeKutta -
,
.

. William Gear .
(1.3) ,
Newton-Raphson .

(x) = 0

0 = (xi ) + 0 (xi )(xi+1 xi )


x
n , [x1 , x2 , , xn ]

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


,

1.5 ode MATLAB

11

1.5 ode MATLAB


ode . t0 x0 ,
.
.

.
-
ode45 Runge-Kutta (4,5) (Dormand-Prince) .

x(tn ) x(tn1 ). , ode45


.
ode23 Runge-Kutta (2,3) (Bogacki-Shampine) .
ode45
. ode45 ode23
x(tn1 ) x(tn ).
ode113 Adams-Bashforth-Moulton PECE .
ode45 x =

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

g(xn (a), tn + ah) = 0


tn + ah.

MATLAB

M (t, x)x = f (t, x)


M (t, x) (mass matrix) . M (t, x)

x = M 1 (t, x)f (t, x)


x0 . M (t, x)
M (t, x)x = f (t, x) -
(Differential-Algebraic Equation, DAE) .
x0 xp0 M (t, x)xp0 = f (t, x).
x0 , xp0
.

. MATLAB ode15s ode23t
.
:
, ode45, ,
ode23, .
,
.

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 .

2.2.2 Van der Pol


1. .

x = f (x, t)
..

Van der Pol

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.

Reltol, AbsTol, NormControl

Refine, Stats

Jacobian, JPattern

Mass, MvPattern, MStateDependence

Events

odeset

options=odeset(name1,value1,name2,value2,...)
.


e(i) .

2.2 ode

18

, RelTol, , AbsTol.

|e(i)| max {RelT ol|x(i)|, AbsT ol(i)}

(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

M (t, x)x = f (t, x)

(2.2)

M (t, x) (mass matrix) . M (t, x)


x = M 1 (t, x)f (t, x)


x0 . 2.2
, M ,
.
M (t, x) M (t, x)x = f (t, x)
- (Differential-Algebraic Equation, DAE) .
x0 xp0
M (t0 , x0 )xp0 = f (t0 , x0 ).
x0 , xp0
.
. MATLAB
ode15s ode23t .
Mass [Constant matrix-Function]
.
MvPattern [Sparse matrix] . M P (i, j) = 1 k (i, k) M (t, x) j x M P (i, j) = 0.
MStateDependence [none-strong] x.
M (t) none
M (t, x) strong .

2.3

21

. (event
detection) zero-crossing

g(x, t)
g(x, t) = 0
xn
g(xn , tn ) g(xn+1 , tn+1 ).
a

g(xn (a), tn + ah) = 0


tn + ah.
Events [Function] .

[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

2.3.1 The Van der Pol equation - 1


Van der Pol
.

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)];

2.3.2 The Van der Pol equation - 2


Van der Pol
.

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

Solution of Van der Pol equation, mi = 1


3
x

x2

solution

10
t(sec)

15

20

Figure 2.1: Van der Pol

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)];

2.3.3 The Robertson problem


.

function Robertson

2.3

24

Solution of Van der Pol equation, mi = 1000


2
1.5
1

solution x1

0.5
0
0.5
1
1.5
2
2.5

500

1000
t(sec)

1500

2000

Figure 2.2: Van der Pol

M=[1 0 0;0 1 0;0 0 0];


options=odeset(Mass,M,RelTol,1e-4,AbsTol,[1e-6 1e-10 1e-6]);
tspan=[0 4*logspace(-6,6)];
x0=[1;0;1e-3];
[t,x]=ode15s(@f,tspan,x0,options);
semilogx(t,[x(:,1) 1e4*x(:,2) x(:,3)]);
title(Robertson DAE Problem)
legend(x_1,1e4*x_2,x_3)
function dx=f(t,x)
dx=[-0.04*x(1)+1e4*x(2)*x(3);
0.04*x(1)-1e4*x(2)*x(3)-3e7*x(2) 2;
x(1)+x(2)+x(3)-1];

2.3

25

Robertson DAE Problem


1
x1

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

Figure 2.3: Robertson

2.3.4 The bouncing ball problem



.

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

Ball trajectory and events


20

15

height

10

10
t(sec)

15

Figure 2.4:

20

[1] Adrian Biran & Moshe Breiner, MATLAB 5 , , , , 1999


[2] Differential Equations, MATLAB User s Guide - Chapter 15, The MathWorks Inc., Natick

28

You might also like