You are on page 1of 96

Computational Methods for Engineers Ordinary Dierential Equations

Abu Hasan Abdullah January 7, 2009

Abu Hasan Abdullah

2009

Introduction
Newtons second law to compute velocity v of a falling mass m as a function of time t c dv =g v dt m where g is gravitational constant, m is mass and c is drag coecient Eq. (1) is composed of unknown function, and its derivatives Such equation is called dierential equation or rate equation
Abu Hasan Abdullah 2009 1

(1)

Introduction
In Eq. (1) v = quantity being dierentiated, i.e. dependent variable t = quantity w.r.t. which dv is dierentiated, i.e. independent variable When function involves ONE independent variable, the equation is called the ordinary dierential equation. When function involves TWO or MORE independent variables, the equation is called the partial dierential equation.

Abu Hasan Abdullah

2009

Non-computer Methods for Solving ODE


Without computers, ODE are solved with analytical integration techniques Eq. (1) could be multiplied by dt and then integrated to yield v= g c v dt m (2)

RHS of Eq. (2) is an indenite integral because limits of integration are not specied Eq. (2) can be solved analytically, assuming v = 0 at t = 0 to yield v(t) = gm 1 e(c/m)t c

Abu Hasan Abdullah

2009

Order of ODE
Dierential equations are classied as to the order: First order equationhighest derivative is rst derivative dv c =g v dt m Second order equationhighest derivative is second derivative, for example equation describing position x of mass-spring system with damping d2 x dx m 2 + c + kx = 0 dt dt where c is gravitational constant and k is mass and c is drag coecient Similarly, nth order dierential equation would include nth derivative.
Abu Hasan Abdullah 2009 4

(3)

Order of ODE
Higher order dierential equation can be reduced to a system of rst order equations. For Eq. (3) above, we do it by dening dx y= dt which is dierentiated to yield dy d2x = 2 dt dt Eqs. (4) and (5) are then substituted into Eq. (3) to yield dy m + cy + kx = 0 dt
Abu Hasan Abdullah 2009

(4)

(5)

(6)

Order of ODE
Eq. (6) can be re-arranged into dy cy + kx = =0 dt m (7)

Thus Eqs. (4) and (7) are two rst order equations that are equivalent to the original second order equation, i.e. Eq. (3).

Abu Hasan Abdullah

2009

Linearization
A linear ODE is one that t the general form an(x)y (n) + . . . + a1(x)y + a0(x)y = f (x) (8)

where y (n) is nth derivative of y w.r.t. x, a is specied function of x and c is specied function of x Eq. (8) is linear because there are NO products or nonlinear function of dependent variable y and its derivatives Why linear ODE are important? Because they can be solved analytically. In contrast, most nonlinear equations cannot be solved exactly!!
Abu Hasan Abdullah 2009 7

Linearization
Take an example of motion of a swinging pendulum governed by d2 g + sin = 0 dt2 l Eq. (9) is nonlinear because of the term sin To obtain solution, we linearize Eq. (9) by assuming, for small values of sin which, on substitution into Eq. (9), yields a linear form d2 g + =0 2 dt l
Abu Hasan Abdullah 2009

(9)

(10)

(11)
8

Linearization

Figure 1: Motion of swinging pendulum.


Abu Hasan Abdullah 2009 9

ODE in Engineering Practice


Newtons second law of motion dv F = dt m where v is velocity , F is force and m is mass Fouriers heat law dT q = k dx where q is heat ux, k is thermal conductivity and T is temperature
Abu Hasan Abdullah 2009 10

(12)

(13)

ODE in Engineering Practice


Ficks law of diusion dc J = D dx where J is mass ux, D is diusion coecient and c is concentration Faradays law (voltage drop across an inductor) Vt = L di dt (15) (14)

where Vt is voltage drop, L is inductance and i is current


Abu Hasan Abdullah 2009 11

ODE - Initial Value Problem Mathematical Background


A solution of an ODE is a specic function of independent variable and parameters that satisfy the original dierential equation. Let say we were given a forth order polynomial y = 0.5x4 + 4x3 10x2 + 8.5x + 1 Dierentiating Eq. (16) gives us an ODE dy = 2x3 + 12x2 20x + 8.5 dx (17) (16)

Eq. (17) also describes the behaviour of a polynomial but in a dierent manner than Eq. (16)i.e. it gives rate of change of y w.r.t. x, which is a slope.
Abu Hasan Abdullah 2009 12

ODE - Initial Value Problem Mathematical Background


But our objective here is to determine the original function (Eq. (16)) given the ODE (Eq. (17)). So we go back to Eq. (17) and solve analytically y= which leads to y = 0.5x4 + 4x3 10x2 + 8.5x + C (18) (2x3 + 12x2 20x + 8.5)dx

