KTH, DN2221, Computer Lab 1

17

description

Applied Numerical Methods, KTH course DN2221, Solution to Computer Lab 1 ( ODE-Systems of LCC-type and Stability ), this report achieved full pointsthis is the solution to the corresponding computer lab 1 in Lennart Edsberg: Introduction to Computation and Modelling for Differential Equations

Transcript of KTH, DN2221, Computer Lab 1

Page 1: KTH, DN2221, Computer Lab 1

Applied Numerical Methods

KTH DN2221

Computer Lab 1

ODE-Systems of LCC-type and Stability

Sebastian Arnoldt

12-2012

Page 2: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 1

Contents

1 Part 1 - Solution of ODE-systems with constant coe�cients 2

2 Part 2 - Stability of ODE-systems 4

2.1 Part 2a - Stability of the solutions of an ODE-system of LCC-

type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2.2 Part 2b - Stability of critical points of a non-linear ODE-system 7

3 Appendix 9

3.1 A1 - MATLAB code for part 1 . . . . . . . . . . . . . . . . . . 9

3.2 A.2a1 - MATLAB code for part 2a - estimating K . . . . . . . 11

3.3 A.2a1 - MATLAB code for part 2a - Root Locus . . . . . . . . 12

3.4 A.2b - MATLAB code for part 2b . . . . . . . . . . . . . . . . 14

4 Bibliography 16

Page 3: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 2

1 Part 1 - Solution of ODE-systems with con-

stant coe�cients

A resistance R, an inductance L and a capacitance C are coupled in series.

Before a voltage source of size E is connected at the time t = 0, the circuit is

at rest. Following [1], this electric ciruit can be described by the di�erential

equation

Lq̈ +Rq̇ +1

Cq = E (1)

, with the initial values q(0) = 0 and q̇(0) = 0. Here, q is the electric charge

and q̇ = i is the electric current. Eq.(1) can be rewritten as a system of �rst

order ODEs. With q̇ = i, we have

du

dt=

(q̇

)=

(0 1

− 1C·L −R

L

(q

i

)+

(0EL

)= Au+ g

, whose analytic solution is described by the Duhamel formula given as

eq.(2.25) in [1]. Since g is independent of t and u(0) = (0, 0)T in the given

system, the Duhamel formula can be simpli�ed to give the solution vector

u(t) = A−1(eAt − Id)g. u(t) is calculated in MATLAB [2] using the MAT-

LAB code appended in Appendix A1 (sec 3.1). For the calculation, the

parameters are set to the values E = 10, L = C = 0.1 and R is varied as

R = 1, 10, 100, 1000, 10000. In �q.1 below both components of the solution

vector u(t) are plotted as a function of time. From the upper left to the

lower right, the parameter R increases by a factor of 10. The system exhibits

the expected behaviour. Since E is a constant voltage source - as opposed to

a time dependent voltage source - the electric current in the circuit depends

solely on the size of the resistance R and practically vanishes for R = 10000.

Page 4: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 3

Figure 1: Plot of the components of the solution vector of eq. 1 at variousvalues for the resistance R.

Page 5: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 4

2 Part 2 - Stability of ODE-systems

2.1 Part 2a - Stability of the solutions of an ODE-system

of LCC-type

In this part of the lab, we investigate how the solution of the di�erential

equation

y′′′ + 3y′′ + 2y′ +Ky = 0 (2)

with the initial conditions y(0) = 1, y′(0) = 1 and y′′(0) = 1, behaves as the

value of K is varied over K = 0, 1, 4, 8. As a �rst step, we rewrite the third

order ODE into a system of �rst order ODEs. With the abbreviations r = y′

and s = r′ we get the system:s′

r′

y′

=

−3 −2 −K

1 0 0

0 1 0

·

s

r

y

= A · u

.

Following [1], the solution vector u(t) of this system can be found using

u(t) = eAtu0, where u0 is given by the initial conditions as u0 = (1, 1, 1)T .

The plots of the components s,r and y of the solution vector u(t) at various

values for the parameter K are shown in �g.2 below. The MATLAB code

that produces these plots is appended in Appendix A.2a1 (sec 3.2). From

�g. 2 below we estimate that the system becomes unstable in the region

4 < K < 8.

Next, we plot the root locus of eq.(2), shown in �g.3 below. The MATLAB

code for the plot is appended in Appendix A.2a2 (sec 3.3)). Decreasing the

step size of the parameter K in the code, we �nd that the smallest value of

K that gives an unstable system is K = 6.0000.

Page 6: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 5

Figure 2: Plots of the components s,r and y of the solution vector u(t) atvarious values for the parameter K

