0% found this document useful (0 votes)
14 views

Chapter 4 Numerical Methods2022

Uploaded by

Nizam Azhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Chapter 4 Numerical Methods2022

Uploaded by

Nizam Azhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 4 Numerical Methods

4.1 Trapezoidal Rule


b
This rule is to approximate the value of ∫ a
f (x) dx oftenly if the integral cannot be
evaluated by all the techniques that we learnt. We state the rule here.

Trapezoidal Rule

Let f be continuous on [a,b]. Then

b b_a
∫a f (x)dx ≈ 2n [ f (x0 ) + 2 f (x1 ) + 2 f (x2 ) + .... + 2 f (xn _ 1 ) + f (xn )]
b_a
where xk = a + k for k = 0, 1, 2, …, n.
n
b
As n → ∞ the right hand side will approach ∫a
f (x) dx .

Example 1

2
Use Trapezoidal Rule to approximate ∫x
0
3
dx using n = 8.
Check your answer using straight forward integration.

Solution
b_a 2
a = 0, b = 2 , n = 8. Δ= = = 0.25
n 8

We build a table for our ease

k xk f(xk) m
0 0 0 1
1 0.25 0.0156 2
2 0.5 0.1250 2
3 0.75 0.4219 2
4 1.0 1.000 2
5 1.25 1.9531 2
6 1.5 3.3750 2
7 1.75 5.3594 2
8 2.0 8.000 1

Total = 32.5
2 2
∫0 x 3 dx ≈ 2(8) [Total ] = 4.0625 Check : Exact ans is 4.

1
Example 2

Consider a plot of land with given coordinates. Estimate the area of the land by
Trapezoidal Rule.

0 1 2 3 4 5 6

Solution

Trapezoidal Rule ( Case of two functions )

Let f and g be continuous on [a,b].

b_a
∫ ( f (x) g(x))dx ≈
b
_
Then [Δ 0 + 2Δ 1 + 2Δ 2 + ... + 2Δ n _ 1 + Δ n ]
a 2n
where Δ k = f (x k ) _ g(x k ) k = 0,1,2,...., n and
b_a
xk = a + k for k = 0, 1, 2, …, n.
n

2
Example 3

∫ (4 x 3x )dx using n = 5. Check with the exact answer.


3
3 _
Estimate
1

Solution
b_ a
a = 1, b = 3 , n = 5 = 0.4 f(x) = 4x3 g(x) = 3x
n
k xk f(xk)-g(xk) m
0 1 1 1
1 1.4 6.776 2
2 1.8 17.928 2
3 2.2 35.992 2
4 2.6 62.504 2
5 3.0 99 1

Total = 346.4

2
∫ (4 x 3x )dx ≈
3
3 _
(Total ) = 69.28 Exact = 68.
1 2(5)

Notice that the error is 1.28. Should we use n > 5 then the estimation would be
better.

This Trapezoidal Rule of two functions can be used if we know the difference
between two ordinates.

Example 4

Consider a plot of land below with difference in ordinates given. Estimate the area
of the land.

3
4.2 Simpson’s Rule

Another estimation to the integral is Simpson’s Rule. It uses quadratic functions


based on three points. Generally the estimation is more accurate compared to
Trapezoidal Rule.

Simpson’s Rule

Let f be continuous on [a,b] and let n be even. Then

b b_a
∫a f (x)dx ≈ 3n [ f (x0 ) + 4 f (x1 ) + 2 f (x2 ) + 4 f (x3 ) + ... + 4 f (xn _ 1 ) + f (xn )]
b_a
where xk = a + k for k = 0, 1, 2, …, n.
n
b
As n → ∞ the right hand side will approach ∫a
f (x) dx .

Example 5

5
Estimate ∫ x ln x dx
2
using n = 6.

Solution

b_a
a=2 b=5 = 0.5
n

k xk f(xk) m
0 2 1.3863 1
1 2.5 2.2907 4
2 3.0 3.2958 2
3 3.5 4.3847 4
4 4.0 5.5452 2
5 4.5 6.7683 4
6 5.0 8.0472 1