Eq. (18) is identical to Eq. (16) with one notable exception1 in Eq. (16) being replaced by C in Eq. (18) and C is the constant of integration which is arbitrary.
Abu Hasan Abdullah 2009 13

ODE - Initial Value Problem Mathematical Background


Therefore, to specify the solution completely, and ODE is usually accompanied by auxiliary condition(s) For rst order ODE, this auxiliary condition, called initial value, is required to determine the constant of integration and obtain a unique solution.

Abu Hasan Abdullah

2009

14

ODE - Initial Value Problem Mathematical Background


In order to get back Eq. (16), Eq. (18) needs the initial value that x = 0, y = 1. Substituing the initial value into Eq.(18) yields 1 = 0.5(0)4 + 4(0)3 10(0)2 + 8.5x + C or C=1 Hence, y = 0.5x4 + 4x3 10x2 + 8.5x + 1 (20) (19)

Abu Hasan Abdullah

2009

15

ODE - Initial Value Problem Basis of Numerical Solution


Basis of numerical method devoted to solving ODE of the form dy = f (x, y) dx is New value = Old value + (Slope Step size) which can be mathematically expressed as yi+1 = yi + h (21)

where is the slope estimate. This method is known as one-step method.


Abu Hasan Abdullah 2009 16

ODE - Initial Value Problem Basis of Numerical Solution

Figure 2: One-step method.


Abu Hasan Abdullah 2009 17

ODE - Initial Value Problem Eulers Method


The rst derivative provides a direct estimate of the slope at xi = f (xi, yi ) where f (xi, yi ) is dierential equation evaluated at xi and yi. Substituting this into Eq. (21) yields yi+1 = yi + f (xi , yi)h Eq. (22) represents the Eulers (or Cauchy-Euler) method. (22)

Abu Hasan Abdullah

2009

18

ODE - Initial Value Problem Eulers Method

Figure 3: Eulers method.


Abu Hasan Abdullah 2009 19

ODE - Initial Value Problem Eulers Method - Example #1


Problem Statement: Use Eulers method to solve dy = 2x3 + 12x2 20x + 8.5 dx from x = 0 to x = 4 with step size of 0.5. Initial condition at x = 0 is y = 1.

Abu Hasan Abdullah

2009

20

ODE - Initial Value Problem Eulers Method - Example #1


Solution: At x = 0.5, Eq. (22), with initial condition y(0) = 1, yield y(0.5) = y(0) + f (0, 1) 0.5 = 1.0 + [2(0)3 + 12(0)2 20(0) + 8.5] 0.5 = 5.25 True solution at x = 0.5 is y = 0.5(0.5)4 + 4(0.5)3 10(0.5)2 + 8.5(0.5) + 1 = 3.21875 Ex = 3.21875 5.25 = 2.03125 or Rx = 63.1%

Abu Hasan Abdullah

2009

21

ODE - Initial Value Problem Eulers Method - Example #1


At x = 1.0, y(1.0) = y(0.5) + f (0.5, 5.25) (0.5) = 5.25 + [2(0.5)3 + 12(0.5)2 20(0.5) + 8.5] 0.5 = 5.875 True solution at x = 1.0 is y = 0.5(1.0)4 + 4(1.0)3 10(1.0)2 + 8.5(1.0) + 1 = 3.0 Ex = 3.0 5.875 = 2.875 or Rx = 95.8%

Abu Hasan Abdullah

2009

22

ODE - Initial Value Problem Eulers Method - Example #1


Computation is repeated and results tabulated x 0.0 0.5 1.0 1.5 . . 4.0 ytrue 1.00000 3.21875 3.00000 2.21875 . . 3.00000 yEuler 1.00000 5.25000 5.87500 5.12500 . . 7.00000 Error (%) 0.0 -63.1 -95.8 131.0 . . -133.3

Show the eect of reduced step size.

Abu Hasan Abdullah

2009

23

ODE - Initial Value Problem Eulers Method - Example #1


Using Matlab to solve the same involves the following steps: 1. Write the ODE into dydx.m function df=dydx(x) df = -2*x.^3 + 12*x.^2 - 20*x + 8.5;

2. Optionally, write the known solution into ytrue.m function y=ytrue(x) y = -0.5*x.^4 + 4*x.^3 - 10*x.^2 + 8.5*x + 1;

Abu Hasan Abdullah

2009

24

ODE - Initial Value Problem Eulers Method - Example #1


