Chapter XI Solution of Ordinary Differential Equationsweb2.uwindsor.ca/courses/engineering/ahmed/S...
Transcript of Chapter XI Solution of Ordinary Differential Equationsweb2.uwindsor.ca/courses/engineering/ahmed/S...
173
dydx
f x y y x y= =( , ); ( ) 0 0 (11.1)
Chapter XI
Solution of Ordinary Differential EquationsSeveral approaches are described for the solution of ODE (ordinary differential
equations). These include:• The Taylor series Method• The Euler Method• The Improved Euler Method• The Runge-Kutta Methods.
We will begin our presentation on the numerical solution of a first-order ODE and later onextend the concepts to any order ODE.
11.1 Problem StatementGiven
where y(x0) = x0 is the initial condition needed to solve the problem. That is, y = y0 at x = x0.
Determine y as a function of x in either tabulated form or graph. Table 10.1 shows thetabulated solution required.
Table 11.1 Tabulated results.
x x0 x1 ... xn-2 xn-1
y y0 y1 ... yn-2 yn-1
Where x x xi i= +−1 ∆and )x is given.
10.2 The Taylor Series Method
Expand y(x) about x0 using Taylor series:
174
y x y hf x yh
f x yh
f x yh
f x y( ) ( , )!
( , )!
( , )!
( , )= + + ′ + ′ ′ + ′ ′ ′ +0 0 0
2
0 0
3
0 0
4
0 02 3 4! (11.2)
y x y x y x x xy x
x x
y xx x
yx x
( ) ( ) ( )( )( )!
( )
( )!
( )!
( )( )
= + ′ − +′ ′
− +
′ ′ ′− + − +
0 0 00
02
00
34
04
2
3 4 !
df x yf x y
xdx
f x yy
dy( , )( , ) ( , )
0 00 0 0 0= +
∂∂
∂∂
dfdx
fx
fy
f F= + =∂∂
∂∂ 1 (11.3)
d fdx
Fx
Fy
f F
d fdx
Fx
Fy
f F
d fdx
Fx
Fy
f Fi
ii i
i
2
21 1
2
3
32 2
3
1 1
= + =
= + =
= + =− −
∂∂
∂∂
∂∂
∂∂
∂∂
∂∂
"
"
(11.4)
Let , therefore:x x h− =0
Note that
We will drop the (x0,y0) from here on. Dividing by dx we get
Similarly one can derive:
Example. Express the solution of
175
f x y Ff x y x y Ff x y x y Ff x y x y F
= − − =′ = − − − − = − + + =′ ′ = + − − = − − =′ ′ ′ = − − − − = − + + =
22 1 2 2 2
2 1 2 2 22 1 2 2 2
0 0 1
0 0 0 0 2
0 0 0 0 3
0 0 0 0 4
( )( )
( )"
y h hh h h
( )! ! !
= − − − + − +1 32
33
34
2 3 4
!
y x y x hf x y( ) ( ) ( , )= +0 0 0
dydx
x y y= − − = −2 0 1 with ( ) .
y x y x hf x yx x h i
i i i i
i i
( ) ( ) ( , ), , , ,
+
+
= += + =
1
1 0 1 2where !
Solution.
From Equations (11.3) and (11.4) we can write:
Substituting x0 = 0 and y0 = -1 we get:
f f f f= − ′ = − ′ ′ = ′ ′ ′ = −1 3 3 3, , , , !
That is:
For small h we can neglect terms higher than h4 and hence given h one can calculate y(h). Setting x0= h and y0 = y(h) we can repeat the above steps to calculate y(2h) and so on.
11.3 Euler MethodIn the Euler method the first two terms of the Taylor series are used.
or
Euler method can be graphically described as shown in Fig.11.1. The slope at (x0, y0) isused to calculate y(x1). Using x1 and y(x1) calculate f(x1, y1) and from there calculate y(x2) and soon.
176
Figure 11.1 Euler Method.
y x y x hf x yh
f x y
i
i i i i i i( ) ( ) ( , )!
( , )
, , , ,
+ = + + ′
=
1
2
20 1 2 3
!
′ ≈−+ +f x y
f x y f x yhi i
i i i i( , )( , ) ( , )*
1 1
y y hf x yi i i i+ = +1* ( , )
( )y x y x hf x y
h f x y hf x y f x yhi i i i
i i i i i i( ) ( ) ( , ), ( , ) ( , )
++= + +
+ −1
21
2
11.4 Improved Euler Method
In the improved Euler method we use three terms of the Taylor series:
The derivative of f( xi, yi ) can be calculated using forward difference as follows:
where is calculated using Euler method as follows:yi+1*
Hence:
177
( )[ ]y x y xh
f x y f x y hf x y
y xh
k k
i i i i i i i i
i
( ) ( ) ( , ) , ( , )
( ) ( )
+ += + + +
= + +
1 1
1 2
2
2
Figure 11.2 Improved Euler Method.
( )k f x y
k f x h y hki i
i i
1
2 1
=
= + +
( , )
,
which can be placed in the form:
where
Procedure:5. Calculate k f x yi i1 = ( , )6. Calculate y y hki i
* = + 1
7. Calculate k f x h yi i2 = +( , )*
8. Calculate y yh
k ki i+ = + +1 1 22( )
The improved Euler method can be described graphically as shown Figure 11.2.
178
dydx
x y y= − − = −2 0 1 with ( ) .
From Fig.11.2 the average slope is used in Euler method to calculateff x y f x y
=+( , ) ( , )*
0 0 1 1
2the next value , which is the same approach derived in this section for the Improvedy y hfi i+ = +1
Euler method.
Example. Calculate y(0.1) and y(0.2) using h=0.1 for the following ODE
Solution. Using and the initial conditions wef x y x y( , ) = − −2 x y0 00 1= = −,follow the procedure described above.
1. Calculate k f1 0 1 2 0 1 1= − = − − − =( , ) ( ) ( )2. Calculate y y hk*
. * .= += − + = −
0 1
1 01 1 0 93. Calculate k f x y f2 0 01 01 0 9 0 7= + = − =( . , ) ( . , . ) .*
4. Calculate y y y k k1 0 1 201012
1012
1 0 7 0 915= = + + = − + + = −( . ).
( ).
( . ) .
Repeating the procedure to obtain y(0.2).1. Calculate k f1 01 0 915 0 715= − =( . , . ) .2. Calculate y y hk* . . ( . ) .= + = − + = −1 1 0 915 01 0 715 084353. Calculate k f x y f2 1 01 0 2 08435 0 4435= + = − =( . , ) ( . , . ) .*
4. Calculate
y y y k k2 1 20 2 01
012
0 915012
0 715 0 4435 0857075
= = + +
= − + + = −
( . ) ( . ).
( )
..
( . . ) .
Hence:
179
y x h y x a k a kk hf x yk hf x mh y mk
( ) ( )( , )( , )
+ ≈ + +== + +
1 1 2 2
1
2 1
where
(11.5)
y x h y x hy xh
y x( ) ( ) ( )!
( )+ = + ′ + ′ ′2
2(11.6)
( ) ( )
f x x y y f x y xf x y
xy
f x yy
xf x y
xx y
f x yx y
yf x y
y
( , ) ( , )( , ) ( , )
!( , ) ( , ) ( , )
+ + = + +
+ + +
+
∆ ∆ ∆ ∆
∆ ∆ ∆ ∆
∂∂
∂∂
∂∂
∂∂ ∂
∂∂
12
222
2
22
2
2
!
yyy
( )( . ) .( . ) .
0 101 0 9150 2 0857075
= −= −= −
11.6 Runge Kutta MethodsThe main reason for the development of alternative methods to the Taylor series approach
is to avoid the differentiations required by the series. Neither Euler nor Improved methods requirethat we differentiate any function. The Runge Kutta Methods are basically generalizations of theImproved Euler Method. For second order Runge-Kutta method the following is assumed:
The problem is now stated as follows: obtain the 3 unknowns such that y(x + h){ }a a m1 2, ,obtained using the above equation matches the Taylor Series expansion of y(x + h) truncated afterthe first derivative.
The Taylor Series expansion with the terms truncated after the second derivative is:
Since the derivatives in the Taylor series expansion are in terms of f(x,y) we need to expandas a series in terms of f(x,y). This calls for the use of Taylor series expansionf x mh y mk( , )+ + 1
in two variables which is given by:
Hence we can write:
180
[ ][ ]
y x h y x a hf x y a h f x y mhf x y mk f
y a hf a h f mhf mhff
y h a a f a mh f ff
x y
x y
x y
( ) ( ) ( , ) ( , ) ( , )
( ) ( )
+ = + + + +
= + + + +
= + + + +
1 2 1
1 2
1 2 22 (11.8)
′ =
′ ′ = = + = +
y x f
y xdf x y
dxfx
fy
f f ffx y
( )
( )( , ) ∂
∂∂∂
( )y x h y x hfh
f ffx y( ) ( )+ = + + +2
(11.9)
a aa m
1 2
2
11
+ ==
a a1 2
12
= =
k h f x y mhf x y mk f x yx y2 1= + + +[ ( , ) ( , ) ( , ) ]! (11.7)
Where f x yf x y
xf x y
f x yyx y( , )
( , )( , )
( , )= =
∂∂
∂∂
and
Replacing k1 with h f(x,y) and k2 with the right-hand side of Eq.(11.7) in Equation (11.5) we get:
Notice that we dropped the (x,y) just for compactness sake.
In the Taylor series expansion (Eq.11.6) replace:
Hence
Comparing (11.8) and (11.9) we get:
These are two equations in three unknowns and hence we have the choice to select one of thevariables. Choosing m=1, therefore
Hence a second order Runge-Kutta method is:
181
y x h y x ak bk ck dkk hf x yk hf x mh y mkk hf x nh y mkk hf x ph y pk
( ) ( )( , )( , )( , )( , )
+ ≈ + + + +== + += + += + +
1 2 3 4
1
2 1
3 2
4 3
y x h y x hy xh
y xh
y xh
y x( ) ( ) ( )!
( )!
( )!
( )( )+ = + ′ + ′ ′ + ′ ′ ′ +2 3 4
2 3 4iv
y x h y x k k( ) ( ) ( )+ = + +12 1 2
which turns out to be the Improved Euler Method. The Improved Euler method is actually aspecial case of the Runge-Kutta methods.
We will now tackle the fourth order Runge-Kutta method. In this case we assume theRunge-Kutta formulas are:
What is required is to find the coefficients such that the above formulas{ }a b c d m n p, , , , , ,duplicate the Taylor series through the term in h4. That is:
Since dydx
y x f x y= ′ =( ) ( , )
then ′ ′ = ′ = + = +
= +
∴ ′ ′ =
y x f xfx
fy
f f f f
F f f fy x F
x y
x y
( ) ( )
( )
∂∂
∂∂
Let
1
1
Similarly′′′ = + + + +
= + +∴ ′′′ = +
y x f ff f f f f ff
F f ff f fy x F f F
xx xy yy y x y
xx xy yy
y
( ) ( )
( )
2
2
2
22
2 1
Let
and
182
k hf1 =
k h f mhF m h F m h F2 12 2
23 3
3
12
16
= + + + +
!
{ }k h f nhF h n F mnf F
h n F m nf F mn f ff F
y
y xy yy
3 12 2
2 1
3 33
22
21
12
2
16
3 6
= + + + +
+ + + +
( )
( ) !
{ }k h f phF h p F npf F
h p F n pf F np f ff F mnpf F
y
y xy yy y
4 12 2
2 1
3 33
22
21
21
12
2
16
3 6 6
= + + + +
+ + + + +
( )
( ) !
y x f ff f f f f f f ff f f
f ff f ff f f ff
F f ff f f f f
y x F f F F f ff f F
xxx xxy xyy yyy y xx xy yy
x y xy yy y x y
xxx xxy xyy yyy
y xy yy y
(iv)
(iv)
Let
( ) ( )
( )( ) ( )
( ) ( )
= + + + + + +
+ + + + +
= + + +
∴ = + + + +
3 3 2
3
3 3
3
2 3 2
2
32 3
3 2 12
1
which allows the Taylor series to be written as:
[ ]y x h y x h F h F f F
h F f F f ff F f F
y
y xy yy y
( ) ( ) ( )
( )
+ = + + + +
+ + + + +
12
16
124
3
21
32 1
43 2 1
21 !
Turning now to the various k values, similar computations produce
Combining these equations as suggested by the Runge-Kutta formula,
183
a b c d cmn dnp
bm cn dp cmn dnp
bm cn dp cm n dn p
bm cn dp dmnp
+ + + = + =
+ + = + =
+ + = + =
+ + = =
116
12
18
13
112
14
124
2 2
2 2 2 2 2
3 3 3
m n p a d b c= = = = = = =12
116
13
y x h y x k k k k
k hf x y
k hf xh
y k
k hf xh
y k
k hf x h y k
( ) ( ) ( )
( , )
( , )
( , )
( , )
+ = + + + +
=
= + +
= + +
= + +
16
212
212
1 2 3 4
1
2 1
3 2
4 3
y x h y x a b c d hf bm cn dp h F
bm cn dp h F bm cn dp h F
cmn dnp h f F cm n dn p h f F
cmn dnp h f ff F dmnph f F
y y
xy yy y
( ) ( ) ( ) ( )
( ) ( )
( ) ( )
( ) ( )
+ = + + + + + + +
+ + + + + + +
+ + + +
+ + + + +
21
2 2 2 32
3 3 3 43
31
2 2 42
2 2 41
4 21
12
16
12
!
Comparison with the Taylor series results in the eight conditions
Since we have eight equations in seven unknowns we can select one of the unknowns andsolve for the rest. A classical solution set is
leading to the Runge-Kutta formulas
184
d ydt
dydt
y
y y
2
2 10
0 0 0 0
+ + =
= ′ =
.
( ) ( ) .given
d ydt
ad ydt
ad ydt
a y f t y
y y y
n
n
n
n
n
n n
nn
+ + + + =
= ′ = =
−
−
−
−
−
1
1
1 2
2
2
1 210 0 0
!
!
( , )
( ) , ( ) , ( )( )given α α α
11.7 Solving Higher Order ODE
An nth order ODE can be broken into n simultaneous first order ODE using a state variableapproach which will be explained through an example.
Example. Using h=0.1 determine y at t=0.1 and 0.2 for the following ODE:
Solve using (a) Euler. (b) Improved Euler. (c) Fourth order Runge-Kutta.
Solution. Let x yx y
1
2
== ′
′ = ′ =′ = ′′ = − − ′ = − −
x y xx y y y x x
1 2
2 1 210 10. .
or in matrix form,
ddt
xx x x
ddt
t
1
2 1 2
1010
=
− −
=
..
( , )or X
G X
X( )( )( )000
00
1
2 0
=
=
′
=
=
xx
yy
t
This approach applies to any order ODE as follows. For an nth order ODE
185
x yx yx y
x ynn
1
2 1
3
1
== ′= ′′
= −
"( )
′ =′ =′ =
′ = − − − −− −
x xx xx x
x f t x a x a x a xn n n n
1 2
2 3
3 4
1 1 1 2 1 1
"!( , )
To place in the state variable form let
We can therefore write:
The coefficients of the ODE can be either constants or functions of t and/or y.
(a) Euler
ProcedureAt t= ti
1. Calculate K G X= ( , )ti i
2. Calculate X X G Xi i i ih t+ = +1 ( , )
Hence at t=0
1 100
1 0 001
2
1 2 0
. .K =− −
=
− −
=
=
xx x
t
200
0101
001
0101
01 0 0
1 01
2. .
.( . )( . )
( . ) .
X X K= + =
+
=
← =← = ′
∴ =
hx yx y
y
At t=0.1
186
1 101
1 0 01010 9
2
1 2 0 1
..
...
.
K =− −
=
− −
=
=
xx x
t
20
0101
010 9
0 010 9
0 20 2
0 2 0 01
2 11
2.
..
..
..
( . )( . )
( . ) .
X X K= + =
+
=
← =← = ′
∴ =
hx yx y
y
(b) Improved Euler Method
ProcedureAt t= ti
1. Calculate K G X1 = ( , )ti i
2. Calculate XEST X K= +i h 1
3. Calculate K G XEST2 = +( , )ti 1
4. Calculate [ ]X X K Ki i
h+ = + +1 1 22
Hence at t=0
1 10
1 0 0011 0
2
1 2 0
. ( , )K G X0= =− −
=
− −
=
=
txx x
t
200 01
01
011
1
2. .XEST X0= + =
+
=
←←
hxxΚ
30
1 0 0010 92
0 1
. ( , )..
.
K G t t XEST0= = + =− −
=
h#$%
( )42
00
012
01
010 9
0 0050 095
01 0 005
0 1 21
2.
. ..
.
.
( . ) .
X X K Ki
h xx
y
= + + =
+
+
=
←←
∴ =
At t=0.1
187
1 010 0050 095
0 0951 0 005 0 095
0 0950 91 1 1. ( , ) ( . ,
.
.)
.. .
..
K G X G= =
=
− −
=
t
20 0050 095 01
0 0950 9
0 014501851 1.
.
. ...
..XEST X K= + =
+
=
h
&3 0 20 01450185
01851 0 0145 0185
018508005
2 10 2
. ( , ) ( . ,..
.. .
..
.
K G XEST G= + =
=
− −
=
t h
( )42
0 0050 095
012
0 0950 9
018508005
0190180025
0 2 019
2 1 1 2...
. ..
..
..( . ) .
X X K K= + + =
+
+
=
∴ =
h
y
(c) Runge-Kutta 4th order Method
ProcedureAt t= ti
1. Calculate K G X1 = ( , )ti i
2. Calculate XEST X K= +i
h2 1
3. Calculate K G XEST2 2= +( , )t
hi
4. Calculate XEST X K= +i
h2 2
5. Calculate K G XEST3 2= +( , )t
hi
188
d ydt
tydydt
e y y e
y y
t t2
22 24
0 05 0 10
+ + =
= ′ =
− −
( ) . ( ) .
d ydt
dydt
y y y2
2 0 0 0 0 10+ + = = ′ =. , ( ) ( ) .
y t e t t( ) cos( ) sin( ).= +
−0 5 3
23
32
6. Calculate XEST X K= +i h 3
7. Calculate K G XEST4 = +( , )t hi
8. Calculate [ ]X X K K K K2i i
h+ = + + + +1 1 3 46
2 2
The calculations follow a similar procedure to the Improved Euler method and are left tothe reader as an exercise.
Exercise. Use the Runge-Kutta method to solve the following ODE for the points y(0.1),y(0.2) and y(0.3). Use h=0.1.
Now we return back to program development. The following is a C++ program for solvingODE using either Euler or Fourth order Runge-Kutta method. To verify that the program works itis tested against an ODE with known analytical solution. The ODE used is:
Its analytical solution is given by:
#include <iostream.h>#include <iomanip.h> //input/output manipulation for formatting using setw, etc.#include <conio.h>#include <math.h>
class ODE {
189
// The default member classification in classes is private.//Private variables double *x; //state space vector x[0],x[1],...
//The k's in the Runge-Kutta method. // each k is a vector of dimension = order of ODE double *k1; double *k2; double *k3; double *k4; double *xest; void(*G)(double tp,double *xp,double *xdot); //function for calculating the k's double t; //independent variable int neqns; //number of equations=order of ODE double h; //step length
public: ODE(void (*GP)(double, double *,double *), int n) { G=GP; neqns=n; x=new double[neqns]; k1=new double[neqns]; k2=new double[neqns]; k3=new double[neqns]; k4=new double[neqns]; xest=new double[neqns]; }
~ODE() { delete []x; delete []k1; delete []k2; delete []k3; delete []k4; delete []xest; }
void Init(double *xp, double tp, double dt);
double get_t() {
190
return t; }
double* getx() { return x; }
void Euler(); void RK4(); // 4th order Runge-Kutta };
void ODE::Init(double *xp, double tp, double dt) { for(int i=0; i<neqns; i++) x[i]=xp[i]; t=tp; h=dt; }
void ODE::Euler() { (*G)(t,x,k1);
for(int i=0; i<neqns; i++) x[i]=x[i]+h*k1[i]; t=t+h; }
//Fourth order Runge-Kuttavoid ODE::RK4() { int i;
(*G)(t,x,k1);
for(i=0; i<neqns; i++) xest[i]=x[i]+0.5*h*k1[i];
(*G)(t+0.5*h,xest,k2);
for(i=0; i<neqns; i++) xest[i]=x[i]+ 0.5*h*k2[i];
191
(*G)(t+0.5*h,xest,k3);
for(i=0; i<neqns; i++) xest[i]=x[i]+h*k3[i];
(*G)(t+h,xest,k4);
for(i=0; i<neqns; i++) x[i]=x[i]+ h*(k1[i]+2.0*k2[i]+2.0*k3[i]+k4[i])/6.0;
t=t+h; }
//User supplied routine// ODE y''(t)+y'(t)+y = 0, intitial conditions y'(0)=y(0)=1// Analytical solution y=exp(-0.5t)*(cos(sqrt(3)*t/2)+sqrt(3)*sin(sqrt(3)*t/2)) void Vector( double t, double *x, double *xdot) { xdot[0]=x[1]; xdot[1]=0.0-x[0]-x[1]; }
//---------------------------------------------------------------------------
int main(){ODE eqn(Vector, 2); //Replace (*G) with Vector and set neqns=2double *x, t, h, yp[10], tp[10];
x=new double[2];//initial conditionsx[0]=1.0;x[1]=1.0;t=0.0;h=0.1;
eqn.Init(x,t,h); //set initial conditionstp[0]=0.0;yp[0]=x[0];
int i;for(i=1; i<10; i++) {
192
eqn.RK4(); x=eqn.getx();
yp[i]=x[0]; tp[i]=eqn.get_t(); }
// print solutioncout << "From Runge Kutta" << " " << "t" << " " << "From Analytical solution" << endl;for(i=0; i<10; i++) { cout << setprecision(7) << setw(10) << yp[i] << setprecision(3) << setw(10) << tp[i] << setprecision(7) << setw(20) << (exp(-0.5*tp[i])*(cos(0.866*tp[i])+1.732*sin(0.866*tp[i])))<< endl; }
getch(); return 1; }
Print-outFrom Runge Kutta t From Analytical solution 1 0 1 1.090171 0.1 1.090163 1.161395 0.2 1.16138 1.214798 0.3 1.214778 1.251569 0.4 1.251544 1.27294 0.5 1.272912 1.280174 0.6 1.280143 1.274547 0.7 1.274516 1.257337 0.8 1.257305 1.22981 0.9 1.229778
Problems1. Solve the following ODE’s in 0 # t # 5 using the improved Euler method with h=0.1 (write
your own program)
a.d ydt
y y y2
2 8 0 0 1 0 0+ = = =, ( ) , ' ( )
193
dPdt
P P= −0 3 0 0000007 2. .
Figure 11.3 Problem 3.
b.d ydt
dydt
y t y y2
2
2
0 01 2 0 0 0 1−
+ = = =. sin( ), ( ) , ' ( )
c. ( ) , ( ) , ' ( )e yd ydt
t y yt + = = =2
2 0 1 0 0
2. The number of bacterial cells (P) in a given reactor is related to time in days (t) as describedby the following mathematical model
If at time t=0, P=106 determine the number of cells when t=30 days. Use the fourth-orderRunge-Kutta method and a time increment of 1 day.
3. When a vehicle travels over a rough road, a vertical displacement will result. Consider thefollowing highly idealized model:
Suppose that a manufacturer is interested in determining the largest verticaldisplacement x for a vehicle traveling at a velocity of 55 miles per hour over a sinusoidalsurface, given
194
md xdt
cdxdt
kx m t2
22+ + = ϖ ϖ∆ sin
4 3 16 02
2
d xdt
dxdt
x+ + =
m=10 lb-s2/in.k=1210 lb/in.c=88 lb-s/in.L=50 ft.)=1 in.
The mathematical model describing this behavior is given by:
where where v is the velocity of the vehicle. Determine the maximumϖ π= 2 v Ldisplacement x using the Runge-Kutta 4th order method and )t=0.01 s. Assume that thevehicle was initially at rest ( ).x x( ) '( )0 0 0= =
4. Use Taylor series to solve the following second-order ordinary differential equation at t=0.1:
with the initial conditions .x x( ) , '( )0 1 0 1= =