8
Module1: Numerical Solution of Ordinary Differential Equations Lecture 2 Numerical Methods: Euler method Keywords: Numerical solution, grid points, local truncation error, rounding error

Lecture 2

Embed Size (px)

Citation preview

Page 1: Lecture 2

Module1: Numerical Solution of Ordinary Differential Equations

Lecture 2

Numerical Methods: Euler method

Keywords: Numerical solution, grid points, local truncation error, rounding error

Page 2: Lecture 2

Numerical Solution

Numerical methods for solving ordinary differential equations are more popular due to

several reasons:

More computational efforts are involved in Picard and Taylor series methods for

complex real life applications

Easy availability of computers

The numerical methods can still be applied in cases where the closed form

expression for the function is not available, but the values of function f are known

at finitely many discrete points. The analytical methods are not applicable.

For example, the velocity of a particle is measured at given points and one is interested

to predict the position of particle at some times in future. In such cases the analytical

methods cannot be applied and one has to obtain solution by numerical methods.

In this lecture a very basic method known as Euler method is being discussed. The

method is illustrated with an example.

Euler method:

When initial value problem (1.2) is solved numerically, the numeric values of the

solution y=g(t) are obtained at finitely many (say n) discrete points in the interval of

interest. Let these n points be equi-spaced in the interval [t0, b] as 1 2 nt , t ,...,t such that

0t kh, k 1,2,...n .  These points are known as grid points. Here the step size h is

computed as 0b th

n

. The numeric value of the solution is known at t=t0. The

approximate numeric value yk of the solution at kth grid point t=tk is an approximation to

the exact solution y(tk) of IVP. The Euler method specifies the formula for computing the

solution:

Page 3: Lecture 2

k 1 k k ky y h f(t ,y ); k 0,1,2,...n 1 (1.5)

Fig1.1 Schematic Diagram for Euler Method

The Euler formula (1.5) is a one step difference formula. The solution obtained by this

formula is shown on the computational grid in figure 1.1.

Observe that 0 0 0f(t,y ); y(t ) y is the slope of the solution curve at t=t0. The solution is

approximated as a straight line passing through y (t0) = y0 having slope f(t0, y(t0)). The

actual solution y(t) (shown in blue) may not be a straight line and y(t1) may be different

than y1 computed by the formula (1.5). It is only an approximation to the exact solution.

Starting from this approximation y1 at t1, the solution at next grid point t2 can be

approximated as y2 using (1.5). This is further continued for other grid points.

The actual solution curve may be above/ below the approximated solution.

Accordingly, the algorithm for computing solution using Euler method is given below:

Algorithm 1 For numerical solution by Eulers method

Step 0 [initialization] k=0, h=(b-t0 )/n , y(tk)=yk,

Step 1 [computation] k 1 k k ky y h f(t ,y )

Step 2 [increment] tk+1=tk+h, k=k+1

t1 

y(t) 

t t2  tn‐1  tn 

y1 

y2 

y0 

Page 4: Lecture 2

Step 3 [check for continuation] If k< n go to step 1

Step 4 [termination] stop

Example 1.3: Solve the initial value problem using the above algorithm

3y t y; y(0) 1

The IVP is solved first for step size h=1. The solution is obtained at t=1, 2 and 3.

The computations are performed using MS-Excel diff-euler1.xls [See columns B and C.

The column D gives the truncation error.] Note that the equation can be solved exactly.

Its exact solution is y(t) 4exp( t / 3) t 3 , y(3.0)=1.471518.

Next, the same problem is solved with step size h=0.5 up to t=3. The solution is

obtained successively at t=0.5, 1.0, 1.5, 2.0, 2.5, 3.0. [See columns E and F of the same

MS-Excel sheet. The column G gives the error.]

Comparing y computed at t=3.0 by two different step sizes, it is observed that solution

with smaller step size is closer to exact solution.

The computations are repeated with step size h=0.25 and 0.125 also. [See the excel

sheet columns H to M. The column O of the sheet gives exact solution at grid points

with h=0.125].

The table 1.1 shows the application of Euler Method for h=0.125. The attached graph

shows that the difference between the exact solution and the solution obtained by Euler

method with h=0.125. The following conclusions can be drawn:

i) As step size decreases, the computational effort increases

ii) The accuracy of the approximate solution increases with decreasing step size

Page 5: Lecture 2

tk yk exact sol error

0 1 1 0

0.125 0.9583333 0.961758 -0.00342

0.25 0.9236111 0.930178 -0.01351

0.375 0.895544 0.904988 -0.00944