3. Matlab script to solve the ODE is written in myeuler.m x0 = 0.0; xn = 4.0; xstep=0.01; x = [x0:xstep:xn]; n = length(x); xt = linspace(x(1),x(n),100); yt = ytrue(x); plot(x,yt,r); hold on y(1) = 1.0; % This is INITIAL VALUE for i=2:n y(i) = y(i-1) + dydx(x(i-1))*xstep; end err = abs((yt-y)./yt)*100; plot(x,y,b) [x y yt err]
Abu Hasan Abdullah 2009 25

ODE - Initial Value Problem Eulers Method - Postscript


First order technique such as Eulers method demands great computational eort to obtain acceptable error level Simplicity of Eulers method makes it attractive, easy to program, suitable for quick analyses.

Abu Hasan Abdullah

2009

26

ODE - Initial Value Problem Heuns Method


An attempt to improve Eulers method Determines two derivatives for the interval or steps at beginning of interval at end of interval These two derivatives are then averaged to get an improved estimate of the slope.

Abu Hasan Abdullah

2009

27

ODE - Initial Value Problem Heuns Method

Figure 4: Heuns method.


Abu Hasan Abdullah 2009 28

ODE - Initial Value Problem Heuns Method


Slope at beginning of interval
yi = f (xi, yi) o is used to extrapolate linearly to yi+1 o yi+1 = yi + f (xi, yi) h o yi+1 is an intermediate prediction called predictor

(23)

(24)

Abu Hasan Abdullah

2009

29

ODE - Initial Value Problem Heuns Method


Slope at end of interval is estimated using the predictor in Eq. (24)
o yi+1 = f (xi, yi+1)

(25)

The two slopesEqs. (23) and (25)are combined to obtain average slope for the interval
o yi + yi+1 f (xi, yi ) + f (xi+1, yi+1) = y= 2 2

(26)

Abu Hasan Abdullah

2009

30

ODE - Initial Value Problem Heuns Method


The average slope, Eq. (26), is then used to extrapolate linearly from yi to yi+1 using Eulers method
o f (xi, yi) + f (xi+1, yi+1) h yi+1 = yi + 2

(27)

Eq. (27) is known as the corrector equation Heuns method is the basis of many other predictor-corrector methods

Abu Hasan Abdullah

2009

31

ODE - Initial Value Problem Heuns Method - Example #1


Problem Statement: Use Heuns method to solve dy = 4e0.8x 0.5y dx from x = 0 to x = 4 with step size of 1.0. Initial condition at x = 0 is y = 2. Note: Analytical solution is y= 4 e0.8x e0.5x + 2e0.5x 1.3

Abu Hasan Abdullah

2009

32

ODE - Initial Value Problem Heuns Method - Example #1


Solution: Slope at initial point (x0, y0)
y0 = 4e0 0.5(2) = 3

Use predictor equation (Eq. (24)) to obtain estimate of y at 1.0


o y1 = 2 + 3(1) = 5 o Use value yi to predict slope at end of interval o y1 = f (x1, y1 ) = 4e0.8(1) 0.5(5) = 6.402164

Abu Hasan Abdullah

2009

33

ODE - Initial Value Problem Heuns Method - Example #1


Average slope over the interval 0 x 1 x = 0 and x = 1 is y = 3 + 6.402164 = 4.701082 2

This average slope is then fed into the corrector equation (Eq. (27)) to give an estimate at x = 1 y1 = 2 + 4.701082(1) = 6.701082

Abu Hasan Abdullah

2009

34

ODE - Initial Value Problem Heuns Method - Example #1


Computation is repeated and results tabulated x 0.0 1.0 2.0 3.0 4.0 ytrue 2.0000000 6.1946314 14.8439219 33.6771718 75.3389626 yHeun 2.0000000 6.70108190 16.3197819 37.1992489 83.3377674 Error (%) -0.00 -8.18 -9.94 -10.46 -10.62

Show the eect of reduced step size.

Abu Hasan Abdullah

2009

35

ODE - Initial Value Problem Heuns Method - Example #1


Using Matlab to solve the same involves the following steps: 1. Write the ODE into dydx1.m function df=dydx1(x,y) df = 4*exp(0.8*x) - 0.5*y;

2. Optionally, write the known solution into ytrue1.m function y=ytrue1(x) y = (4/1.3)*(exp(0.8*x) - exp(-0.5*x)) + 2*exp(-0.5*x);

Abu Hasan Abdullah

2009

36

ODE - Initial Value Problem Heuns Method - Example #1


3. Matlab script to solve the ODE is written in myheun.m x0=0.0; xn=4.0; xstep=0.25; x=[x0:xstep:xn]; n=length(x); yt = ytrue1(x); plot(x,yt,r); hold on x(1)=0.0; y(1)=2.0; % INITIAL CONDITION for i=2:n islope = dydx1(x(i-1),y(i-1)); predictor = y(i-1) + islope*xstep; eslope = dydx1(x(i),predictor); avslope = (islope + eslope)/2; y(i) = y(i-1) + avslope*xstep; end
Abu Hasan Abdullah 2009 37