Page 7: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 6

Figure 3: Plot of the root locus of eq.(2) for 0 ≤ K ≤ 10

Page 8: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 7

2.2 Part 2b - Stability of critical points of a non-linear

ODE-system

In this part of Lab 1 we compute the critical points of the non-linear ODE-

System given in the lab assignment or on page 218 in [1]. To do this, we

solve the equation f(u) = dudt

= 0 by means of Newton's Method described

in Appendix A.1 in [1]. The central element of the solution process is the

coding of the recursive formula

u(i+1) = u(i) − J(u(i))−1f(u(i)) (3)

where u is the solution vector, J the Jacobian of the ODE-system. Eq. 3 is

given in [1] in Appendix 1 as eq. (A.7).

Using the start points given in the lab assignment, we use the code given

in Appendix A.2b (sec. 3.4) to �rst calculate the critical points. We then plug

the calculated critical points into the Jacobian and �nd its eigenvalues for

each of the four points. The calculated critical points are (7.95037, -5.07940,

2.44169), (-7.94457, 5.08761, 2.43845), (8.81439, 3.39015, 6.54798) and (-

8.81439, -3.39015, 6.54798). For each of them, the corresponding Jacobian

has three eigenvalues. These eigenvalues are also calculated in the MATLAB

code in Appendix A.2b (sec. 3.4) and plotted in the complex plane in �g.4

below. Here we see, that the �rst two points - in the upper half of �g. 4 are

unstable. The last two points - in the lower half of �g. 4 - however, are stable

stationary points of the ODE-system.

Page 9: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 8

Figure 4: Eigenvalues of the Jacobian J at calculated critical points of thegiven ODE system

Page 10: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 9

3 Appendix

3.1 A1 - MATLAB code for part 1

E = 10 ; L = 0 . 1 ; C = 0 . 1 ; preR = [ 1 , 10 , 100 , 1000 ,

1 0000 ] ; %s e t parameters

f o r n = 1 :5 %loop through parameter R

R = preR (n)

S = −1/(C * L) ; T = −R/L ; U = E/L ;

%de f i n e sho r t cu t s

A = [ 0 1 ; S T ] ;

%de f i n e

Matrix A

g = [ 0 U ] ' ;

%

de f i n e column vec to r g

% s i n c e i n i t i a l va lue s f o r q and q ' are 0 , only

need to c a l c u l a t e second

% term in forumla ( 2 . 2 6 ) from book

f o r k = 1:500

t = (k − 1) * 0 . 0 1 ; %s e t

time r e s o l u t i o n to 0 .1 s ec per i t e r a t i o n ,

s t a r t i n g at t = 0

time ( (k ) , : ) = t ; %s t o r e

time in vec to r

%u_t = inv (A) *(expm(A* t )−eye (2 ) ) *g ; %

c a l c u l a t e s o l u t i o n u_t f o r each value o f t

u_t = A\(( expm(A* t )−eye (2 ) ) *g ) ;

Page 11: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 10

mat( ( k ) , : ) = u_t ; %s t o r e

r e s u l t in a matrix

end

subplot (3 , 2 , n ) ;

p l o t ( time ,mat)

t i t l e ( [ 'R = ' , num2str (R) ] )

x l ab e l ( ' time / t ' )

y l ab e l ( ' u ( t ) / a r b i t r a r y units ' ) ;

mat

end ;

Page 12: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 11

3.2 A.2a1 - MATLAB code for part 2a - estimating K

preK = [ 0 , 1 , 4 , 8 ] ; %de f i n e parameter

preK

h = 0 . 0 1 ; N = 5000 ; %s e t s tep s i z e

and number f o r time c a l c u l a t i o n s

u0 = [1 1 1 ] ' ; %de f i n e vec to r u0

f o r n = 1 : 4 %loop through preK to

de f i n e K

K = preK(n) ;

A = [−3 −2 −K; 1 0 0 ; 0 1 0 ] ; %de f i n e matrix A

f o r k = 1 :N

t = h * (k−1) ;

time (k , : ) = t ;

u = expm(A* t ) *u0 ;

mat(k , : ) = u ;

end

subplot (2 , 2 , n ) ;

p l o t ( time ,mat)

t i t l e ( [ 'K = ' , num2str (K) ] )

x l ab e l ( ' time / t ' )

y l ab e l ( ' components o f s o u l t i o n vec to r u( t ) ' ) ;

end

Page 13: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 12

3.3 A.2a1 - MATLAB code for part 2a - Root Locus

K = 0 ;

h = 0 . 0 0 1 ;

k=1;

whi l e ( K <= 10)