0.5 0.8738546 0.885927 -0.01207

0.625 0.8582774 0.872745 -0.01447

0.75 0.8485575 0.865203 -0.01665

0.875 0.8444509 0.86307 -0.01862

1 0.8457238 0.866125 -0.0204

1.125 0.852152 0.874157 -0.02201

1.25 0.8635206 0.886963 -0.02344

1.375 0.8796239 0.904347 -0.02472

1.5 0.9002646 0.926123 -0.02586

1.625 0.9252536 0.952111 -0.02686

1.75 0.9544097 0.982141 -0.02773

1.875 0.9875593 1.016046 -0.02849

2 1.024536 1.053668 -0.02913

2.125 1.0651803 1.094857 -0.02968

2.25 1.1093395 1.139466 -0.03013

2.375 1.156867 1.187356 -0.03049

2.5 1.2076225 1.238393 -0.03077

2.625 1.2614716 1.292448 -0.03098

2.75 1.3182853 1.349399 -0.03111

2.875 1.3779401 1.409126 -0.03119

3 1.4403176 1.471518 -0.0312

Table1.1: Solution of Example 1.3 with h=0.125

Observe that the error is increasing with t.

Page 6: Lecture 2

Fig 1.2: Comparison with exact solution (Example 1.3)

For the derivation of Euler formula, consider the finite difference approximation of the

derivative

k 1 k

kt t

y ydyhdx

Also the approximate value of the function f(t,y(t) is computed as

k k f(t,y(t)) f(t , y )

Substitution in the differential equation gives the Euler formula

k 1 k k ky y hf(t ,y )

When k=0, the right side of the formula can be computed from known initial value y0.

Once y1 is computed, other yk, k=2, 3, 4,… can be computed successively in the similar

manner.

Analysis :

The Taylors Theorem gives

2

k k k k k

hy(t h) y(t ) hy (t ) y ( ), (t ,t h)

2

yk

exact sol

Page 7: Lecture 2

Substituting the derivative from the differential equation and neglecting second order

terms of the Taylor theorem gives Euler formula which is an approximation of the

solution at next grid point:

 k k k k

k 1 k k k

y(t h) y(t ) h f(t ,y(t ))

y y hf(t ,y )

 

Starting from the initial condition at t0, the approximate solution y1 at t1 computed by

Euler method has error due to following reasons:

i) The solution is assumed to be of constant slope equal to f(t0,y0) in the interval

(t0,t1)

ii) Rounding errors in numerical computation of the formula.

The local truncation error k 1 k k 1T y t h y is the difference between the exact

solution and approximate solution as obtained by the numerical method assuming the

solution is exact at kth step.

k 1 k k 1

2

k 1 k k k k k

2

k 1 k k k k k k

T y t h y

hT y(t ) h y (t ) y ( ) [y h f(t ,y )]

2

hT y(t ) y h[f(t ,y(t )) f(t ,y )] y ( )

2

Using the inequalities

k k k k k kf(t ,y(t ) f(t ,y ) L (y(t ) y ) and y ( ) M

the above expression for truncation error is simplified to

2

k 1 k

hT (1 hL) T M

2

For y1, the initial condition y0 is assumed to be the correct solution; hence the local

truncation error is of order h2.

Page 8: Lecture 2

However, the solution at t2 has one more source of error and that is approximate value

of the solution y1 at t1 as computed in earlier step. This error is further accumulated as

solution is advanced to more grid points tk, k=3, 4,… . The accumulation of error is

evident from the fig. 1.1 also. The Final Global Error (F.G.E) in computing the final value

at the end of the interval (a,b); a= t0, b=to+Mh is accumulated error in M steps and is of

order h. This means that the error E(y(b),h) in computing y(b) using step size h is

approximated as

( ( ), )E y B h Ch

Accordingly, ( ( ), / ) /E y B h 2 Ch 2

Therefore, halving the step size will half the FGE. FGE gives an estimate of

computational effort required to obtain an approximation to exact solution.

Repeated application gives

k 1

k 1

MhT 1 hL 1

2L

Using m mx0 (1 x) e ; x 1 gives overall truncation error of order h:

( )

k 1 hLk 1

MhT e 1

2L

Apart from the Euler method, there are numerous numerical methods for solving IVP. In

the next couple of lectures more methods are discussed. These are not exhaustive. The

selection of methods is based on the fact that these are generally used by scientists and

engineers in solving IVP because of their simplicity. Also, more complex techniques are

the combination of one or more of these and their development is on similar lines.