ODE - Initial Value Problem Heuns Method - Example #1


err = abs((yt-y)./yt)*100; plot(x,y,b) [x y yt err]

Abu Hasan Abdullah

2009

38

ODE - Initial Value Problem Runge-Kutta (RK) Methods


Advantageachieve accuracy of a Taylor series approach without requiring calculation of higher derivatives Generalized form yi+1 = yi + (xi, yi, h) h (28)

where (xi, yi , h) is an increment function which is a representative slope over the interval and given by = a1k1 + a2k2 + . . . + ankn (29)

Abu Hasan Abdullah

2009

39

ODE - Initial Value Problem Runge-Kutta (RK) Methods


ks in Eq. (29) are recurrence relationships i.e. k1 appears in k2 which appears in k3 and so forth k1 = f (xi, yi ) k2 = f (xi + p1h, yi + q11k1h) k3 = f (xi + p2h, yi + q21k1h + q22k2h) ... kn = f (xi + pn1h, yi + qn1,1k1h + qn2,2k2h + . . . + qn1,n1kn1h) (33) (30) (31) (32)

Abu Hasan Abdullah

2009

40

ODE - Initial Value Problem Runge-Kutta (RK) Methods


Various types of RK methods can be devised by employing dierent numbers of terms in the increment function as specied by n Once n is chosen, values for as, ps and qs are evaluated by setting Eq. (28) equal to terms in a Taylor series expansion. First order RK method, when n = 1, is actually Eulers method. Second order RK method, when n = 2, is exact if solution to ODE is quadratic.

Abu Hasan Abdullah

2009

41

ODE - Initial Value Problem RK1 Method


For n = 1, we can write, using Eq. (28), the rst order RK (RK1) method as yi+1 = yi + (a1k1)h where k1 = f (xi, yi ) With a1 = 1, the rst order RK method yi+1 = yi + f (xi , yi)h can be seen to be the same as Eulers method.
Abu Hasan Abdullah 2009 42

(34)

(35)

ODE - Initial Value Problem RK2 Methods


For n = 2, we can write, using Eq. (28), the second order RK (RK2) method as yi+1 = yi + (a1k1 + a2k2)h where k1 = f (xi, yi) k2 = f (xi + p1h, yi + q11k1h) To use Eq. (36), values for a1, a2, p1 and q11 must rst be determined.
Abu Hasan Abdullah 2009 43

(36)

(37) (38)

ODE - Initial Value Problem RK2 Methods


Recall second order Taylor series for yi+1 in terms of yi and f (xi, yi ) h2 yi+1 = yi + f (xi, yi)h + f (xi, yi ) 2!

(39)

where f (xi, yi), by chain-rule dierentiation, yields f (x, y) f (x, y) dy f (xi , yi) = + x y dx

(40)

Abu Hasan Abdullah

2009

44

ODE - Initial Value Problem RK2 Methods


Substituting Eq. (40) into Eq. (39) yields yi+1 = yi + f (xi, yi)h + f f dy h2 + x y dx 2! (41)

Abu Hasan Abdullah

2009

45

ODE - Initial Value Problem RK2 Methods


Taylor series for two-variable function is g(x + r, y + s) = g(x, y) + r and apply this to expand Eq. (38) f f + q11k1h + O(h2) f (xi + p1h, yi + q11k1h) = f (xi, yi) + p1h x y (42) g g + s + ... x y

Abu Hasan Abdullah

2009

46

ODE - Initial Value Problem RK2 Methods


Eqs. (42) and (37) are then substituted into Eq. (36) to yield yi+1 = yi + a1hf (xi, yi) + a2hf (xi , yi) + a2p1h2 f a2q11h f (xi, yi) + O(h3) y
2

f + x (43)

and collecting terms we get yi+1 = yi + a1f (xi, yi) + a2f (xi , yi) h+ f 2 f + a2q11f (xi, yi) h + O(h3) a2p1 x y
Abu Hasan Abdullah 2009

(44)
47

ODE - Initial Value Problem RK2 Methods


Comparing like terms in Eqs. (41) and (44), the following must hold a1 + a2 = 1 a2p1 = a2q11 =
1 2 1 2

We now have 3 simultaneous equations containing 4 unknown constants hence, theres no unique solution. However, assuming a value for one of the constants, we can determine the other 3! Consequently, there is a family of second order RK methods rather than a single version!
Abu Hasan Abdullah 2009 48

ODE - Initial Value Problem RK2 Methods


