Chapter 4 Numerical Methods2022
Chapter 4 Numerical Methods2022
Trapezoidal Rule
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
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
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
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
Simpson’s Rule
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.
≈
4
Simpson’s Rule ( in case of two functions )
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.
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 )
_ f (x 0 ) _ f (x 0 )
So h ≈ . Therefore a better root is x0 + h ≈ x0 .
f '(x 0 ) f '(x 0 )
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
Solution
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
0.0951
x2 = 0.8214 - = 0.7924.
3.2737
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
Solution
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.
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.
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
x _ x1 x _ x0
L0 = _ and L1 = = _ .
x 0 x1 x1 x 0
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
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.
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.
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
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
Before we go to non linear example we look first at the equation which can be
solved by methods studied.
Example
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
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
This is perhaps the most popular to many engineers and scientist. However it is
not very convenient like Euler’s Method.
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
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
16