Transcript
Page 1: Numerical Integration of Ordinary Differential Equation

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:

( ) t

t t tdex e x e e x

dt= → = = = =ɺ ɺ

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

Page 2: Numerical Integration of Ordinary Differential Equation

0

0

(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 y

y 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)

Page 3: Numerical Integration of Ordinary Differential Equation

where

1 1

2 2

(0)

(0)

(0)n n

c y

c 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 x

x x

==

ɺ (7)

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

11 12 11

21 22 22

1 2

and

n

n

nn 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:

Page 4: Numerical Integration of Ordinary Differential Equation

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 0

0 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 ce

cy t c e ey 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:

Page 5: Numerical Integration of Ordinary Differential Equation

1

21

0

0 0

0 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

2

4 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 1

4 3A

=

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

1

1 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

Page 6: Numerical Integration of Ordinary Differential Equation

% 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 i s 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 s ystem (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)

Page 7: Numerical Integration of Ordinary Differential Equation

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)

φ

Page 8: Numerical Integration of Ordinary Differential Equation

% 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=[phi0 phip0]'; % the vector of initial conditions (11) tspan=[0 6*pi]; % duration of integration [t,y]=ode45( 'pendulumrk' ,tspan,X0); %tau is the output time,y is a matrix where phi=y(:,1); %the first row y(:,1) is the angle phi phip=y(:,2); %second row y(:,2) is the angular velocity figure(1) subplot(2,1,1) %the screen is divided in two rows and this plot is shown plot(t,phi*180/pi) %in the first row: time history of the angle (deg) subplot(2,1,2) %the screen is divided in two rows and this plot is shown plot(t,phip*180/pi) %in the second row: time history of angular velocit y (deg/s) figure(2) %in this figure the phase space is shown plot(phi*180/pi,phip*180/pi) title(‘Phase space’) xlabel(‘Angle (deg)’) ylabel(‘Angular velocity (deg/s)’)

%pendulumrk function pendulumrk=pendulumrk(t,y) global k a1=y(2) ; % the first line of equation (14) a2=-k*sin(y(1)) ; % the second line of equation (14) pendulumrk=[a1 a2]’ ; In the following figures the variation in time of the angle φ and of the angular velocity φɺ are shown.

In the next figure the phase plane ( ),φ φɺ is shown the five states A-E are shown in the plot.

Page 9: Numerical Integration of Ordinary Differential Equation

Note that there is a function of the state variables that stay constant during the motion, that is an “integral of the motion”. This is the pendulum energy:

( )21cos

2E kφ φ= −ɺ

In fact

( ) ( )( )12 sin sin

2

dEk k

dtφ φ φ φ φ φ φ= + = +ɺ ɺɺ ɺ ɺɺ ɺ

and the term in the bracket is zero according to the equation of motion (13). Then the energy at the beginning of the integration

( )20 0 0

1cos

2E kφ φ= −ɺ

must be equal to the energy evaluated along the numerical solution found:

0Error E E= −

The above formula measures the error of our numerical integration, since the difference 0E E−

should be equal to zero. Then let us add the following commands to the program pendulum: E0=0.5*phip0^2-k*cos(phi0); E=0.5*phip^2-k*cos(phi); Error=E0-E; figure (4) plot(t,Error) The following figure shows the time variation of the Error function.

Page 10: Numerical Integration of Ordinary Differential Equation

Note that Error is increasing and of order 1e-06: to get a better accuracy it is convenient to change the values of the relative tolerance ‘RelTol’ and of the absolute tolerance ‘AbsTol’ in the file ode45. A good choice is the following: Tol=1e-012; Tol0=1e-09; options=odeset( 'RelTol' ,Tol0, 'AbsTol' ,Tol); Adding these commands to the program pendulum and substituting the command [t,y]=ode45( 'pendulumrk' ,tspan,XX0);

with the command [t,y]=ode45( 'pendulumrk' ,tspan,XX0, options);