The second order RK method version of Eq. (28) is yi+1 = yi + (a1k1 + a2k2)h where k1 = f (xi, yi) k1 = f (xi + p1h, yi + q11k1h) a1 + a2 = 1 a2p1 = a2q11 =
Abu Hasan Abdullah 2009

(45)

(46) (47) (48) (49) (50)


49

1 2 1 2

ODE - Initial Value Problem RK2 Methods


Suppose we specify a value for a2, then Eqs. (48)(50) can be solved simultaneously for a1 = 1 a2 1 p1 = q11 = 2a2 (51) (52)

Abu Hasan Abdullah

2009

50

ODE - Initial Value Problem RK2 Methods


Heuns Method with a Single Corrector: a2 =
1 2 1 2 1 If a2 = 2 , Eqs. (51) and (52) can be solved for a1 = These parameters, when substituted into Eq. (45) yield 1 1 yi+1 = yi + ( 2 k1 + 2 k2)h

and p1 = q11 = 1.

(53)

where k1 = f (xi, yi ) k2 = f (xi + h, yi + k1h) : slope at begining of interval : slope at end of interval (54) (55)

Abu Hasan Abdullah

2009

51

ODE - Initial Value Problem RK2 Methods


The Midpoint Method: a2 = 1 If a2 = 1, then a1 = 0 and p1 = q11 =
1 2

and Eq. (45) becomes (56)

yi+1 = yi + k2h where k1 = f (xi, yi)


1 k2 = f (xi + 1 h, yi + 2 k1h) 2

(57) (58)

Abu Hasan Abdullah

2009

52

ODE - Initial Value Problem RK2 Methods


Ralstons Method: a2 =
2 3

Choosing a2 = 2 provides a minimum bound on the truncation error for second 3 order RK algorithms. It leads to a1 = 1 and p1 = q11 = 3 , and yields 3 4 yi+1 = yi + where k1 = f (xi, yi)
3 3 k2 = f (xi + 4 h, yi + 4 k1h) 1 3 k1 2 + 3 k2 h

(59)

(60) (61)

Abu Hasan Abdullah

2009

53

ODE - Initial Value Problem RK3 Methods


For n = 2, we can write, using Eq. (28), the third order RK (RK3) method as yi+1 = yi + (a1k1 + a2k2 + a3k3)h where k1 = f (xi, yi) k2 = f (xi + p1h, yi + q11k1h) k3 = f (xi + p2h, yi + q21k1h + q22k2h) (63) (64) (65) (62)

To use Eq. (62), values for a1, a2, a3, p1, p2, q11, q21 and q22 must rst be determined.
Abu Hasan Abdullah 2009 54

ODE - Initial Value Problem RK3 Methods


The popular one being yi+1 = yi + 1 (k1 + 4k2 + k3)h 6 where k1 = f (xi, yi )
1 k2 = f (xi + 2 h, yi + 1 hk1) 2

(66)

k3 = f (xi + h, yi hk1 + 2hk2) See Section 9.7.3 on page 658 of Rao (2002) for details.
Abu Hasan Abdullah 2009 55

ODE - Initial Value Problem RK4 Methods


The most popular RK methods are the fourth oder (RK4) with the most commonly used form being the classical fourth order RK (RK4) method
1 yi+1 = yi + 6 (k1 + 2k2 + 2k3 + k4)h

(67)

where k1 = f (xi, yi)


1 1 k2 = f (xi + 2 h, yi + 2 hk1) 1 k3 = f (xi + 1 h, yi + 2 hk2) 2

k4 = f (xi + h, yi + hk3)

Abu Hasan Abdullah

2009

56

ODE - Initial Value Problem RK4 MethodsExample #1


Problem Statement: Use the classical RK4 method to solve f (x) = dy = 2x3 + 12x2 20x + 8.5 dx

using step size of h = 0.5 and initial condition of y = 1 at x = 0.

Abu Hasan Abdullah

2009

57

ODE - Initial Value Problem RK4 MethodsExample #1


Solution:
At x = 0.0

3 2 k1 = 2(0.0) + 12(0.0) 20(0.0) + 8.5 = 8.5000 3 2 k2 = 2(0.0 + 0.5 0.5) + 12(0.0 + 0.5 0.5) 20(0.0 + 0.5 0.5) + 8.5 = 4.2188 3 2 k3 = 2(0.0 + 0.5 0.5) + 12(0.0 + 0.5 0.5) 20(0.0 + 0.5 0.5) + 8.5 = 4.2188 3 2 k4 = 2(0.0 + 0.5) + 12(0.0 + 0.5) 20(0.0 + 0.5) + 8.5 = 1.2500
Hence, estimate at x = 0.5 is

yi+1 = yi + 1 (k1 + 2k2 + 2k3 + k4 )h 6 1 y(0.5) = 1 + 8.5 + 2(4.21875) + 2(4.21875) + 1.25 0.5 = 3.2188 6