Total = 80.8903
5 5_2
∫2 x ln x dx 3(6) (Total ) = 13.4817.

Exact ( using IBP ) = [ (x2/2)ln x - x2/4]25 = 13.4817.

4
Simpson’s Rule ( in case of two functions )

Let f and g be continuous on [a,b].

b_a
∫ ( f (x) g(x))dx ≈
b
_
Then [Δ 0 + 4Δ 1 + 2Δ 2 + ... + 4Δ n _ 1 + Δ n ]
a 3n
where Δ k = f (x k ) _ g(x k ) k = 0,1,2,...., n and
b_a
xk = a + k for k = 0, 1, 2, …, n.
n

Example 6

Rework Example 4 using Simpson’s Rule for the case of two functions.

5
4.3 Newton’s Method for Approximating Roots

In this section we will learn how to estimate roots of equations. Some equations
are quite easy to find the root such as 2x - 9 = 1 which will give the root x = 5 .
However many equations are difficult to find the roots for example 4sin x = x
Although the equation seems to be very simple but to find the root ( the x value ) is
difficult. We have no other alternative but to estimate.

There are many estimation methods of finding roots. One of them is Newton’s
Method.

Suppose x0 is an approximation root to f (x) = 0.


From First Principle Formula we have
f (x 0 + h) _ f (x 0 )
f ‘(x0) = lim .
h→0 h

f (x 0 + h) _ f (x 0 ) f (x 0 + h) _ f (x 0 )
This means f ‘(x0) ≈ or h ≈ when h is small.
h f '(x 0 )

If x0+h is a better approximation to the root, f(x0+h) ≈ 0.

_ f (x 0 ) _ f (x 0 )
So h ≈ . Therefore a better root is x0 + h ≈ x0 .
f '(x 0 ) f '(x 0 )

Newton’s Method for approximating roots

Consider in finding roots to the equation f(x) = 0 where f is differentiable.


f (x n )
Suppose xn is an approximate root. Then x n +1 = x n _ is a better
f '(x n )
approximation to the root provided f ‘(xn) is not too small compared to f (xn).

Before we go on giving example , let us look at a theorem which will ease us in


picking up candidates to the root of equations.

Theorem
Suppose f(x) is continuous on [a,b] and suppose f(a) > 0 (or < 0) and
f(b) < 0 ( or > 0) . Then there is a c in (a,b) such that f( c) = 0.

The above theorem which we gave without proof guarantees that f(x) = 0 has at
least a root if f is continuous on [a,b] provided f(a) and f(b) have different signs.

6
Example 7

Approximate a root to the equation ex = 3 - x .

Solution

First we rewrite the equation as ex + x - 3 = 0 . Let f (x) = ex + x - 3. By trying some


values of x we get f (1) = 0.718 > 0 and f (0)= -2 . By the above theorem since f
is continuous on [0,1] the root must be in (0,1) . Clearly f ‘(x) = ex + 1

Let the first root be x0 = 0.5 . Then f (0.5) = -.8513 and f ‘(0.5) = 2.6487
Putting into Newton’s Formula we get

_ .8513
x1 = 0.5 - = 0.8214.
2.6487

f (x1) = f (.8214) = .0951, so x1 is a better estimate. For even better estimate


we again use the formula to find x2 using x1.

0.0951
x2 = 0.8214 - = 0.7924.
3.2737

f (x2) = f (0.7924) = 0.0011 which is of course very near to 0.

Proceed to x3 with f ’( 0.7924) = 3.2087 we get

0.0011
x3 = 0.7924 - = 0.7921
3.2087

If we let our final root to be 0.792 we have given the answer correct to 3 dec.
places as the first three decimals of x2 and x3 are the same.

Example 8

Show that the equation 2sin x - x = 0 has a root between 1 and 2.


Use Newton’s Method to approximate the root of using first trial root x0 = 1.
Comment.

