coupled 1st order odes

28
Simultaneous or Coupled First Order ODEs Numerical Analysis Numerical Analysis

Upload: muhammad-ahtisham-asif

Post on 11-Feb-2016

47 views

Category:

Documents


0 download

DESCRIPTION

Coupled 1st Order ODEs

TRANSCRIPT

Page 1: Coupled 1st Order ODEs

Simultaneous or Coupled First Order ODEs

Numerical AnalysisNumerical Analysis

Page 2: Coupled 1st 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.

Page 3: Coupled 1st Order ODEs

Simultanous Differential EquationsIntroduction• 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.

Page 4: Coupled 1st Order ODEs

Example of an Electrical Network ModelC = 0.25 farad

R1 =4 ohms

L = 1 henry

V = 12 Volts

R2 =4 ohms

Switch

t = 0I2I1 I2

I1

This has two coupled loops. The Kirchoff’s law for voltage balance can yield the model equations:

Page 5: Coupled 1st Order ODEs

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

Models for Coupled Electrical circuits

)(4/1 21 211 IIdtdI

or 1244 211 IIdtdI

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:

dtIIII 2122 4)(46 0

When we differentiate it we get the following balance equation:

212 4410 0 IdtdI

dtdI

04.04.0 212 IdtdI

dtdI

Page 6: Coupled 1st Order ODEs

This model is a linear first order non-Homogeneous. It ha coupled ODE Based equations

Initial conditions are : I1(0) = 0.0; I2(0) =0.0

1244 211 IIdtdI

04.04.0 212 IdtdI

dtdI

C = 0.25 farad

R1 =4 ohms

L = 1 henry

V = 12 Volts

R2 =4 ohms

Switch

t = 0I2I1 I2

I1

Models for Coupled Electrical circuits

Page 7: Coupled 1st Order ODEs

Example of Models for mixing of salt and two tanks

The tank T1 has initially 100 liters of

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.

T1T2

Page 8: Coupled 1st Order ODEs

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.

This is also true for Tank-2:

Hence, mathematical model is a system of first order ODEs:

)(100

2100

2noutflow/mi-inflow/min

)(100

2100

2noutflow/mi-inflow/min

2212

1121

Tyyy

Tyyy

)(02.002.0)(02.002.0

2212

1111

TyyyTyyy

There are two initial conditions: y1(0) = 0 (no salt in Tank-1); y2(0) = 150.

T1 T2

Example of Models for mixing of salt and two tanks

Page 9: Coupled 1st Order ODEs

Simultaneous ODEs

,,,

,,,,, 00000

yxtgdxdy

ttytyxtxyxtfdxdx

We shall also see how our solutions as successive estimates of x and y compare with values obtained by analytical methods.

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.

Page 10: Coupled 1st Order ODEs

Simultaneous ODEs

,,,

,,,,, 00000

yxtgdxdy

ttytyxtxyxtfdxdx

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

Page 11: Coupled 1st Order ODEs

Euler’s Method for Simultaneous ODEs

nnnnn yxtghyy

yxtghyyyxtghyyyxtghyy

,,

,,,,,,

1

22223

11112

00001

,,,

,,,,, 00000

yxtgdxdy

ttytyxtxyxtfdxdx

nnnnn yxtfhxx

yxtfhxxyxtfhxxyxtfhxx

,,

,,,,,,

1

22223

11112

00001

Consider two first order simultaneous Ordinary Differential Equations with initial conditions t time t = t0 :

Then successive estimates of x nd y can be obtained using following:

ii

ii

yyhxxh

1

1

Page 12: Coupled 1st 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.

Example-1 on System of First Order ODEs

,32),,(/2)0(,1)0(,),,(/

yxtyxtgdtdyyxyxtyxtfdtdx

Solution:

,1230),,(,32),,(,3210),,(,),,(

;1.0,2,10

000

000

00

0

yxtgyxtyxtgyxtfyxtyxtf

hyxt

Page 13: Coupled 1st Order ODEs

Example 1:

1.2)1)(1.0(2,,

3.1)3)(1.0(1,,1.01.00

00001

00001

01

yxtghyyyxtfhxx

htt1st point:

3.2)1.23.131.02)(1.0(1.2,,

63.1)1.23.11.0)(1.0(3.1,,2.01.01.0

11112

11112

12

yxtghyyyxtfhxx

htt2nd point:

3rd point:

599.2)3.263.132.02)(1.0(3.2,,

003.2)3.263.12.0)(1.0(63.1,,3.01.02.0

22223

22223

23

yxtghyyyxtfhxx

htt

Page 14: Coupled 1st Order ODEs

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

Page 15: Coupled 1st Order ODEs

Example 1 :

time x-est y-est 0.10 1.300000 2.100000 0.20 1.630000 2.300000 0.30 2.003000 2.599000 0.40 2.433200 3.000000 0.50 2.936520 3.509960 0.60 3.531168 4.139920 0.70 4.238277 4.905278 0.80 5.082632 5.826234 0.90 6.093519 6.928400 1.00 7.305711 8.243616>>

These results are approximate values of x and y as a function of time and error is substantially large

Page 16: Coupled 1st Order ODEs

Initial Value Problems

0

0

)0()0(vtvxtx

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.

),,(

),,(

vxtgxmkv

mc

dtdv

vxtfvdtdx

Now assume,

c = 1

m = 1

k = 1

Page 17: Coupled 1st Order ODEs

Example on Initial Value Problems

,),,(/1)0(,0)0(,),,(/

vxyxtgdtdvvxvyxtfdtdx

,110),,(,),,(1),,(,),,(

;1.0,1,00

000

000

00

0

vxtgvxvxtgvxtfvvxtf

hvxt

1.01.009.0)1)(1.0(1,,

1.0)1)(1.0(0,,

01

00001

00001

httyxtghvvyxtfhxx

We can apply single step Euler method as following:

This can be further continued for t = 0.2, 0.3 and so on.

Page 18: Coupled 1st Order ODEs

Example on Initial Value Problems

042

2

2

2

xdtxdx

mk

dtxd

Now Consider another example on simple second order differential equation for a vibrating spring mass system.

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:

xvxtgdtdv

vxtfvdtdx

4),,(

),,(

The boundary conditions are , x(0) = 0.2 and v(0) = 0.

Page 19: Coupled 1st Order ODEs

Example on Initial Value ProblemsHere we display the large errors of the Euler method.

time x-est y-est exact-x error (%) 0.10 0.200000 -0.080000 0.196013 2.033884 0.20 0.192000 -0.160000 0.184212 4.227625 0.30 0.176000 -0.236800 0.165067 6.623292 0.40 0.152320 -0.307200 0.139341 9.314291 0.50 0.121600 -0.368128 0.108060 12.529596 0.60 0.084787 -0.416768 0.072472 16.993771 0.70 0.043110 -0.450683 0.033993 26.819805 0.80 -0.001958 -0.467927 -0.005840 66.473972 0.90 -0.048751 -0.467144 -0.045440 7.284645 1.00 -0.095465 -0.447644 -0.083229 14.701077

Page 20: Coupled 1st Order ODEs

Example on Initial Value Problems• The example

• There is problem with the step sizes (Δt) causing an instability.

• When it is sufficiently small, the answer becomes closer to the actual.

Euler Example

-0.5

-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

0.5

0 0.5 1 1.5 2

Time (t)

Disp

lace

men

t

xvactual value

iii

iii

vtvvvtxx

41

1

Page 21: Coupled 1st Order ODEs

Use of Improved Euler Method

.

The errors introduced by the use of Euler method can be reduced by the IMPROVED EULER METHOD

,,,

,,,,, 00000

yxtgyttytyxtxyxtfx

ppcccc

ppcccc

cccpcccp

ppc

ppc

pp

yxtgyxtghyy

yxtfyxtfhxx

yxtghyyyxtfhxx

yxtgyxtghyy

yxtfyxtfhxx

yxtghyyyxtfhxx

221112

221112

11

000001

000001

0000100001

,,,,2/

,,,,2/

,,;,,

,,,,2/

,,,,2/

,,;,,

11

11

11121112

11

11

Then successive estimates of x nd y can be obtained using following:

ii

ii

yyhxxh

1

1

Page 22: Coupled 1st Order ODEs

Use of Improved Euler Method

.

The improved Euler method is also called Predicted – Corrector method. In this method we first estimate both x1 and y1 as predicted values Then Using these values we can find accurate or corrected value as a mean or average of two slopes for both x and y.

x

y

y0

True value

xo x1

y1

h

Slope f(x0, y0)

Slope f(x0, y0)

Page 23: Coupled 1st Order ODEs

Use of Improved Euler Method

ii xxh 1

cncn

cn

pn yxtfhxx

n,,1

pn

pnn

cn

cnn

cn

cn yxtfyxtfhxx 111 ,,),,(

2

cnncn

pn yxghyy ,1

pn

pnn

cn

cnn

cn

cn yxtgyxtghyy 111 ,,),,(

2

The (n+1)th step will be calculated using predictor and corrector as

Page 24: Coupled 1st Order ODEs

Example 2: Use of Improved Euler Method

15.2.)2.1)(2/1.0(0.2

315.1)3.3.3)(2/1.0(0.1

0.21.23.131.02,,

3.31.23.11.0,,

1.21.0)21302(0.2,,

3.11.0)210(0.1,,

1.01.00

1

1

111

111

00001

00001

01

c

c

pp

pp

p

p

y

x

yxtg

yxtf

hyxtgyy

hyxtfxx

htt

2)0(,32),,(/1)0(,),,(/

yyxtyxtgdtdyxyxtyxtfdtdx

1st step:

Page 25: Coupled 1st Order ODEs

Example 2: Use of Improved Euler Method

4000.2)005.3995.1)(2/1.0(15.2

6733.1)801.3365.3)(2/1.0(315.1

005.33695.26415.132.02,,

801.33695.26415.12.0,,

3495.21995.015.21.0)15.2315.131.02(15.2

6515.13365.0315.11.0)15.2315.11.0(315.1

2.01.01.0

2

2

222

222

2

2

12

c

c

pp

pp

p

p

y

x

yxtg

yxtf

y

x

htt

2)0(,32),,(/1)0(,),,(/

yyxtyxtgdtdyxyxtyxtfdtdx

2nd step:

Page 26: Coupled 1st Order ODEs

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

Page 27: Coupled 1st Order ODEs

Results for Example 2 time x_p x_c y_p y_c 0.00 1.30000 1.31500 2.10000 2.15000 0.10 1.65150 1.67330 2.34950 2.40000 0.20 2.06063 2.09010 2.70199 2.75499 0.30 2.54460 2.58291 3.16652 3.22412 0.40 3.12361 3.17227 3.75658 3.82106 0.50 3.82160 3.88255 4.49064 4.56456 0.60 4.66726 4.74291 5.39287 5.47916 0.70 5.69511 5.78847 6.49411 6.59620 0.80 6.94694 7.06171 7.83312 7.95504 0.90 8.47338 8.61412 9.45805 9.60465 >>

Page 28: Coupled 1st Order ODEs

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.1500.2 1.63 2.3 1.6733 2.4000.3 2.0030 2.599 2.0901 2.7550.4 2.4332 3.00 2.5829 3.22410.5 2.9365 3.51 3.1723 3.8211