A = [−3 −2 −K; 1 0 0 ; 0 1 0 ] ; %c a l c u l a t e

Eigenva lues o f A

vec = e i g (A) ' ;

e i g enva lu e s (k , : ) = vec ;

dummy = [ e i g (A) ' K] ;

mat(k , : ) = dummy;

K = K + h ;

k = k + 1 ;

end

Re1 = r e a l ( e i g enva lu e s ( : , 1 ) ) ;

Im1 = imag ( e i g enva lu e s ( : , 1 ) ) ;

Re2 = r e a l ( e i g enva lu e s ( : , 2 ) ) ;

Im2 = imag ( e i g enva lu e s ( : , 2 ) ) ;

Re3 = r e a l ( e i g enva lu e s ( : , 3 ) ) ;

Im3 = imag ( e i g enva lu e s ( : , 3 ) ) ;

p l o t (Re1 , Im1 , Re2 , Im2 , Re3 , Im3)

Page 14: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 13

t i t l e ( ' Root Locus ' )

x l ab e l ( 'Re( z ) ' )

y l ab e l ( ' Im( z ) ' ) ;

%f i l t e r from c a l c u l a t i o n s sma l l e s t va lue o f K that

g i v e s an unstab le system

n=1;

whi l e ( r e a l (mat(n , 3 ) ) < 0 | | r e a l (mat(n , 2 ) ) < 0 | | r e a l (

mat(n , 3 ) ) < 0)

n = n+1;

end

d i sp ( [ ' sma l l e s va lue o f K that g i v e s unstab l e system i s

: ' , num2str (mat(n−1 ,4) ) ] )

Page 15: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 14

3.4 A.2b - MATLAB code for part 2b

Main MATLAB script used in part 2b

p r e s e t s = [ 8 −5 2 ; −8 5 2 ; 9 3 7 ; −9 −3 7 ] ;

format long ;

f o r k = 1 :4

u_i = p r e s e t s (k , : ) ;

count=1;

dh=1;

cond i t i on = 0 .00001 ;

whi l e ( ( dh > cond i t i on ) ) % & (dh < 10^5) )

count = count +1;

J_h = Jacobian ( u_i (1 ) , u_i (2 ) , u_i (3 ) ) ;

f_h= −f ( u_i (1 ) , u_i (2 ) , u_i (3 ) ) ;

dh = h(J_h , f_h ) ;

u_new = dh + u_i ' ;

u_i = u_new ' ;

end

r e s u l t (k , : ) = u_new ;

k = k + 1 ;

end

f o r n = 1 :4

A=Jacobian ( r e s u l t (n , 1 ) , r e s u l t (n , 2 ) , r e s u l t (n , 3 ) ) ;

e i g en = e i g (A) ;

Page 16: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 15

Re = r e a l ( e i gen ) ;

Im = imag ( e igen ) ;

subplot (2 , 2 , n )

p l o t (Re (1 ) , Im(1) , Re (2 ) , Im(2) ,Re (3 ) , Im(3) , 'Marker

' , ' x ' , ' MarkerSize ' , 1 2 )

t i t l e ( [ ' S t a b i l i t y at Point [ ' , num2str ( r e s u l t (n , : ) )

, ' ] ' ] )

x l ab e l ( 'Re( z ) ' )

y l ab e l ( ' Im( z ) ' ) ;

end

d i sp ( 'The c r i t i c a l po in t s are at : ' ) ;

r e s u l t

Additional functions used in function �les in part 2b

func t i on [ r e s u l t ] = f (u1 , u2 , u3 )

%func t i on f o f LAB1

r e s u l t = [ (5*u1 + 4*u2 − u1*u3 ) ; ( u1 + 4*u2 − u2*u3 ) ;

( u1^2 + u2^2 −89) ] ;

end

func t i on [ x ] = h(J , f )

%co r r e c t i o n term h f o r the Newton Method used in part 2

b

x = l i n s o l v e (J , f ) ;

end

func t i on [ r e s u l t ] = Jacobian ( u1 , u2 , u3 )

%Jacobian used in part 2b

r e s u l t = [(5−u3 ) 4 (−1*u1 ) ; 1 (4−u3 ) (−1*u2 ) ; 2*u1 2*u2

0 ] ;

end

Page 17: KTH, DN2221, Computer Lab 1

KTH - DN2221 - Computer Lab 1 16

4 Bibliography

References

[1] L. Edsberg, 2008. Introduction to Computation and Modelling for Dif-

ferential Equations. 1st edition. New Jersey: John Wiley & Sons.

[2] MATLAB, 2012. MATLAB R2012a (7.14.0.739). Natick, MA: The

MathWorks Inc.