# Numerical Integration of Ordinary Differential Equation

• View
218

3

Embed Size (px)

DESCRIPTION

Conceptual approach of integrating ODE.

Transcript

• High Level Postgraduate Course in Aerospace Engineering

School of Aerospace Engineering of Rome

Lecture Notes for Cycle 1 Numerical Integration of Ordinary Differential Equations

The aim is to find a curve (with respect to time) having tangent direction defined by a vector field of directions. Namely the usual formula1

( )x f x= (1) means that a vector f is attached to any point x and we are looking for a curve ( )x t such that its tangent is equal to the vector ( ( ))f x t . The curve ( )x t is called the solution of the differential equation (1) and a unique solution is identified by the passage through a specified point at the initial time 0t (time zero, 0 0t = ):

0 0( ) (0)x t x x= = (2) Example 1 An easy differential equation is the following one:

0(0)x x

x x

=

=

(3)

Namely from the mathematical point of view we are seeking for a function x such that its derivative is equal to the function itself: this is a property of the exponential function:

( ) tt t tdex e x e e xdt= = = = = Also the product of the exponential function by a constant makes the same job:

( ) ( ) t

t t td ke

x ke x k e ke xdt

= = = = =

In fact the general solution of the equation x x= is just tx ke= , and the particular solution required by the problem (3) is given by the relationships:

1 Here and in the following the symbols and x x both indicate vectors

• 00

(0)

(0)

x ke k

x x

= =

=

so 0k x= , and the solution of (3) is:

0( ) tx t x e=

Example 2 A similar problem is the following:

0

(0)x a x

x x

=

=

(4)

Since this problem is similar to problem (3) , let as try a solution similar to the one before: ( ) tx t ke=

First derive:

deftx ke x

x a x

= =

=

Then, by comparison, a = , and ( ) atx t k e= Now impose the passage through 0x at time 0 0t = :

0

0

(0)

(0)

ax ke k

x x

= =

=

Then 0k x= and the solution of (4) is t

0( ) ax t x e=

Example 3 Till now we considered scalar differential equations: the above solution applies to the following system of de-coupled differential equations

1 1 1

2 2 2

n n n

y yy y

y y

=

= =

(5)

Since the equations are decoupled, we have n solutions equal to the one obtained in Example 2:

1

2

1 1

2 2

( )( )

( ) n

t

t

tn n

y t c e

y t c e

y t c e

=

=

=

(5.1)

• where 1 1

2 2

(0)(0)

(0)n n

c yc y

c y

=

= =

Example 4 Consider now the system of linear differential equations:

1 11 1 12 2 1

2 21 1 22 2 2

1 1 2 2

n n

n n

n n n nn n

x a x a x a x

x a x a x a x

x a x a x a x

= + + +

= + + + = + + +

(6)

with initial conditions 1 10

2 20

0

(0)(0)

(0)n n

x x

x x

x x

=

= =

That is in matrix form:

0

(0)x A xx x

=

=

(7)

where the vector x and the matrix A are equal to:

11 12 11

21 22 22

1 2

and

n

n

n

n n nn

a a ax

a a ax

x A

xa a a

= =

The difference with respect to Example 3 is that now the differential equations of the system are paired. It would be nice to find a transformation of coordinates (a matrix T of constant entries )

yx T= (8)

such that the transformed system of differential equation in y coordinates has the same form of (4), so we can find the solution ( )y t easily and the required solution ( )x t will be obtained by the transformation T. Let us try:

• y

x T

x Ax AT y

=

= =

Then the differential equations in y coordinates are:

1 y T A T y= (9)

It is of interest to determine the transformation T such that:

1

21

0 00 0

with =

0 n

T AT

=

so the system (9) becomes: y y=

which is exactly the matrix version of the system of differential equations (5). The solution (5.1) can be used:

1 1

2 2

1 1 1

22 2

( ) 0 0( ) 0 0( )

0 0( ) nn

t t

t t

ttnn n

y t c e cecy t c e e

y t

cey t c e

=

= = =

=

where ( )1 2, , , nc c c are by now unknown constant terms. On the other hand the required initial conditions 0(0)x x= imply:

1

20(0) (0)

n

c

cx x Ty T

c

= = =

then

1

2 10

n

c

cT x

c

=