the value of the error function becomes as in the following figure, thus the accuracy is by far improved (of order 1e-12). We suggest to adopt always these values of ‘RelTol’ and of ‘AbsTol’.

Page 11: Numerical Integration of Ordinary Differential Equation

Example 6: Kepler equation The gravitational acceleration given by the Earth to a satellite is

3 2ˆg r r

r r

µ µ= − = −� � (15)

where the symbol ⋮ indicates versor , that is ˆr rr=� . In the formula (15) the vector r

� is the radius vector, that is the vector connecting the centre of the

Earth with the center of mass of the satellite and (15) is also called the Keplerian field: it is the Earth gravitational acceleration in the hypothesis of spherical homogeneous Earth. The equations of the motion are then:

3

drV

dt

dVr

dt r

µ

=

= −

� �

��

(16)

where the first equation states that the derivative of the position vector is the velocity vector and second states that the acceleration of the satellite obeys the Kepler law. We are going to write the system of the 6 differential equations (three for the position and three for the velocity) in scalar form projecting (16) in the inertial reference frame ( )1 2 3ˆ ˆ ˆ, ,c c c , see the

following figure.

Let the position coordinates with respect to the inertial reference frame be (X,Y,Z), that is:

1 2 3ˆ ˆ ˆr Xc Yc Zc= + +�

and let the velocity coordinates with respect to the inertial reference frame be ( ), ,X Y ZV V V , that is:

1 2 3ˆ ˆ ˆX Y ZV V c V c V c= + +�

Page 12: Numerical Integration of Ordinary Differential Equation

Then the derivative of the vector r�

is equal to:

( ) 31 21 2 3 1 2 3

ˆˆ ˆˆ ˆ ˆ ˆ ˆ ˆ

dcdr d dc dcXc Yc Zc Xc Yc Zc X Y Z

dt dt dt dt dt= + + = + + + + +�

ɺ ɺ ɺ

However the reference frame ( )1 2 3ˆ ˆ ˆ, ,c c c is inertial, that is the directions of the three versors

( )1 2 3ˆ ˆ ˆ, ,c c c do not change with time, so 31 2 ˆˆ ˆ0

dcdc dc

dt dt dt= = = and

1 2 3

1 2 3

ˆ ˆ ˆ

ˆ ˆ ˆX Y Z

drXc Yc Zc

dt

drV V c V c V c

dt

= + +

= = + +

�ɺ ɺ ɺ

� �

Comparing the two expressions of dr

dt

we get the three scalar equations:

X

Y

Z

X V

Y V

Z V

=

=

=

ɺ

ɺ

ɺ

(17)

The Keplerian acceleration g�

written in the inertial reference frame is:

( ) 2 2 21 2 33 3ˆ ˆ ˆ , g r Xc Yc Zc r X Y Z

r r

µ µ= − = − + + = + +� �

On the other hand

1 2 3ˆ ˆ ˆX Y Z

dVV c V c V c

dt= + +�ɺ ɺ ɺ

Hence the second vector equation in (16) becomes:

( )

( )

( )

3/ 22 2 2

3/22 2 2

3/ 22 2 2

X

Y

Z

V XX Y Z

V YX Y Z

V ZX Y Z

µ

µ

µ

= −+ +

= −+ +

= −+ +

ɺ

ɺ

ɺ

(18)

Equations (17) and (18) represent the Kepler equations in the inertial reference frame. A Keplerian orbit is a solution of the above Kepler equations with a specified initial condition for the position and the velocity:

0 0 0 0 0 0 0 0, , , , , ,X Y Zr V X Y Z V V V=��

Note that the initial conditions 0 0 0ZZ V= = correspond to an equatorial orbit, since the vertical

acceleration also will be vanishing (0 03 30 0

.0 0ZV Zr r

µ µ= − = − =ɺ ) and the initially equatorial orbit will

stay equatorial. Also for the Kepler equation an integral of the motion exists: namely

Page 13: Numerical Integration of Ordinary Differential Equation

( ) ( )2