Solution

Let f (x) = 2sin x - x .


Then f (1) = 0.6829 and f (2) = -0.1814. Since f (1) and f (2) have different
signs, by the above theorem a root must exists between 1 and 2 .
f ‘(x) = 2cos x - 1

7
0.68290
So x1 = 1 - = -0.75509, which is out of range.
0.0806
Notice that f ‘(1) is too small compared to f (1) . Use trial root = 1.5 .

Example 9

1 a
To find a one can use the Mechanic Formula xn+1 = ( x n + ) , n =1,2,…
2 xn
Derive this formula using Newton’s Method. Use the formula above to estimate
10 correct to two dec. places.

4.4 Lagrange Interpolation

Oftenly we need to estimate a point between two known points, or three known
points etc. How can we do this ? One answer to this is interpolation. In this section
we will look into three types of interpolation : Linear Interpolation, Lagrange
Quadratic Interpolation and Lagrange Cubic Interpolation.

4.4.1 Linear Interpolation

Suppose two points are known P0(x0, y0) and P1(x1,y1) . For given x where x0
< x < x1 we wish to find P(x,y) .
Now to find the y value we can draw straight line joining P0 and P1 ( Fig 1 ) and
draw right angle triangle beneath it.

Let P(x,y) be the point we wish to seek and it is on the line between P0 and P1.

8
Fig 1

y _ y 0 y1 _ y 0
If m is the gradient then mP0P = mP0P1 ⇒ _ = _ .
x x 0 x1 x 0

y1 _ y 0 _ x _ x1 x _ x0
Thus ⇒ y = y 0 + _ (x x 0 ) = y 0 _ + y1 _ .
x1 x 0 x 0 x1 x1 x 0

The above formula can be written as y = �y L i i where


i =0

x _ x1 x _ x0
L0 = _ and L1 = = _ .
x 0 x1 x1 x 0

Linear Interpolation Formula

Suppose points P0(x0, y0) and P1(x1,y1) are known. Then for a given x ,
y can be estimated by

1 x _ x1 x _ x0
y =  y i Li where L0 = and L1 = = .
i 0 x 0 _ x1 x1 _ x 0

Example 10

Given P(2,5) and Q(9, 50) . Find the value of y if x = 6.

Solution

Here x0 = 2 y0 = 5 x1 = 9 y1 = 50.

6_9 6 _ 2 215
y = y0L0 + y1L1 = 5( _ ) + 50( _ ) = ≈ 30.714 .
2 9 9 2 7

9
4.4.2 Lagrange Quadratic Interpolation

Now suppose three points are known. Then we can find a quadratic function
through all these three points. The interpolation formula is just an extension to the
above formula.

Lagrange Quadratic Interpolation

Suppose points P0(x0,y0), P1(x1, y1) and P2(x2,y2) are known. Then for a given x,
y can be estimated by

2
y =  y i Li
i 0
x _ x1 x _ x 2 x _ x 0 x _ x2 x _ x 0 x _ x1
where L0 = ( _ )( _ ) L1 = ( _ )( _ ) L2 == ( _ )( _ )
x 0 x1 x 0 x 2 x1 x 0 x1 x 2 x 2 x 0 x 2 x1 .

Example 11

Given three points (1,17), (5,69) and (10,179). Estimate the value of y if x = 7.

Solution

x0 = 1 y0 = 17 x1 = 5 y1 = 69 x2 = 10 y2 = 179

7 _ 5 7 _ 10 7 _ 1 7 _ 10 7_1 7_5
y = y0L0 + y1L1 + y2L2 = 17( _ )( _ ) + 69( _ )( _ ) + 179( _ )( _ ) = 107.
1 5 1 10 5 1 5 10 10 1 10 5

10
Example 12

Given the following plotted points. Find a quadratic function which can
estimate the curve.

If we have four points then Lagrange Cubic Interpolation will be appropriate.

4.4.3 Lagrange Cubic Interpolation

Lagrange Cubic Interpolation