Abu Hasan Abdullah

2009

58

ODE - Initial Value Problem RK4 MethodsExample #1

At x = 0.5

k1 = 2(0.5)3 + 12(0.5)2 20(0.5) + 8.5 = 1.25000 k2 = 2(0.5 + 0.5 0.5)3 + 12(0.0 + 0.5 0.5)2 20(0.0 + 0.5 0.5) + 8.5 = 0.5938 k3 = 2(0.0 + 0.5 0.5)3 + 12(0.0 + 0.5 0.5)2 20(0.0 + 0.5 0.5) + 8.5 = 0.5938 k4 = 2(0.5 + 0.5)3 + 12(0.5 + 0.5)2 20(0.5 + 0.5) + 8.5 = 1.50000
Hence, estimate at x = 1.0 is

1 yi+1 = yi + 6 (k1 + 2k2 + 2k3 + k4 )h 1 y(1.0) = 1 + 1.2500 + 2(0.5938) + 2(0.5938) 1.5000 0.5 = 3.0000 6

Abu Hasan Abdullah

2009

59

ODE - Initial Value Problem RK4 MethodsExample #2


Problem Statement: Use the classical RK4 method to solve f (x, y) = dy = 4e0.8x 0.5y dx

from x = 0 to x = 0.5 with step size of 0.5 and initial condition of y(0) = 2.

Abu Hasan Abdullah

2009

60

ODE - Initial Value Problem RK4 MethodsExample #2


Solution: Using Matlab to solve the same involves the following steps: 1. Write the ODE into dydx1.m function df=dydx1(x,y) df = 4*exp(0.8*x) - 0.5*y;

2. Optionally, write the known solution into ytrue1.m function y=ytrue1(x) y = (4/1.3)*(exp(0.8*x) - exp(-0.5*x)) + 2*exp(-0.5*x);

Abu Hasan Abdullah

2009

61

ODE - Initial Value Problem RK4 MethodsExample #2


3. Matlab script to solve the ODE is written in myrk4xy.m x0 = 0.0; xn = 0.5; h=0.05; x = [x0:h:xn]; n = length(x); % yt = ytrue1(x); plot(x,yt,r); hold on %

Abu Hasan Abdullah

2009

62

ODE - Initial Value Problem RK4 MethodsExample #2


x(1) = 0.0; y(1) = 0.5; % INITIAL CONDITION for i=2:n k1 = dydx1(x(i-1),y(i-1)); k2 = dydx1(x(i-1)+0.5*h,y(i-1)+0.5*k1*h); k3 = dydx1(x(i-1)+0.5*h,y(i-1)+0.5*k2*h); k4 = dydx1(x(i-1)+h,y(i-1)+k3*h); phi = (k1+2*k2+2*k3+k4)/6; y(i) = y(i-1) + phi*h; end

Abu Hasan Abdullah

2009

63

ODE - Initial Value Problem RK4 MethodsExample #2


err = abs((yt-y)./yt)*100; plot(x,y,o) display(x y yt err) [x y yt err] [x y]

Abu Hasan Abdullah

2009

64

ODE - Initial Value Problem Example #3 - Using Matlab Built-in Function


Problem Statement: Use built-in Matlab function ode23 to numerically solve rst order ODE dy = xy 2 + y f (x, y) = dx Initial condition of at x = 0 is y = 1.

Abu Hasan Abdullah

2009

65

ODE - Initial Value Problem Example #3 - Using Matlab Built-in Function


Solution: Using Matlab to solve the same involves the following steps: 1. Write the ODE into myode1.m function df=myode1(x,y) % Computes yprime number yprime = x*y.^2 + y; % 2. Matlab script to solve the above ODE and plotting its solutions may take the following form: xr = [0,0.5]; % set x-range y0 = 1; % initial conditions [x,y] = ode23(myode1,xr,y0) plot(x,y)
Abu Hasan Abdullah 2009 66

ODE - Initial Value Problem Example #4 - Using Matlab Built-in Function


Problem Statement: Use built-in Matlab function ode45 to solve the rst order ODE dy = y + t + 1 f (t, y) = dt Initial condition of at x = 0 is y = 1.

Abu Hasan Abdullah

2009

67

ODE - Initial Value Problem Example #4 - Using Matlab Built-in Function


Solution: Using Matlab to solve the above involves the following steps: 1. Write the ODE into iv1.m function ydot=iv1(t,y) ydot = -y+t+1; % 2. Matlab script to solve the above ODE and plotting its solutions may take the following form: tspan = [0,1.0]; % set time span y0 = 1; % initial condition [t,y] = ode45(iv1,tspan,y0) plot(t,y)
Abu Hasan Abdullah 2009 68