It follows that the solution of problem (7) is given by the following formula:

• 121

0

0 00 0( )

0 0 n

t

t

t

e

ex t Ty T T x

e

= =

The matrix is the matrix of the eigenvalues2 of the matrix A and T is the matrix of the eigenvectors of the matrix A. To find the eigenvalues and eigenvectors of a matrix (especially if the number n is bigger than three) it is convenient to relay to a numerical software using the command: [T,Lambda]=eig(A);

Example 4.1 Let the following system of linear differential equations be given:

1 1 2

2 1 2

24 3

x x x

x x x

= +

= +

(10)

with initial conditions: 1

1

(0) 1(0) 3

x

x

=

= (11)

The matrix of the system is 2 14 3

A =

The eigenvalues and eigenfunctions of the matrix A are equal to: [ , ] ( )T Lambda eig A=

0.5393 0.3637 0.4384 0,

0.8421 0.9315 0 4.5616T Lambda

= =

That is

( )* * inv T A T Lambda=

The solution of the problem (10)-(11) is then:

1

2

10.4384 t1

0 4.5616 t

0.5393 0.3637 0.5393 0.3637 100( )0.8421 0.9315 0.8421 0.9315 300

t

t

eex t T T x

ee

= =

(12)

where

11 0.5393 0.3637 1.1520 0.4497

0.8421 0.9315 1.0414 0.6669T

= =

Now the analytic solution (12) will be compared with the numerical solution of the problem (10)-(11) obtained by the following numerical program:

2 Here it is assumed for simplicity that all the eigenvalues of the matrix A are distinct

• % program lin close all % this command cancels all the figures clear all % this command refreshes the memory global a11 a12 a21 a22 % the parameters are shared with other programs a11=2; a12=1; a21=4; a22=3; A=[a11 a12;a21 a22]; % the matrix A of the system (10) x10=1; x20=3; X0=[x10 x20]'; % the vector of initial conditions (11) tspan=[0 1]; % duration of integration [tau,y]=ode45('linrk',tspan,XX0); %tau is the output time, y is the matrix where X1=y(:,1); %the first row y(:,1) is the solution x1 the X2=y(:,2); %second row y(:,2) is the solution x2 subplot(2,1,1) % the screen is divided in two rows and this plot is shown plot(tau,X1,o) % in the first row by dots subplot(2,1,2) % the screen is divided in two rows and this plot is shown plot(tau,X2,o) % in the second row by dots

%program linrk it is the program defining the derivatives of the system (10) function linrk=linrk(tau,y) global a11 a12 a21 a22 a1=a11*y(1)+a12*y(2); a2=a21*y(1)+a22*y(2); linrk=[a1 a2]';

The following figure compares the numerical solutions X1 and X2 obtained by the programs lin, linrk (plotted by dots) and the analytic solutions (12) (plotted by a continuous line). The good agreement is apparent.

Time (s)

• Example 5: Pendulum

The dynamics of a pendulum is represented by the equation:

sin( )k = (13) where is the angular displacement from the vertical direction of the tip mass of the pendulum, see Figure 1. The motion depends on the initial angle and the initial angular velocity, that is

0

0

(0)(0)

=

=

For instance, let the tip mass be initially set along the vertical, that is (0) 0 = , with angular velocity (0) 0.1 = (point A in Figure 1). Because of this angular velocity the tip mass will move to the right to reach a point of maximum angular deviation from the vertical direction (maximum ). This state will be reached with zero angular velocity (point B in Figure 1). Then the angular velocity becomes negative and the tip mass will move backwards toward the vertical position and will reach such a state with the same initial velocity, but now in negative sense (point C in Figure 1). The maximum negative amplitude will be reached with zero angular velocity (point D) . After reaching point D the angular velocity becomes positive and the entire cycle is performed when the tip mass crosses the vertical position with the same (positive) initial velocity (point E).

The pendulum motion can be obtained by the following numerical program which solves the equation of motion (13) (a differential equadion of order two) in the equivalent form of a system of two differential equations of order one:

sin( )u

u k

=

=

(14)

• % program pendulum close all % this command cancels all the figures clear all % this command refreshes the memory global k % the parameters are shared with other programs k=1; phi0=0; % initial angle phip0=0.1; % initial angular velocity X0=[phi