1/2

1 1

2 2E V V V

r r r

µ µ= − = −� �i � �

i

In fact the derivative of E gives:

( ) ( ) ( )1/2 3/2

1 1 1 2 2 2

dE d d dV dV dr drV V V V r r

dt dt dt dt dt dt dtr r r r

µ µ = − = + + +

� � � �� � � � � �i i i i i� � � �

i i

that is

( )3/ 2 3

dE dV dr dV drV r V r

dt dt dt dt r dtr r

µ µ = + = +

� �� �� �� �i i i i� �

i

Using the equations of motion (13) we get the conservation of the energy :

( )3/ 2 0 0dE dV

r V Vdt dt r r

µ = + = =

�� ��i i� �

i

As an example let us integrate the Kepler equation (17)-(18) with the initial conditions (equatorial

orbit): ( )0 0 0 0 0 0 0 0, 100 km, 0, 0, 0, 8 / , 0E X Y Zr V X R Y Z V V km s V= = + = = = = =��

, using the

numerical program: %kepler clear all close all global mi RE %mu is the Earth grav. constant and RE the radius o f the Earth Tol=1e-012; Tol0=1e-09; RE=6378; % Earth radius in km mi=398601; % Earth gravitational costant in km^3/s^2 tmax=6000; % integration time (s) tspan=[0 tmax]; x0=RE+700; % initial conditions y0=0; z0=0; Vx=0; Vy=8; Vz=0; r0=(x0.^2+y0.^2+z0.^2).^0.5; % initial radius V0=(Vx.^2+Vy.^2+Vz.^2).^0.5; % initial velocity XX0=[x0 y0 z0 Vx Vy Vz]; options=odeset( 'RelTol' ,Tol0, 'AbsTol' ,Tol); [t,y]=ode45( 'keplerk' ,tspan,XX0,options); X=y(:,1); Y=y(:,2); Z=y(:,3); VX=y(:,4); VY=y(:,5); VZ=y(:,6); LL=length(X); [XS,YS,ZS]=sphere; % commands to define the Earth surface XS=XS*RE; YS=YS*RE; ZS=ZS*RE; figure(1) plot3(X,Y,Z, 'k' ) % plot of the orbit grid hold on surf(XS,YS,ZS)

Page 14: Numerical Integration of Ordinary Differential Equation

r=(X.^2+Y.^2+Z.^2).^0.5; V=(VX.^2+VY.^2+VZ.^2).^0.5; En=0.5*V.*V-mi./r; % the orbit energy En0=0.5*V0.*V0-mi./r0; % the initial energy figure(2) %plot of the three components of the orbit velocity plot(t,VX,t,VY,t,VZ) xlabel(‘time (s)’) title(‘Velocities (km/s)’) figure(3) plot(t,En-En0) % plot of the energy variation title( 'Energy(t)-Energy(t0)' ) xlabel( 'time (s)' ) % program keplerk function keplerk=keplerk(t,y) global mi RE X=y(1); Y=y(2); Z=y(3); vx=y(4); vy=y(5); vz=y(6); r=sqrt(X^2+Y^2+Z^2); a1=vx; a2=vy; a3=vz; a4=-mi/r^3*X; a5=-mi/r^3*Y; a6=-mi/r^3*Z; keplerk=[a1 a2 a3 a4 a5 a6]'; The following two figures show the orbit and the orbit together the Earth surface.

Page 15: Numerical Integration of Ordinary Differential Equation

The following figure shows the three components of the orbit velocity

The following figure shows the difference between the initial energy and the energy along the orbit.

Page 16: Numerical Integration of Ordinary Differential Equation

The orbit that we found is not a circle around the Earth, but rather an ellipse, as it can be noticed looking at the X,Y plot.

This means that the modulus of the radius is changing in time, that is 0r r= ≠ɺɺ ɺ . To get a circular orbit one should have constant radius and a constant angular velocity of the satellite around the Earth.

