Numerical Analysis
Simultaneous or Coupled
First Order ODEs
Systems of Ordinary Differential Eqs
In these two lecture we shall consider only first order
ODEs systems with initial conditions.
We shall develop numerical methods for solution of
Systems of Ordinary differential equations
Euler method
Improved Euler Method
Runge Kutta fourth order
Finally we shall talk about Stiff Equations as well.
Simultanous Differential Equations
Introduction
These equations occur in many situations in practice.
Simple examples can be for oscillating masses and
coupled electrical circuits.
We shall learn how to solve such equations
numerically.
Example of an Electrical Network Model
L = 1 henry
Switch
I1
t=0
I1
C = 0.25 farad
I2
I2
R1 =4 ohms
V = 12 Volts
R2 =4 ohms
This has two coupled loops. The Kirchoffs law for voltage
balance can yield the model equations:
Models for Coupled Electrical circuits
For the left loop, the voltage drops across each element of the circuit yields:
Voltage from the source = voltage drop across L + Voltage drop across R1
12 1 dI1 / dt 4( I1 I 2 )
or
dI1
4 I1 4 I 2 12
dt
Where I1 is the current in first loop and I2 is the current in the second
loop. For the next loop again the voltage balance gives:
0 6 I 2 4( I 2 I1 ) 4 I 2 dt
When we differentiate it we get the following balance equation:
dI 2
dI1
0 10
4
4I 2
dt
dt
dI 2
dI1
0.4
0.4 I 2 0
dt
dt
Models for Coupled Electrical circuits
L = 1 henry
dI1
4 I1 4 I 2 12
dt
dI 2
dI1
0.4
0.4 I 2 0
dt
dt
Switch
I1
t=0
I1
C = 0.25 farad
I2
R1 =4 ohms
V = 12 Volts
R2 =4 ohms
Initial conditions are : I1(0) = 0.0;
I2(0) =0.0
This model is a linear first order non-Homogeneous.
It ha coupled ODE Based equations
I2
Example of Models for mixing of salt and two tanks
The tank T1 has initially 100 liters of
T1
pure water. The tank T2 has 100 liters
of water in which 150 kg of salt is
mixed.
The liquid circulates through tanks at a
constant rate of 2 liter/minute and the
mixture is kept uniform by stirring.
Let us ask this question.
What is amount of salt y1(t) and y2(t) in T1 and T2 tanks respectively.
T2
Example of Models for mixing of salt and two tanks
As for a single tank the time rate of change of
y1(t) (amount of salt in tank-1) is equal to the
inflow minus the outflow.
T1
This is also true for Tank-2:
2
2
y2
y1 (T1 )
100
100
2
2
y 2 inflow/min - outflow/min
y1
y2 (T2 )
100
100
Hence, mathematical model is a system of first order ODEs:
y 1 inflow/min - outflow/min
y 1 0.02 y1 0.02 y1 (T1 )
y 2 0.02 y1 0.02 y2 (T2 )
There are two initial conditions: y1(0) = 0 (no salt in Tank-1); y2(0) = 150.
T2
Simultaneous ODEs
We have already gone through various methods to solve numerically
a single ODE such as dy/dt = f(t, y).
The methods can be modified easily to solve coupled (say two)
ODEs. For example, we have two first order simultaneous Ordinary
Differential Equations with initial conditions.
dx
f t , x, y ,
dx
dy
g t , x, y ,
dx
x t0 x0 , y t0 y0 , t t0
We shall also see how our solutions as successive estimates of x and y
compare with values obtained by analytical methods.
Simultaneous ODEs
The methods used for a single ODE were:
Euler method
Improved Euler
Runge Kutta, order 2
Runge Kutta, order 4
Now each of these will be used for two simultaneus first order
ODEs. The general form will be
dx
f t , x, y ,
dx
dy
g t , x, y ,
dx
x t0 x0 , y t0 y0 , t t0
Eulers Method for Simultaneous ODEs
Consider two first order simultaneous Ordinary Differential
Equations with initial conditions t time t = t0 :
dx
f t , x, y ,
dx
dy
g t , x, y ,
dx
x t0 x0 , y t0 y0 , t t0
h xi 1 xi
h yi 1 yi
Then successive estimates of x nd y can be obtained using following:
x1 x0 h f t0 , x0 , y0
y1 y0 h g t0 , x0 , y0
x3 x2 h f t 2 , x2 , y2
y3 y 2 h g t 2 , x2 , y 2
x2 x1 h f t1 , x1 , y1
xn 1 xn h f t n , xn , yn
y2 y1 h g t1 , x1 , y1
yn 1 yn h g t n , xn , yn
Example-1 on System of First Order ODEs
Consider two first order simultaneous ODEs and try to solve by Euler
method. Let us find answers of x and y at t=0.1, 0.2 and 0.3.
dx / dt f (t , x, y ) t x y,
dy / dt g (t , x, y ) 2t 3 x y,
x ( 0) 1 , y ( 0) 2
Solution:
t0 0
x0 1,
y0 2 ,
f (t , x, y ) t x y,
h 0.1;
f (t0 , x0 , y0 ) 0 1 2 3,
g (t , x, y ) 2t 3 x y, g (t0 , x0 , y0 ) 0 3 2 1,
Example 1:
1st point:
t1 t 0 h 0 0.1 0.1
x1 x0 h f t 0 , x0 , y0 1 (0.1)(3) 1.3
y1 y0 h g t 0 , x0 , y0 2 (0.1)(1) 2.1
2nd point:
t 2 t1 h 0.1 0.1 0.2
x2 x1 h f t1 , x1 , y1 1.3 (0.1)(0.1 1.3 2.1) 1.63
y2 y1 h g t1 , x1 , y1 2.1 (0.1)(2 0.1 3 1.3 2.1) 2.3
3rd point:
t3 t 2 h 0.2 0.1 0.3
x3 x2 h f t 2 , x2 , y2 1.63 (0.1)(0.2 1.63 2.3) 2.003
y3 y2 h g t 2 , x2 , y2 2.3 (0.1)(2 0.2 3 1.63 2.3) 2.599
A computer program
% It Uses Euler method to solve Simultaneous first order
odes:
%
dx/dt = -t + x + y ; x(0) = 1.0
%
dy/dt = 2*t + 3*x y ; y(0) = 2.0
%
Input: x0 = initial guess; h = step size (h = 0.1)
%
n = number of iterations; default: n = 10;
%
Output: estimate of x and y;
n = 10; t0 = 0.0; x0=1.0; y0 = 2.0; h = 0.1;
t = t0; x = x0;
y = y0;
% Initial Guess
fprintf('
time
x-est
y-est
\n');
for k=1:n
f = -t + x + y ;
g = 2*t + 3*x - y ;
t = t + h; x = x + h*f ; y = y + h*g ;
fprintf('%6.2f %12.6f
%12.6f \n', t, x, y );
end
Example 1 :
These results are approximate values of x and y as a function of
time and error is substantially large
time
0.10
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
1.00
>>
x-est
1.300000
1.630000
2.003000
2.433200
2.936520
3.531168
4.238277
5.082632
6.093519
7.305711
y-est
2.100000
2.300000
2.599000
3.000000
3.509960
4.139920
4.905278
5.826234
6.928400
8.243616
Initial Value Problems
When initial conditions are given and we need to solve numerically any set of
ODEs, the problem is termed as Initial Value Problem.
For example,
consider a set of first order ODEs about velocity and acceleration. The initial
conditions are also given with them.
x(t 0) x0
v(t 0) v0
dx
v f (t , x, v)
dt
dv
k
c
v x g (t , x, v)
dt
m
m
Now assume,
c=1
m=1
k =1
Example on Initial Value Problems
We can apply single step Euler method as following:
dx / dt f (t , x, y ) v, x(0) 0 , v(0) 1
dv / dt g (t , x, y ) x v,
t0 0
x0 0,
v0 1,
h 0.1;
f (t , x, v) v,
f (t0 , x0 , v0 ) 1
g (t , x, v) x v,
g (t0 , x0 , v0 ) 0 1 1,
x1 x0 h f t0 , x0 , y0 0 (0.1)(1) 0.1
v1 v0 h g t0 , x0 , y0 1 (0.1)(1) 0.9
t1 t0 h 0 0.1 0.1
This can be further continued for t = 0.2, 0.3 and so on.
Example on Initial Value Problems
Now Consider another example on simple second order differential
equation for a vibrating spring mass system.
d 2x k
d 2x
x
4x 0
2
2
dt
m
dt
The initial conditions are
x(0) =0.2,
x(0) =0
and t = 0.02.
The Exact solution = 0.2 cos(2t)
Then the above equation can be decomposed as two first order equations:
dx
v f (t , x, v)
dt
dv
g (t , x, v) 4 x
dt
The boundary conditions are ,
x(0) = 0.2 and
v(0) = 0.
Example on Initial Value Problems
Here we display the large errors of the Euler method.
time
0.10
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
x-est
0.200000
0.192000
0.176000
0.152320
0.121600
0.084787
0.043110
-0.001958
-0.048751
y-est
-0.080000
-0.160000
-0.236800
-0.307200
-0.368128
-0.416768
-0.450683
-0.467927
-0.467144
exact-x
0.196013
0.184212
0.165067
0.139341
0.108060
0.072472
0.033993
-0.005840
-0.045440
error (%)
2.033884
4.227625
6.623292
9.314291
12.529596
16.993771
26.819805
66.473972
7.284645
1.00
-0.095465
-0.447644
-0.083229
14.701077
Example on Initial Value Problems
The example
Euler Example
xi 1 xi t vi
0.5
vi 1 vi t 4vi
0.4
0.3
actual value
There is problem with
the step sizes (t)
causing an instability.
When it is sufficiently
small, the answer
becomes closer to the
actual.
Displacement
0.2
0.1
0
-0.1
0.5
-0.2
-0.3
-0.4
-0.5
Time (t)
1.5
Use of Improved Euler Method
The errors introduced by the use of Euler method can be reduced
by the IMPROVED EULER METHOD
x f t , x, y ,
y g t , x, y ,
x t0 x0 , y t0 y0 , t t0
h xi 1 xi
h yi 1 yi
Then successive estimates of x nd y can be obtained using following:
.
x1p x0 h f t 0 , x0 , y0 ;
y1p y0 h g t 0 , x0 , y0
h / 2 g t , x , y g t , x , y
h f t , x , y ;
y y h g t , x
h / 2 f t , x , y f t , x , y
h / 2 g t , x , y g t , x , y
x1c x0 h / 2 f t 0 , x0 , y0 f t 0 , x 1p , y 1p
y1c y0
x 2p x 1c
x2c x 1c
y 2c y 1c
c
1
c
1
p
2
p
2
p
2
p
2
c
,
y
1
1
Use of Improved Euler Method
The improved Euler method is
also called Predicted
Corrector method.
y
True
value
In this method we first
estimate both x1 and y1 as
predicted values
Slope f(x0, y0)
Then Using these values we
can find accurate or
corrected value as a mean
or average of two slopes for
both x and y.
Slope f(x0, y0)
y1
y0
h
xo
x1
Use of Improved Euler Method
The (n+1)th step will be calculated using predictor and corrector as
h xi 1 xi
xnp1 xnc h f t n , x cn , ync
c
n 1
h
x f (t n , xnc , ync ) f t n , xnp1 , ynp1
2
c
n
ynp1 ync h g xn , ync
c
n 1
h
y g (t n , xnc , ync ) g t n , xnp1 , ynp1
2
c
n
Example 2: Use of Improved Euler
Method
dx / dt f (t , x, y ) t x y,
dy / dt g (t , x, y ) 2t 3 x y,
x ( 0) 1
y ( 0) 2
1st step:
t1 t0 h 0 0.1 0.1
x1p x0 f t0 , x0 , y0 h 1.0 (0 1 2) 0.1 1.3
y1p y0 g t0 , x0 , y0 h 2.0 (2 0 3 1 2) 0.1 2.1
f t1 , x1p , y1p 0.1 1.3 2.1 3.3
g t1 , x1p , y1p 2 0.1 3 1.3 2.1 2.0
x1c 1.0 (0.1 / 2)(3. 3.3) 1.315
y1c 2.0 (0.1 / 2)(1. 2.) 2.15
Example 2: Use of Improved Euler
Method
dx / dt f (t , x, y ) t x y,
dy / dt g (t , x, y ) 2t 3 x y,
x ( 0) 1
y ( 0) 2
2nd step:
t 2 t1 h 0.1 0.1 0.2
x2p 1.315 (0.1 1.315 2.15) 0.1 1.315 0.3365 1.6515
y2p 2.15 (2 0.1 3 1.315 2.15) 0.1 2.15 0.1995 2.3495
g t , x
2 0.2 3 1.6415 2.3695 3.005
f t 2 , x2p , y2p 0.2 1.6415 2.3695 3.801
2
p
2
, y2p
x2c 1.315 (0.1 / 2)(3.365 3.801) 1.6733
y2c 2.15 (0.1 / 2)(1.995 3.005) 2.4000
A computer program
% It Uses Improved Euler method to solve two Simultaneous first order odes:
%
dx/dt = -t + x + y ; x(0) = 1.0
%
dy/dt = 2*t + 3*x y ; y(0) = 2.0
% Input: x0 = initial guess; h = step size (h = 0.1)
%
n = number of iterations; default: n = 10;
% Output: estimate of x and y;
n = 10; t0 = 0.0; x0=1.0; y0 = 2.0; h = 0.1;
t = t0; x_c = x0; y_c = y0; % Initial Guess
fprintf(' time
x-p
x_c
y_p
y_c
\n');
for k=1:n
fa = ( -t + x_c + y_c );
ga = ( 2.*t + 3.*x_c - y_c );
x_p = x_c + h*fa;
y_p = y_c + h*ga;
fb = ( -(t+h) + x_p + y_p);
gb = ( 2.*(t+h) + 3.*x_p - y_p );
x_c = x_c + (h/2.)*(fa + fb);
y_c = y_c + (h/2.)*(ga + gb);
fprintf('%6.2f %12.5f %12.5f %12.5f %12.5f \n', t, x_p, x_c, y_p, y_c );
t = t + h;
end
Results for Example 2
time
0.00
0.10
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
>>
x_p
1.30000
1.65150
2.06063
2.54460
3.12361
3.82160
4.66726
5.69511
6.94694
8.47338
x_c
1.31500
1.67330
2.09010
2.58291
3.17227
3.88255
4.74291
5.78847
7.06171
8.61412
y_p
2.10000
2.34950
2.70199
3.16652
3.75658
4.49064
5.39287
6.49411
7.83312
9.45805
y_c
2.15000
2.40000
2.75499
3.22412
3.82106
4.56456
5.47916
6.59620
7.95504
9.60465
Example 2:
A comparison table for estimated values for Euler and Improved
Euler method
Time
Euler Method
Improved Euler
(t)
x(t)
y(t)
x(t)
y(t)
0.1
1.30
2.1
1.3150
2.150
0.2
1.63
2.3
1.6733
2.400
0.3
2.0030
2.599
2.0901
2.755
0.4
2.4332
3.00
2.5829
3.2241
0.5
2.9365
3.51
3.1723
3.8211