Suppose points P0(x0,y0), P1(x1, y1) , P2(x2,y2) and P3(x3,y3) are known. Then for a
given x, y can be estimated by
3
y =  y i Li
i 0
x _ x1 x _ x2 x _ x3 x _ x0 x _ x2 x _ x3
where L0 = ( _ )( _ )( _ ) L1 = ( _ )( _ )( _ )
x0 x1 x0 x2 x0 x2 x1 x0 x1 x2 x1 x3

x _ x0 x _ x1 x _ x3 x _ x0 x _ x1 x _ x2
L2 = ( _ )( _ )( _ ) L3 = ( _ )( _ )( _ )
x2 x0 x2 x1 x2 x3 x3 x0 x3 x1 x3 x2

11
Example 13

Given four points (1,6), (2,31), (5,454), (8, 1903) . Estimate the value of y when
x = 6.

Solution

x0 = 1 y0 = 6 x1 = 2 y1 = 31 x2 = 5 y2 = 454 x3 = 8 y3 = 1903

y = y0L0 + y1L1 + y2L2 + y3L3=


6_2 6_5 6_8 6_1 6_5 6_8 6_1 6_2 6_8 6_1 6_2 6_5
6( _ )( _ )( _ ) + 31( _ )( _ )( _ ) + 454( _ )( _ )( _ ) + 1903( _ )( _ )( _ )
1 2 1 5 1 8 2 1 2 5 2 8 5 1 5 2 5 8 8 1 8 2 8 5
= 791.

Example 14
The points (0,-23), (2,-25), (3,1) and (5,167) are on a cubic polynomial. Find the
polynomial.

By Lagrange we have y =

 _ _ _  _ _ _ _ _ _ _ _ _
  23( x 2 )( x 3)( x 5 )   25( x 0 )( x 3 )( x 5 )  1( x 0 )( x 2 )( x 5 )  167( x 0 )( x 2 )( x 3)
 0 _ 2 0 _ 3 0 _ 5  2_0 2_3 2_5 3_ 0 3_ 2 3_5 5_0 5_2 5_3

23 3 25 1 167 3
 (x  10 x 2  31x  30)  (x 3  8 x 2  15 x)  (x 3  7 x 2  10 x)  (x  5 x 2  6 x)
30 6 6 30

 2 x 3  x 2  7 x  23.

12
Numerical Solution To Non Linear First Order ODE

1. Euler Method

We assume the equation is in the form dy/dx = f(x,y) and


given y = y0 when x = x0 . Define the step size h for x.

Use the formula yi+1 = yi + f(xi, yi)h, xi+1 = xi + h


We seek the approximate value of y at xi . The smaller the h the more
accurate will be the approximation. But the computation will be more
laborious.

Before we go to non linear example we look first at the equation which can be
solved by methods studied.

Example

Solve using Euler’s Method the equation dy/dx = x + y/5 , y(0) = -3


(a) first with step size h = 1 on the interval [0,5]
(b) then with step size h = 0.2 on the interval [0,1]

Solution
The equation can be put into dy/dy - (1/5)y = x which is linear.
If we solve using the method studied we get y = 22ex/5 -5x - 25.
This is the exact solution to the equation.

h= 1:
With x0 = 0 y0 = -3 and f(x,y) = x + y/5
we have y1 = y0 +f(x0,y0)h
=-3+ f(0,-3)(1)=-3+(-0.6)= -3.6
y2 = y1 +f(x1,y1)h
= -3.6+f(1,-3.6)(1) = -3.32
y3 = y2 +f(x2,y2)h
= -3.32 + f(2, -3.32) = -1.984
y4 = y3 +f(x3,y3)h
= -1.984+f(3,-1.984)= 0.6192
y5 = y4 +f(x4,y4)h
= 0.6192 + f(4, 0.6192) = 4.7430.