ODE - Initial Value Problem Problems of Order n


We will show this kind of problem through an example: d2 x dx +3 +x=5 dt2 dt This is nth order problem where, in this case n = 2. It can be re-written as a system of n rst order ODE. This can be done in the following steps:

Abu Hasan Abdullah

2009

69

ODE - Initial Value Problem Problems of Order n


1. Let x1 = x, then d2x1 dx1 + x1 = 5 +3 dt2 dt or, re-written as dx1 d2x1 x1 + 5 = 3 dt2 dt

(68)

Abu Hasan Abdullah

2009

70

ODE - Initial Value Problem Problems of Order n


2. Next, let dx1 = x2 dt This is the rst equation of the system. 3. Dierentiate x2 and substitute it into Eq. (69) dx1 dx2 d2x1 = x1 + 5 = 3x2 x1 + 5 = 3 dt dt2 dt This is the second equation of the system.
Abu Hasan Abdullah 2009 71

(69)

(70)

ODE - Initial Value Problem Problems of Order n

4. System of ODE represented by the rst order Eqs. (69) and (70) can be then be written into a user-dened function in Matlab called, say iv0.m, as shown below function ydot=iv0(t,y) y1dot = y(2); y2dot = -3*y(2) - y(1) + 5; ydot = [y1dot;y2dot];

Abu Hasan Abdullah

2009

72

ODE - Initial Value Problem Problems of Order n - Example #1


Problem Statement: Use built-in Matlab function(s) to solve the classic van de Pol equation x (1 x2)x + x = 0 where is a parameter greater than zero.

Abu Hasan Abdullah

2009

73

ODE - Initial Value Problem Problems of Order n - Example #1


Solution: Choose y1 = x y2 = then y1 = y2
2 y2 = (1 y1 )y2 y1

dx dt

Abu Hasan Abdullah

2009

74

ODE - Initial Value Problem Problems of Order n - Example #1


Using Matlab to solve the above involves the following steps: 1. Write the system of rst order ODE into vdpol.m function ydot=vdpol(t,y) mu = 2; % set to a value greater than zero y1dot = y(2); y2dot = mu*(1-y(1)^2)*y(2)-y(1) ydot = [y1dot; y2dot];

Abu Hasan Abdullah

2009

75

ODE - Initial Value Problem Problems of Order n - Example #1

2. Matlab script to solve the above ODE and plotting its solutions may take the following form: % callvdpol.m -- program to call vdpol.m function to solve % the classic van der Pol equation tspan=[0 20]; % time span yo = [2; 0]; % initial condition [t,y] = ode45(vdpol,tspan,yo); % solve the ODE-IVP plot(t,y(:,1),t,y(:,2),--) xlabel(time) ylabel(y1dot and y2dot) title(van der Pol Solution)
Abu Hasan Abdullah 2009 76

ODE - Initial Value Problem Problems of Order n - Example #2


Problem Statement: Use built-in Matlab function(s) to solve the following third order ODE d2 y dy d3 y + 3 2 + 5 + 7y = 6 dt3 dt dt This is nth order problem with n = 3. It can be re-written as a system of 3 rst order ODE.

Abu Hasan Abdullah

2009

77

ODE - Initial Value Problem Problems of Order n - Example #2


Solution: 1. Let x1 = y, then d3x1 d2x1 dx1 + 7x1 = 6 +3 2 +5 dt3 dt dt or, re-written as d3x1 d2x1 dx1 7x1 + 6 = 3 2 5 dt3 dt dt (71)

Abu Hasan Abdullah

2009

78

ODE - Initial Value Problem Problems of Order n - Example #2


2. Let dx1 = x2 dt This is the rst equation of the system. Dierentiate x2, twice dx2 d2x1 = dt dt2 d2x2 d3x1 = dt2 dt3 (73a) (72)

(73b)

Abu Hasan Abdullah

2009

79

ODE - Initial Value Problem Problems of Order n - Example #2


3. Let dx2 = x3 dt This is the second equation of the system. But from Eq. (73a) dx2 d2x1 = dt dt2 Thus d2x1 x3 = dt2
Abu Hasan Abdullah 2009

(74)

80

ODE - Initial Value Problem Problems of Order n - Example #2


4. Dierentiate x3 dx3 d3x1 = dt dt3 (75)

Subsituting Eqs. (75), (74) and (72) into Eq. (71) and making substitution dx3/dt = d3x1/dt3, etc. yields dx3 = 3x3 5x2 7x1 + 6 dt This is the third equation of the system. (76)

Abu Hasan Abdullah

2009

81

ODE - Initial Value Problem Problems of Order n - Example #2


