Professional Documents
Culture Documents
Example 1.
Numerically solve the Kepler’s equations describing the
motion of one body around another of equal mass
located at the origin under the influence of gravity.
u v
u , v
r3 r3
1 e
u (0) 1 e, u(0) 0, v (0) 0, v(0)
1 e
y3
y1
y y 4
2 y
or 1 3
y3 r
y4 y2
r 3
1 e
y1 (0) 1 e, y2 (0) 0, y3 (0) 0, y4 (0)
1 e
B. Uniqueness of Solutions
There are many physical problems that do not have
solutions for certain values of parameters. Also some
may have more than one solution. Clearly we will have
trouble computing a solution that does not exist, and if
there is more than one solution then we will have trouble
computing the right one (the desired solution).
Example 2.
(y) 2 1 0
No solution
Example 3.
Consider a heavy mass pendulum system with light
and rigid rod hanging vertically from a friction-less
pivot. The angle that the pendulum makes with the
vertical satisfies the ODE
sin( ) 0
Suppose that the pendulum is hanging vertically so that
the initial angle (0) 0 and that we thump the mass to
give an initial velocity (0) . If the velocity is small, the
pendulum will swing back and forth. If the velocity is
large, the pendulum will swing over the top and it will
whirl around the pivot forever. There is a very special
solution that occurs as the solutions change from
oscillatory to increasing. Physically, it corresponds to an
initial velocity that causes the pendulum to approach and
then come to rest vertically and upside down. Clearly,
this solution is unstable: an arbitrary small change to the
initial velocity gives rise to a solution that is very
different. In other words, this situation is ill-condition.
(0) 2.5
(0) 1.5
Time (s)
Example 4.
Consider the ODE
y y cos(t )
One may plot the direction (slope) field as
C. Computational Errors
Digital systems like calculators and computers
hardly make a mistake, since they follow the
programmed order faithfully. Nonetheless, we often
encounter some numerical errors in the computing results
made by digital systems, mostly coming from
representing the numbers in finite bits, which is an
intrinsic limitation of digital world. If you let the
computer compute something without considering what
is called the finite-word-length effect, you might obtain a
weird answer. In this section we will see how the
computer represents and stores the numbers. Then we
think about the cause of computational error in order not
to be deceived by unintentional mistakes of the computer
and, it is hoped, to be able to take some measures against
them.
MATLAB uses the IEEE 64-bit floating-point
number system to represent all numbers. It has a word
structure consisting of the sign bit, the exponent field,
and the mantissa field as follows:
Sign bit
S = b63 = 0 for positive numbers
1 for negative numbers
Exponent field
E = (b62b61b60 ・ ・ ・ b52):
adopting the excess 1023 code
E = Exp − 1023 = {0, 1, . . . , 211 − 1 = 2047} − 1023
= {−1023,−1022, . . . ,+1023,+1024}
Mantissa field
M = (b51b50 . . . b1b0):
Example 5.
>> (3/2-1)*2-1
ans =
0
>> (4/3-1)*3-1
ans =
-2.2204e-016
Example 6.
Solving the two linear algebraic equations
17x1 + 5x2 = 22 and 1.7x1 + 0.5x2 = 2.2
yn 1 yn nh 2
Writing out the first few equations
y1 y0 0
y2 y1 h 2 y0 h 2
y3 y2 2h 2 y0 3h 2
…
The Euler’s method leads to solution
n(n 1)h 2
y n y0
2
n 2h2
(compare with the exact solution yn y0 )
2
For the given step size h = 0.1,
y(10) occurs at n = 10/0.1 = 100 thus
100 99 0.12
y (10) y100 0 = 49.5
2
(comparing with exact solution = 50.0)
50
45
40
35
30
25
20
15
10
0
0 1 2 3 4 5 6 7 8 9 10
Example 8. Consider the problem (Ref. 1)
t 4 6t 3 12t 2 14t 9
y y
2
(1 t ) 2
with y(0) = 2
(1 t )(2 t )
(The exact solution is y (t ) )
1 t
f (t , y )dt
tn 1
y (t ) t
n
tn
(11)
tn 1
Example 10.
Consider the same problem as in Example 8.
t 4 6t 3 12t 2 14t 9
y y
2
(1 t ) 2
with y(0) = 2
(1 t )(2 t )
(The exact solution is y (t ) )
1 t
Heun’s Method:
with step size h = 0.4 (4 steps)
= 0.2 (8steps)
t
1.4 Runge-Kutta Method (Trapezoidal method)
Although Heun’s method is a little better than the
Euler’s method, it is still not accurate enough for most
real world problems. The Runge-Kutta method can have
higher order of truncation error is one of the most widely
used methods for solving differential equations. Its
algorithm is described below.
Considering the second order Runge-Kutta method,
the algorithm is expressed by
y n 1 y n a1k 1 a2k 2 (16)
where k 1 hf (tn , y n )
k 2 hf (tn b1h, y n b2k 1 )
h2 f f
y n 1 y n hf (tn , y n ) t y f (19)
2
Recall Eq.(16)
y n 1 y n a1hf (tn , y n ) a2 hf (tn b1h, y n b2 hf (tn , y n ))
(20)
with the last term can be expanded as
f
f (tn b1h, y n b2 hf (tn , y n )) f (tn , y n ) b1h
t tn , y n
f
b2 hf (tn , y n ) (21)
y tn , y n
f
a2b2 h 2f (tn , y n )
y tn , yn
with term hf a1 a2 = 1
f
with term h2 b1a2 = 0.5
t tn , y n
f
with term h 2f (tn , y n ) b2 a2 = 0.5
y tn ,y n
There is one free unknown, one may set
a1 a2 0.5 and b1 b2 1 (22)
Therefore the second order Runge-Kutta algorithm
becomes
h
y n 1 y n f1 f2 (23)
2
where f1 f (tn , y n )
f 2 f (tn 1 , y n hf (tn , y n ))
This is, in fact, equivalent to Heun’s method. Another
popular algorithms in the second order Runge-Kutta is
known as the modified Euler method (when b1=0.5).
h
Predictor p n 1 y n (55f n 59f n 1 37fn 2 9f n 3 )
24
251
Modifier m n 1 p n 1 (c n p n )
270
Corrector
h
c n 1 y n (9f (tn 1 , m n 1 ) 19fn 5f n 1 9f n 2 )
24
and
19
y n 1 c n 1 (cn 1 p n 1 )
270
3.2 Hamming Method
Hamming method use explicit linear four-step method
for predictor and use modified implicit linear three-step
method for corrector (but different from Adams-
Bashforth-Moulton method) as:
(with modification formulas)
4h
Predictor p n 1 y n 3 (2f n f n 1 2f n 2 )
3
112
Modifier m n1 p n 1 (c n p n )
121
Corrector
1
c n 1 [(9y n y n 2 3h(f (tn 1 , m n 1 ) 2f n f n 1 )]
8
and
9
y n 1 c n 1 (c n 1 p n 1 )
121
4. MATLAB Application
Syntax
[T,Y] = solver (odefun, tspan, y0)
[T,Y] = solver (odefun, tspan, y0, options)
[T,Y,TE,YE,IE] = solver (odefun, tspan, y0, options)
sol = solver (odefun,[t0 tf], y0 ...)
- options
Structure of optional parameters that change the default
integration properties. This is the fourth input argument.
[T, Y] = solver (odefun, tspan, y0, options)
You can create options using the odeset function.
Stiff Solvers
xg
becomes
1 0 0 x1 7 4 0 x1 1
0 1 0 105 x 4 8 4 106 x 1 105 x
2 2 g
0 0 1 x3 0 4 4 x3 1
Write the first-order differential equations using state-
space form using
y1 x1
y x
2 2
y x
y 3 3
y4 x1
y5 x2
y6 x3
get
0 M 1K 1
y y xg
I 0 0
or
y1 0 0 0 70 40 0 y1 1
y 0 0 0 40 80 40 y2 1
2
y3 0 0 0 0 40 40 y3 1
4sin(20t )
y4 1 0 0 0 0 0 y4 0
y5 0 1 0 0 0 0 y5 0
y6 0 0 1 0 0 0 y6 0
with initial conditions @ t=0 (from rest)
y1 0
y 0
2
y3 0
y 4 0
y5 0
y6 0
Implement into MATLAB and use ode45 to numerically
solve above problem from t=0 to t=60 s
MATLAB M-File
clear
global A B
M=eye(3)*1e5;
K=[7 -4 0; -4 8 -4; 0 -4 4]*1e6;
A=[zeros(3) -inv(M)*K; eye(3) zeros(3)];
B=[4 4 4 0 0 0]';
t1=cputime;
[T,Y]=ode45(@building3,[0 60],zeros(1,6));
tcom=cputime-t1; disp(tcom)
disp(max(Y(:,6))
plot(T,Y(:,6));
title('Top Floor Movement')
ylabel('Displacement (m)');
xlabel('Time (s)')
0.1
0.05
Displacement (m)
-0.05
-0.1
-0.15
-0.2
0 10 20 30 40 50 60
Time (s)
Beside the ability of solving the ODE by numerical
techniques. The MATLAB can be used to analytically
solve the ODE as well. The following will briefly
demonstrate this powerful tool.
1) y' + y - 2 = 0
2
x /2
2) y' + xy = e
1
3) y y 3 x 2 3
y
x
4) y 4 y 8 x 2 x
2
5) y 2 y 3 y 4e
2x
Example of MATLAB M-File
clear
DE='D2y+2*Dy-3*y=4*exp(2*t)'; %INPUT OF EQ.
t1=cputime;
y=dsolve(DE);
t2=cputime;
pretty(y); disp(t2-t1)
1) (2*exp(t)+C1)/exp(t) ; 0.05s
2) (t+C1)*exp(-1/2*t^2); 0.11s
3) [ 1/(-6*t+C1)^(1/2)/t]
[ -1/(-6*t+C1)^(1/2)/t] ; 0.09s
4) -1+1/2*t-2*t^2+C1*exp(2*t)+C2*exp(-2*t)
0.14s
5) -1/4*exp(t)+9/20*exp(-3*t)+4/5*exp(2*t);
0.18s
Plot of the symbolic function --> ezplot(y)
10
-6 -4 -2 0 2 4 6
t
References
1 An Introduction to Numerical Analysis, Suli and
Mayers, Cambridge University Press (2003)
2 Solving odes with MATLAB, Shampine, Gladwell
and Thomson, Cambridge University Press (2003)
3 An Introduction to Ordinary Differential Equations,
Jame Robinson, Cambridge University Press (2004)
4 An Introduction to Programming and Numerical
Methods in MATLAB, Otto and Denier, Springer
(2005)
5 Applied numerical methods using MATLAB, Yang,
Cao and Chung, Wiley (2005)