13
h=0.2 :
y1 = y0 +f(x0,y0)h
=-3+ f(0,-3)(0.2)=-3+(-0.12)= -3.12
y2 = y1 +f(x1,y1)h
= -3.12+f(0.2,-3.12)(0.2) = -3.205
y3 = y2 +f(x2,y2)h
= -3.205 + f(0.4, -3.205)(0.2) = -3.253
y4 = y3 +f(x3,y3)h
= -3.253+f(0.6,-3.253)(0.2)= -3.263
y5 = y4 +f(x4,y4)h
= -3.263 + f(0.8, -3.263) = -3.234

Using computer programs we can generate the following table.

x Yapprox h= 0.2 Yapprox h= 0.02 Yapprox h= 0.002 Yactual


0 -3.000 -3.000 -3.000 -3.000
0.2 -3.120 -3.104 -3.102 -3.102
0.4 -3.205 -3.172 -3.168 -3.168
0.6 -3.253 -3.201 -3.196 -3.195
0.8 -3,263 -3.191 -3.184 -3.183
1 -3.234 -3.140 -3.130 -3.129

Example

Solve the non linear ODE : dy/dx = x + y1/2, where y(0)= 1. Use step size of
h = 0.5. Solve for y when x = 2.

14
Solution
x0 = 0 y0 = 1 f(x,y) = x + y1/2

Y1 = 1 + f(0,1)(0.5) = 1.5
Y2 = 1.5 + f(0.5,1.5)(0.5) = 2.3624
Y3 = 2.3624 + f(1.0, 2.3624)(0.5) = 3.6309
Y4 = 3.6309 + f(1.5,3.63091)(0.5) = 5.3336

We can improve the above solution by taking smaller h such as h = 0.02

2. Runge-Kutta Fourth Order Method

This is perhaps the most popular to many engineers and scientist. However it is
not very convenient like Euler’s Method.

Again we want to solve non linear dy/dx = f(x,y) , y(x0) = y0


We use yi+1 = yi + 1/6( k1 + 2k2 +2k3 + k4) h .
where k1 = f(xi,yi)
k2 = f(xi+h/2, yi+ 0.5k1h)
k3 = f(xi+h/2, yi+ 0.5k2h)
k4 = f(xi+h, yi+ k3h)
Define step size for h. xi+1 = xi + h.

We calculate all the values for k’s and pluck into the formula yi+1.
Note that y0, y1, y2,…. corresponding to x0, x1, x2,…..

Example

Repeat the last example and approximate y at x = 1.

Solution
x0 = 0 y0 = 1 f(x,y) = x + y1/2
k1 = f(x0,y0) = f(0,1)= 1
k2 = f(x0+h/2, y0+ 0.5k1h)= f(0+0.5/2, 1+ 0.5(1)0.5)
=f(.25,1.25)= 1.3680
k3 = f(x0+h/2, y0+ 0.5k2h)= f(0+0.5/2, 1+ 0.5(1.3680)0.5)
= f(.25,1.342)= 1.4084

15
k4 = f(x0+h, y0+ k3h) = f(0.5, 1+1.4084(.5))= f(0.5,1.7042)= 1.8055

y(0.5) = 1 + 1/6( 1 + 2(1.3680 ) +2(1.4084)+ 1.8055)0.5 = 1.6965

x1 = 0.5 y1 = 1.6965 f(x,y) = x + y1/2


k1 = f(x1,y1) = f(0.5,1.6965)= 1.8025
k2 = f(x1+h/2, y1+ 0.5k1h)= f(0.5+0.5/2, 1.6965+ 0.5(1.8025)0.5))
=f(.75,2.1471)=2.2153

k3 = f(x1+h/2, y1+ 0.5k2h)= f(0.5+0.5/2, 1.6965 + 0.5(2.2153)0.5)


= f(.75,.2.2503)= 2.2500
k4 = f(x1+h, y1+ k3h) = f(1, 2.8215)= 2.6797

y(1) = 1.6965 + 1/6( 1.8025 + 2(2.2153 ) +2(2.2500)+ 2.6797)(0.5) = 2.8142

16

You might also like