Generally this angular velocity is variable and it can represent the angular velocity hω θ=� ɺ of the rotation of the frame having the mobile axis r , as in the figure.

r

Page 17: Numerical Integration of Ordinary Differential Equation

The transformation between the two reference frames is:

1 1

2 2

3 3

ˆ ˆˆ ˆcos sin 0 cos sin 0ˆ ˆˆ ˆsin cos 0 and sin cos 0

ˆ ˆˆ ˆ0 0 1 0 0 1

r rc c

c c

c ch h

θ θ θ θθ θ θ θ θ θ − = − =

It follows that:

( ) ( )1 2 1 2

ˆˆ ˆ ˆ ˆ ˆ ˆ ˆ ˆcos sin sin cos

dr dr dr rr rr r rr r c c rr r c c

dt dt dtθ θ θ θ θ= ⇒ = + = + + = + − +

�� ɺɺ ɺ ɺ

That is

( ) ( )1

2

ˆˆ ˆcos sin ˆˆ ˆ ˆsin cos sin cosˆˆ ˆsin cos

c rdrrr r rr r rr r

cdt r

θ θθθ θ θ θ θ θ θθ

θ θθ

− = + − = + − = + +

�ɺ ɺ ɺɺ ɺ ɺ

Note that ˆ ˆˆ ˆ ˆ h hr r r rr rθ θ θ θ ω= ∧ = ∧ = ∧� �ɺ ɺ ɺ

so dr

r rdt

ω= + ∧�

�ɺ

This is a general result: the derivative of any vector b�

in a reference frame rotating by the angular velocity ω� with respect to the inertial frame is equal to the derivative of the vector written in the

rotating frame plus the vector product between the angular velocity ω� and the vector b�

. Then the acceleration is:

( ) ( ).2

22

d rr r r r r r r r

dtω ω ω ω ω ω ω

= + ∧ + ∧ + ∧ = + ∧ + ∧ + ∧ ∧

� ��� � � � � �ɺɺ ɺ ɺɺ ɺ

and the Kepler equation is in the (orbital) frame ( )ˆˆˆ , ,r hθ is:

( ) 2ˆ2r r r r r

r

µω ω ω ω+ ∧ + ∧ + ∧ ∧ = −� � �ɺɺɺ ɺ

For a circular orbit we must have 0r r ω= = =ɺɺɺ ɺ hence the Kepler equation gives:

( ) 2ˆr r

r

µω ω∧ ∧ = −� �

That is

2 22 2

ˆ ˆ r r r rr r

µ µθ θ− = − ⇒ =ɺ ɺ

Then the constant angular velocity of a circular orbit of radius 0r r= is:

30

cr

µθ =ɺ

We have also for circular orbits:

Page 18: Numerical Integration of Ordinary Differential Equation

ˆ dr

V r rdt

ω θ θ= = ∧ =� � � ɺ

Therefore the velocity of a circular orbit is :

00

ˆ ˆ c cV rr

µθ θ θ= =�

ɺ

The velocity needed to have a circular orbit with altitude 100 km is 7.84 km/s. The following figure shows the (circular) orbit obtained with 7.84 /YV km s= .

Decreasing the value of YV the orbit will approach the Earth surface, till the impact with the Earth,

see for instance the following picture where 5 /YV km s=

Only a rather small part of the orbit is outside the Earth: most of the trajectory would go through inside the Earth, passing close the Earth centre.

Page 19: Numerical Integration of Ordinary Differential Equation

On the other hand increasing the velocity YV the orbits will be bigger and bigger ellipses around the

Earth till the value 0

2YVr

µ= (zero keplerian energy) : with this and higher values of the velocity

the orbit will be non longer closed curves. This means that we have not longer a satellite of the Earth, but the spacecraft will leave the Earth: the gravitational attraction of the Earth is not longer able to keep the satellite close to Earth. The following figure shows the orbit obtained by 11.5 /YV km s=

With such a velocity the spacecraft will leave the Earth along an hyperbolic trajectory.


Recommended