Thus, the third order ODE d2 y dy d3 y + 3 2 + 5 + 7y = 6 dt3 dt dt can be re-written as a system of 3 rst order ODE: dx1 = x2; dt dx2 = x3; dt dx3 = 3x3 5x2 7x1 + 6 dt

Abu Hasan Abdullah

2009

82

ODE - Initial Value Problem Problems of Order n


Written in a Matlab user-dened function called, say iv1.m, it may look like: function ydot=iv1(t,x) x1dot = x(2); x2dot = x(3); x3dot = -3*x(3) - 5*x(2) - 7*x(1) + 6; xdot = [x1dot;x2dot;x3dot];

Abu Hasan Abdullah

2009

83

IVP and BVP: The Dierences


In an initial value problem (IVP), all conditions are specied at the same value of the independent variable, and for nth order ODE, n conditions are required. ODE - Boundary Value Problem (ODE-BVP) are dened when the auxiliary conditions are NOT known at a single value but rather at many dierent values of the independent variable, because these values are often specied at the extreme points or boundaries of a system, they are referred to as ODE-BVP.

Abu Hasan Abdullah

2009

84

ODE - Boundary Value Problem Classication


In an ODE-BVP, for example, d2 y = f (x, y) 2 dx (77)

conditions are specied at the dierent values of the independent variable; in this case at x = 0 and x = L, where y = y0 y = yL @ x=0 @ x=L

Abu Hasan Abdullah

2009

85

ODE - Boundary Value Problem Methods of Numerical Solution


ODE-BVP may be classied into linear or nonlinear, separated or mixed, specied at two points or more. Two general approaches to numerical solution of ODE-BVP are shooting method, nite-dierence method

Abu Hasan Abdullah

2009

86

ODE - Boundary Value Problem Methods of Numerical Solution


Take conservation of energy to develop a heat balance for a long, thin rod. Mathematical model is represented by d2 T + h(Ta T ) = 0 2 dx where h : heat transfer coecient Ta : temperature of surrounding air (78)

Abu Hasan Abdullah

2009

87

ODE - Boundary Value Problem Methods of Numerical Solution


To solve Eq. (78), appropriate boundary conditions (BC) must be specied. They are T (0) = T1 T (L) = T2 With these two BC, Eq. (78) can be solved analytically using calculus. If L = 10 m, Ta = 20C, T1 = 40C, T2 = 200C and h = 0.01, the analytical solution is T = 73.4523e0.1x 53.4523e0.1x + 20 (79)

Abu Hasan Abdullah

2009

88

ODE - Boundary Value Problem Methods of Numerical Solution Shooting Method


Based on converting BVP into equivalent IVP. Trial-and-error approach is then implemented to solve IVP. Problem Statement: As an example, solve Eq. (78) d2 T + h(Ta T ) = 0 dx2 for a 10 m rod with h = 0.001, Ta = 20 and BC: T (0) = 40 and T (10) = 200

Abu Hasan Abdullah

2009

89

ODE - Boundary Value Problem Methods of Numerical Solution Shooting Method


Solution: Transform the second order ODE of Eq. (78) into two rst order ODE Solve Eq. (78) dT =z dx dz = h(T Ta) dx We need initial value for z to solve this system of ODE. (80a) (80b)

Abu Hasan Abdullah

2009

90

ODE - Boundary Value Problem Boundary Conditions


Consider a simple example of second order BVP d2y(x) =f 2 dx with boundary conditions at x = a; at x = b; y= y= x, y, dy ; dx axb (81)

Abu Hasan Abdullah

2009

91

ODE - Boundary Value Problem Boundary Conditions


Sometimes the BCs are specied in mixed form at C1yx=a + C2 dyx=a = C3 dx dyx=b = C6 at C4yx=b + C5 dx

where C1, C2, . . . C6 are constants.

Abu Hasan Abdullah

2009

92

ODE - Boundary Value Problem Engineering Applications


Transverse deection of beam, (x), subjected to distributed load, p(x) d2 d2 EI 2 2 dx dx = p(x) (82)

A cooling n extending from a hot furnace, assuming heat ows only in x-direction d2 T kA 2 hP (T T) = 0 dx (83)

Abu Hasan Abdullah

2009

93

ODE - Boundary Value Problem Solution Methods


Two methods are commonly used to solve ODE-BVP 1. Shooting Method for linear/nonlinear problems easy to implement no guarantee to converge, but convergence fast if it does! 2. Finite Dierence Method nite dierence of ODE is obtained at a number of mesh points in the interval of integration not necessary to convert nth order ODE to n rst order ODE

Abu Hasan Abdullah

2009

94

ODE - Boundary Value Problem Solution by Shooting Method

Abu Hasan Abdullah

2009

95

You might also like