Numerical Analysis 3rd Stage
Numerical Analysis 3rd Stage
Numerical Analysis 3rd Stage
Syllabus of Numerical Analysis
1. Roots of Equations
2. Solution of Linear Simultaneous Equations
3. Finite Difference, Interpolation and Extrapolation
4. Numerical Integration and Differentiation
5. Curve Fitting
6. Numerical Solution of Ordinary Differential Equations
7. Numerical Solution of Ordinary Partial Equations
8. Introduction to Finite Element Method.
1
1. Roots of Equations
Years ago, you learned to use the quadratic formula
−b ± b 2 − 4ac
x = (1)
2a
To solve
f ( x )= ax 2 + bx + c= 0 (2)
The values calculated with eq.(2) are called the "roots" of eq.(1). They represent the
vakues of x that make eq.(2) equal to zero.
Root of equation: The value of x that makes f(x)=0. The roots are sometimes
are called zeros of equation.
There are many other functions for which the root cannot be determined so easily
such as f(x) = e-x - x. For these cases the numerical methods efficient means to obtain
answer.
Before the advent of digital computers, there were several ways to solve for roots of
algebraic and transcendental equations. Such as Graphical technique method is to
plot the function and determine where it crosses the x-axis. This method is useful for
obtaining rough estimated of roots, this is limited because of their lack precision.
The other method is Trial and error. This method consists of guessing a value of x
and evaluating whether f(x) is zero. If not, another guess is made, and f(x) is again
evaluated to determine whether the new value provides a better estimate of the root.
f (x ) =
ln x 2 − 1, or f (x ) =
e x + sin(3x )
One of the first numerical methods developed to find the root of a nonlinear equation
f(x)=0 was the bisection method (also called binary-search method).
In general, an equation f(x)=0, where f(x) is a real continuous function, has at least
one root between xl and xu if f(xl)f(xu) < 0.
2
Algorithm for the bisection method
The steps to apply the bisection method to find the root of the equation f(x) = 0 are
1. Choose xl and xu as two guesses for the root such that f(xl)f(xu) < 0 , or in other
x l + xu
xm =
2
3. Now check the following:
a) If f(xl)f(xm) < 0, then the root lies between xl and xm; then xl = xl and xu = xm.
b) If f(xl)f(xm) > 0, then the root lies between xm and xu; then xl = xm and xu = xu.
c) If f(xl)f(xu) = 0 ; then the root is xm. Stop the algorithm if this is true.
4. Find the new estimate of the root
x l + xu
xm =
2
Find the absolute relative approximate error as
x mnew − x mold
=∈a × 100
x mnew
where
3
Ex.1
The floating ball has a specific gravity of 0.6 and has a radius of 5.5 cm. You are
asked to find the depth to which the ball is submerged when floating in water. The
equation that gives the depth x to which the ball is submerged under water is given
by:
Use the bisection method of finding roots of equations to find the depth x to which
the ball is submerged under water. Find the absolute relative approximate error at the
end of each iteration.
Sol.
From the physics of the problem, the ball would be submerged between x = 0 and
x = 2R, where, R = radius of the ball
that is
0 ≤ x ≤ 2R
0 ≤ x ≤ 2(0.055)
0 ≤ x ≤ 0.11
Assume xl = 0 , xu = 0.11
f (x u ) =
f ( 0.11) =
( 0.11) − 0.165 ( 0.11) + 3.993 × 10−4 =
−2.662 × 10−4
3 2
Hence
f ( x l ) f ( x u ) = f ( 0 ) f ( 0.11) = ( 3.99 × 10−4 )( −2.662 × 10−4 ) < 0
So there is at least one root between xl and xu, that is between 0 and 0.11.
Iteration 0
4
x l + x u 0 + 0.11
=xm = = 0.055
2 2
f ( x m ) = f ( 0.055) = ( 0.055)3 − 0.165 ( 0.055)2 + 3.993 × 10−4 = 6.655 × 10−5
f (x l )f (x m ) = f ( 0 ) f ( 0.055) = ( 3.993 × 10−4 )( 6.655 × 10−5 ) > 0
Hence the root is bracketed between xm and xu. So, The new bracket is
xl = 0.055 , xu = 0.11
Iteration 1
x l + x u 0.055 + 0.11
=xm = = 0.0825
2 2
f (x m ) =
f ( 0.0825) =
( 0.0825) − 0.165 ( 0.0825) + 3.993 × 10−4 =
−1.622 × 10−4
3 2
Hence the root is bracketed between xl and xm. So, The new bracket is
xl = 0.055 , xu = 0.0825
5
Iteration 2
x l + x u 0.055 + 0.0825
=xm = = 0.06875
2 2
f (x m ) =
f ( 0.06875) =
( 0.06875) − 0.165 ( 0.06875) + 3.993 × 10−4 =
−5.563 × 10−5
3 2
Hence the root is bracketed between xl and xm. So, The new bracket is
xl = 0.055 , xu = 0.06875
6
1.2. Iteration Method (Fixed point method) (open method)
We put x=g(x), assume initial value for xo then calculating next value as follows
x n +1 = g ( x n )
x new − x old x i +1 − x i
=∈a =× 100 × 100
x new x i +1
x = g1 ( x ) =
3
( x + 1) ,
1 2
⇒ x n +1 =
3
( x n + 1)
1 2
If Choose x0 = 1
If Choose x0 = 3
g1 ( x n )
∈a
N xn
g1 ( x n )
N xn ∈a
0 1 0.667 50%
0 3 3.33 10%
1 0.667 0.481 39%
1 3.33 4.037 17.5%
2 0.481 0.411 17%
2 4.037 5.766 30%
3 0.411 0.39 5.3%
3 5.766 11.415 50%
4 0.39 0.38 2.6%
4 11.415 Divergence
Convergence
7
If Choose x0 = 1 If Choose x0 = 3
g1 ( x n ) g1 ( x n )
N xn ∈a N xn ∈a
y=x
x0 = 3
x0 = 1
1 2 2
g 1 ( x )= ( x + 1) ⇒ g ′ ( x )= x
3 3
2 3
∴ g ′( x ) < 1 ⇒ x < 1 → x < to converges
3 2
8
1.3. Newton - Raphson Method
The Newton method is another iteration method for solving equation f(x) = 0, where
f(x) is assumed to have a continuous derivative f(x). The method commonly used
because of its simplicity and great speed.
f (x 0 )
′( x )
tan β f =
=
x 0 − x1
f (x 0 )
x= x0 −
f ′( x 0 )
1
f (x n )
x n= xn −
f ′( x n )
+1
f ( x ) = x 2 − 3x + 1 ⇒ f ′ ( x ) = 2x − 3
f (x n ) x n2 − 3x n + 1 2x n2 − 3x n − x n2 + 3x n − 1
∴ x n +1 =x n − =x n − =
f ′( x n ) 2x n − 3 2x n − 3
x n2 − 1
∴ x n +1 =
2x n − 3
If Choose x0 = 1 If Choose x0 = 3
N xn xn+1 ∈a N xn xn+1 ∈a
0 1 0 - 0 3 2.667 12.5%
1 0 0.333 100% 1 2.667 2.619 1.8%
2 0.333 0.381 12% 2 2.619 2.618 0.04%
3 0.381 0.381 0% 3 2.618 2.618 0%
Convergence Convergence
H.W. Write a computer program to solve the following equation using the above
three methods: Take xl = 0 and xu = 3 For Bisection method.
x
e −x − =
0 where N is your number
N
9
2. Solution of linear simulation equation
Where:
m = No. of equations
n = No. of variables (unknowns)
n
b − a x a .
i ∑
1. Put x=
i
j =1
ij j
ii
j ≠i
2. Assume values for xi , i = 2, 3, ... , n
3. Substitute in the equation to find new set of xi and continue till we get convergence
10
x ik +1 − x ik <∈ where ∈ rate of convergence (very small)
x 1 + 4x 2 − x 3 =
6 (1)
2x 1 − x 2 + x 3 =
3 (2)
− x 1 + 2x 2 + 4x 3 =
15 (3)
2x 1 − x 2 + x 3 =
3 (1)
x 1 + 4x 2 − x 3 =
6 (2)
− x 1 + 2x 2 + 4x 3 =
15 (3)
n 0 1 2 - - -
Eqs.
3+ x 2 − x3
x1 = - 1.5 0.25 ........ 1 1
2
6 − x1 + x 3
x2 = 0 1.125 2.34 ........ 2 2
4
15 + x 1 − 2x 2
x3 = 0 3.625 2.64 ........ 3 3
4
*Convergence at Max x ik +1 − x ik ≤ ∈
11
2.2 Gauss Elimination Method
Forward Elimination and Back substitution
Ax = b A: Coefficient matrix
−a21
multiply equ.(1) by and add to equ. (2)
a11
−a31
multiply equ.(1) by and add to equ. (3)
a11
′
−a32
multiply equ.(2) by and add to equ. (3)
′
a22
b3′′
x3 =
′′
a33
b ′ − a′ x
x 2 = 2 22 3
′
a22
b1 − a12 x 2 − a13x 3
x1 =
a11
12
Ex. Solve the system by using Gauss elimination method.
8x 2 + 2 x 3 =
−7 (1)
3x 1 + 5x 2 + 2x 3 =
8 (2)
6 x 1 + 2 x 2 + 8x 3 =
26 (3)
Note.: Pivoting, select the equation with maximum x1 coefficient to eliminate the
coefficient in the other equations. (swap eqs. (1), (2) & (3))
6 x 1 + 2 x 2 + 8x 3 =
26 (1)
3x 1 + 5x 2 + 2x 3 =
8 (2)
8x 2 + 2 x 3 =
−7 (3)
−3
Step (1) Eliminate x1 from eq.(2), by multiply * (1) + eq .(2)
6
6 2 8 x 1 26
3 5 2 x = 8
2
0 8 2 x 3 −7
-3/6 *(1) 6 2 8 26
=A =
A b 3 5 2 8 Pivot (6)
0 8 2 − 7
6 2 8 26
0 4 −2 − 5 Swap (2) & (3) Because 8 > 4
0 8 2 − 7
−4
Step (2) Elimination x2 from eq.(3) by multiply * (2) + eq .(3)
8
6 2 8 26 6 2 8 26
Pivot (8)
0 8 2 − 7 ⇒ 0 8 2 − 7
-4/8 *(2)
0 4 −2 − 5 0 0 −3 − 3 / 2
13
6 x 1 + 2 x 2 + 8x 3 =
26 (1)
8x 2 + 2 x 3 =
−7 (2)
− 3x 3 =
−3 / 2 (3)
1
x3 =
2
1
x 2 = ( −7 − 2x 3 ) =−1
8
1
x 1 = ( 26 − 2x 2 − 8x 3 ) = 4
6
Note: From smaller coefficient of equation can be multiply with factor and after the
solution divided it's on the same factor.
Note: Pivoting, selecting the equation with maximum coefficient to eliminate the
coefficient in the other equation.
Step (3) Backward elimination: eliminate all elements above the main diagonal.
2x − y + z =3 (1)
x + 4y − z =
6 (2)
− x + 2 y + 4z =
15 (3)
Sol.
-1/2 *eq(1) 2 −1 1 3 2 −1 1 3
1 4 −1 6 ⇒ 0 4.5 − 1.5 4.5
−1 2 15 0 1.5 16.5
-1/2 *(1)+eq(3) 4 -1.5/4.5 *(2)+eq(3) 4.5
14
2 −1 1 3
0 4.5 − 1.5 4.5
0 0 5 15
*Back elimination:
−1
multiply eq.(3) by and add to eq. (1)
5
−1.5
multiply eq.(3) by and add to eq. (2)
5
-1/5 *(3)+eq(1) 2 −1 1 3 2 −1 0 0
0 4.5 − 1.5 4.5 ⇒ 0 4.5 0 9
-1.5/5 *(3)+eq(2)
0 0 5 15 0 0 5 15
1
Step (4) eliminate x2 from eq.(1) by multiply eq. (2) by and add to eq. (1)
4.5
1/4.5 *eq(2)+eq(1) 2 −1 0 0 2 0 0 2
0 4.5 0 9 ⇒ 0 4.5 0 9
0 0 5 15 0 0 5 15
Step (5)
2 9 15
x= = 1 , y= = 2 , z= = 3
2 4.5 5
H.W. Write a computer program to solve N-simultaneous linear equation using Gauss
elimination and Gauss-Jordan method.
15
3. Finite Difference, Interpolation and Extrapolation
Finite Differences
1. Forward differences:
f(x)
f2
f1
∆f 0 = f 1 − f 0
f0
∆f 1 = f 2 − f 1 h
x0 x1 x2 x
∆f n = f n +1 − f n "1st Forward difference"
∆ 2f 0 =
∆ ( ∆f 0 ) = (f 2 − f 1 ) − (f 1 − f 0 )
∆f 1 − ∆f 0 =
=f 2 − 2f 1 + f 0
∆ 3f n = ∆ ( ∆ 2f n ) = ∆f n + 2 − 2∆f n +1 + ∆f n
= ( f n + 3 − f n + 2 ) − 2 ( f n + 2 − f n +1 ) + ( f n +1 − f n )
∴∆ 3=
f n f n + 3 − 3f n + 2 + 3f n +1 − f n
∆ 4f n = ∆ 3f n +1 − ∆ 3f n
Ex. Construct the forward difference table for the following data;
16
x 0 0.5 1 1.5 2.0
f(x) 1.2 1.8 3.1 4.6 7.6
n xn fn ∆f n ∆ 2f n ∆ 3f n ∆ 4f n
0 0 1.2 0.6 0.7 -0.5 1.8
1 0.5 1.8 1.3 0.2 1.3 -
2 1.0 3.1 1.5 1.5 - -
3 1.5 4.6 3.0 - - -
4 2.0 7.6 - - - -
∇f 1 = f 1 − f 0
=( f n − f n −1 ) − ( f n −1 − f n − 2 )
=
f n − 2f n −1 + f n − 2
∇ k f n = ∇ k −1f n − ∇ k −1f n −1
17
3. Central differences: "δ "
f2 −f0
δf1 = f(x)
2
Or f2
1 f1
δ f n= ( ∆ f n + ∇f n ) "1st Central"
f0
2
h h
= {( f n +1 − f n ) − ( f n − f n −1 )}
1
2 x0 x1 x2 x
1
∴δ f =
n ( f n +1 + f n −1 )
2
1
(δ f n )
δ 2f n δ=
= (δ f n +1 − δ f n −1 )
2
(δ f n ) (δ 2f n +1 − δ 2f n −1 )
1
δ 3f n δ=
= 2
δkf n
=
2
( δ f n +1 − δ k −1f n −1 )
1 k −1
H.W. Construct the central difference table for the data in previous example.
* Linear interpolation
f(x)
f1 −f 0 f −f 0
=
x1 − x 0 x − x 0 f1
(f − f 0 ) h =( x − x 0 ) ∆f 0
f
x −x0
f =
f0 + ∆f 0
h
f0
x −x0
r=
h x0 x x1 x
f = f 0 + r ∆f 0 "linear" h = x1 - x0
= xn+1 - xn "step"
18
** Quadratic interpolation
r ( r − 1) 2
f = f 0 + r ∆f 0 + ∆ f0
2
r ( r − 1) 2 r ( r − 1)( r − 2 ) 3 r ( r − 1)( r − 2 )( r − 3) 4
f = f 0 + r ∆f 0 + ∆ f0 + ∆ f0 + ∆ f +
2! 3! 4!
r ( r − 1) 2 r ( r + 1)( r + 2 ) 3 r ( r + 1)( r + 2 )( r + 3) 4
f = f 0 + r ∇f 0 + ∇ f0 + ∇ f0 + ∇ f +
2! 3! 4!
Ex. For the same data in last example, find f( - 0.5), f(1.25), f(2.75) using linear and
quadratic and 4-term Newton formula.
x 0 0.5 1 1.5 2.0
f(x) 1.2 1.8 3.1 4.6 7.6
Sol.
f( - 0.5) = ? extrapolation
x −x0
linear : f = f 0 + r ∆f 0 r=
h
−0.5
x0 = 0 , h = 0.5 "step" h = xn+1 - xn r= = −1
0.5
f ( −0.5)= 1.2 + ( −1)( 0.6 )= 0.6
r ( r − 1) 2
Quadratic : f = f 0 + r ∆f 0 + ∆ f0
2
f ( −0.5)= 1.2 + ( −1)( 0.6 ) +
( −1)( −2 ) * 0.7= 1.3
2
4-term Newton Forward formula
r ( r − 1) 2 r ( r − 1)( r − 2 ) 3
f = f 0 + r ∆f 0 + ∆ f0 + ∆ f0
2! 3!
4-term Newton formula Let x0 = 0.5 *Because x0=1 not found ∆3f0
x − x 0 1.25 − 0.5
=r = = 1.5
h 0.5
r ( r − 1) 2 r ( r − 1)( r − 2 ) 3
f = f 0 + r ∆f 0 + ∆ f0 + ∆ f0
2! 3!
1.5* 0.5 1.5* 0.5* ( −0.5)
f (1.25) =
1.8 + 1.5*1.3 + * 0.2 + *1.3 =
3.74
2 6
f( 2.7) = ? Let x0 = 2.0 Note: Using Backward Table because containing all terms
x − x 0 2.75 − 2
=r = = 1.5
h 0.5
linear : f ( 2.75) = f 0 + r ∇f 0 = 7.6 + 1.5* 3.0 = 12.1
Quadratic :
r ( r + 1) 2 1.5* 2.5
f ( 2.75) = f 0 + r ∇f 0 + ∇ f 0 = 7.6 + 1.5* 3.0 + *1.5 = 14.91
2 2
4-term Newton formula
r ( r + 1) 2 r ( r + 1)( r + 2 ) 3
f = f 0 + r ∇f 0 + ∇ f0 + ∇ f0
2! 3!
1.5* 2.5 1.5* 2.5* 3.5
f ( 2.75) =
7.6 + 1.5* 3.0 + *1.5 + *1.3 =
17.75
2 6
Note: Newton interpolation formula can be used only when the point are equally
spaced ( h = constant).
H.W. Write a computer program to interpolate f(x) using Newton forward and
backward formula using n-term.
20
Lagrange Interpolation formula
This method has less accuracy them Newton formula, but it has advantage of it
can be used for equally or non-equally spaced data.
n
f (x ) = ∑ N k ( x )f k
k =0
n (x − x ) n (x − x )
= ∏ x −x
= ∏ x −x
j j
N k (x )
( k j) j 0( k j)
=j 0=
j ≠k j ≠k
2 (x − x j ) (x − x 1) . (x − x 2 )
=N0 ∏ x −x
=
k =0 j =0 ( k j ) (x 0 − x 1) (x 0 − x 2 )
j ≠k
2 (x − x j ) (x − x 0 ) . (x − x 2 )
=N1 ∏=
k =1 j =0 ( x k − x j ) (x 1 − x 0 ) (x 1 − x 2 )
j ≠k
2 (x − x )
(x − x 0 ) . (x − x 1)
= ∏ x −x
=
j
N2
k =2 j =0 ( k j ) (x 2 − x 0 ) (x 2 − x 1 )
j ≠k
∴f ( x )
=
( x − x 1 ) . ( x − x 2 ) .f + ( x − x 0 ) . ( x − x 2 ) .f + ( x − x 0 ) . ( x − x 1 ) .f
(x 0 − x 1 ) (x 0 − x 2 ) 0 (x 1 − x 0 ) (x 1 − x 2 ) 1 (x 2 − x 0 ) (x 2 − x 1 ) 2
Ex. For the last example, find f(1.25) using 3-term Lagrange interpolation formula.
x0 x1 x2
x 0 0.5 1 1.5 2.0
x = 1.25
f(x) 1.2 1.8 3.1 4.6 7.6
n = No. of terms - 1 = 3 - 1 = 2 f0 f1 f2
2
f (x ) = ∑ N k ( x )f k = N 0 ( x )f 0 + N 1 ( x )f 1 + N 2 ( x )f 2
k =0
21
N0 =
( x − x 1 ) . ( x − x 2 ) = (1.25 − 1) . (1.25 − 1.5) = −0.125
k =0 ( x 0 − x 1 ) ( x 0 − x 2 ) ( 0.5 − 1) ( 0.5 − 1.5)
N1
(=x − x 0 ) ( x − x 2 ) (1.25 − 0.5) (1.25 − 1.5)
. = . 0.75
k =1 ( x 1 − x 0 ) ( x 1 − x 2 ) (1 − 0.5) (1 − 1.5)
N2
(=x − x 0 ) ( x − x 1 ) (1.25 − 0.5) (1.25 − 1)
. = . 0.375
k =2 ( x 2 − x 0 ) ( x 2 − x 1 ) (1.5 − 0.5) (1.5 − 1)
∴ f (1.25) =
−0.125*1.8 + 0.75* 3.1 + 0.375* 4.6 =
3.825
H.W. Write a general computer program to read points (xn, fn) then construct the
difference table up ∆n-1f, then estimate y(x) for any x using 4-term Newton and
Lagrange interpolation.
1. Trapezoidal Rule f3
b
f2
f1
∫f ( x )dx f0
a
22
f f
b
∫f ( x )dx =∆x a + f 1 + f 2 + f 3 + + b
2 2
a
2. Simpson Rule
f(x) fb
Note: No. of division (strips) should be even
f4
f3
∆x f2
A=
1 ( f 0 + 4f 1 + f 2 ) f1
3 f0
∆x
A=2 ( f 2 + 4f 3 + f 4 ) A1
A2
3
∆x a=x0 x1 x2 x3 x4 xn-1=b x
A=n ( f n −3 + 4f n −2 + f n −1 ) ∆x
3
b
∆x
∫f ( x )dx= [f 0 + 4f 1 + 2f 2 + 4f 3 + 2f 4 + + 2f n −3 + 4f n −2 + f n −1 ]
a 3
f 0= f a , f n= f b , x =
1 x 0 + ∆x , x 2= x 1 + ∆x
2
Ex. Evaluate I = ∫ e − x dx using trapezoidal & Simpson methods. take h = 0.5.
2
b −a 2−0
Sol. No. of division = = = 4
h 0.5
f n = e −x n
2
n xn
0 0 1
1 0.5 0.778 Trapezoidal Simpson
value Value
2 1.0 0.367
3 1.5 0.105
4 2.0 0.018
0.8795 0.88
f f
(1) Trapezoidal I t =∆x 0 + f 1 + f 2 + f 3 + 4
2 2
23
1 0.018
= 0.5 + 0.778 + 0.367 + 0.105 + = 0.8795
2 2
∆x
(2) Simpson I=
s [ f 0 + 4f 1 + 2f 2 + 4f 3 + f 4 ]
3
0.5
= [1 + 4 * 0.778 + 2 * 0.367 + 4 * 0.105 + 0.018] =0.88
3
H.W Write computer program to evaluate the following internal using trapezoidal,
Simpson methods; Take any number of division
2π
1
I = ∫x
2
sin 2 x 2dx
2π 0
1 2r − 1 2
f ′( x ) = ∆f + ∆ f 0 "Three points formula"
h
0
2!
24
(i) At x = x0 r=0
1 1 2 1
f ′( x 0 ) = f 0′ = ∆ f − ∆ f 0 ⇒ ∴ f 0′ = [ −3f 0 + 4f 1 − f 2 ]
h
0
2 2h
x1 − x 0
(ii) At x = x=
1 , r = 1
h
1 1 2 1
f ′( x 1 ) = f 1′ = ∆ f + ∆ f 0 ⇒ ∴ f 1′ = [f 2 − f 0 ]
h
0
2 2h
x 2 − x 0 2h
(iii) At x = x2 =
r = = 2
h h
1 3 2 1
f 2′= ∆ f + ∆ f 0 ⇒ ∴ f 2′= [f 0 − 4f 1 + 3f 2 ]
h
0
2 2h
Ex. Consider f(x) = x4 for x0 = 0, x1 = 0.2, x3 = 0.4, x4 = 0.6, and x4 = 0.8. Calculate
f 0′, f 1′, and f 2′ using three point formula.
n xn fn = x4n h = 0.2
1
0 0 0 f 0′ = [ −3f 0 + 4f 1 − f 2 ] =
2h
1 0.2 16×10-4
1
2 0.4 256×10-4 f=1
′ [f 2 − f 0=]
2h
3 0.6 1296×10-4 1
f=2
′ [f 0 − 4f 1 + 3f 2=]
4 0.8 4096×10-4 2h
f (x )
n (x − x )
n
∑
= ∏ x −x
j
N k ( x )f k , where N k ( x )
k 0=j 0
j ≠k
( k) j
25
5. Curve Fitting
In Curve Fitting we are given n points (pairs of numbers) (x1, y1),
(x2, y2),...,(xn, yn) and we want to determine a function f(x) approximately. The type
of function (for example, polynomials, exponential functions, sine and cosine
functions) may be suggested by the nature of the problem (the underlying physical
law, for instance), and in many cases a polynomial of a certain degree will be
appropriate.
f(x) y=f(x)
e 2 =[ y 2 − y ( x 2 )] = y 2 − (ax 2 + b )
2 2
e3
e2
e1
e n =[ y n − y ( x n )] = y n − (ax n + b )
2 2
x
n
1
∴Total error =
E= ∑
2 i =1
e i2 n: No. of points
1 n
∑ y i − (ax i + b ) Should be minimum.
2
=
∴E
2 i =1
26
∂E ∂E
= 0,= 0
∂a ∂b
∂E 1 n
∴
= ∑ 2. y i − (ax i + b ) * (=
∂a 2 i =1
−x i ) 0
n
∑ y i − (ax i + b ) x i =
0
i =1
y i x i ∑ (ax 2 + bx i )
n n
∑= i
divided both sides by n
=i 1=i 1
=
yx ax 2 + b x ...(1)
∂E 1 n
= ∑ 2. y i − (ax i + b )=
∂b 2 i =1
* ( −1) 0
n n
∑yi
= ∑ (ax i + b ) divided both sides by n
=i 1=i 1
=
y ax + b ... (2)
=
yx ax 2 + b x ...(1)
Ex. Find the best line representing the following points (0, 1), (1, 2), (2, 4), (3, 8),
(4, 10) and calculate the approximate errors.
Sol. n = 5
i xi yi yixi x i2 y(xi)=axi+b ei ei 2
1 0 1 0 0 0.2 0.8 0.64
2 1 2 2 1 2.6 -0.6 0.36
3 2 4 8 4 5.0 -1 1
4 3 8 24 9 7.4 0.6 0.36
5 4 10 40 16 9.8 0.2 0.04
∑ 10 25 74 30 - - 2.4
Avg. 2 5 14.8 6
27
yx − y x 14.8 − ( 5)( 2 )
=a = = 2.4
x 2 − ( x )2 6 − ( 2)
2
5 ( 2.4 )( 2.) =
b =y − ax =− 0.2
∴ y= 2.4x + 0.2
5.2 Polynomial fitting
In General
y ( x ) = am x m + am −1x m −1 + am − 2 x m − 2 + + a0
Ex. Find the best second degree curve representing the following points (0, 1), (1, 3),
(2, 5), (3, 11), (4, 15).
Sol.
y ( x ) = a2 x 2 + a1x + a0 ;n=5
i x y x2 x3 x4 yx yx2
1 0 1 0 0 0 0 0
2 1 3 1 1 1 3 3
3 2 5 4 8 16 10 20
4 3 11 9 27 81 33 99
5 4 15 16 64 256 60 240
28
yx 2 = a2 x 4 + a1 x 3 + a0 x 2 ⇒ 72.4 = 70.8 a2 + 20 a1 + 6 a0 (1)
yx = a2 x 3 + a1 x 2 + a0 x ⇒ 21.2 = 20 a2 + 6 a1 + 2 a0 ( 2 )
y = a2 x 2 + a1 x + a0 ⇒ 7= 6 a2 + 2 a1 + a0 ( 3)
Solve the above linear simultaneous equation using iteration or Gauss method to
find a2, a1, and a0
∴a =eA
Ex. Find the best exponential function representing the following points (1, 4.7),
(2, 9), (3, 25), (4, 80) and calculate the approximate errors.
Sol. n = 4
y = ae bx ⇒ ln y = ln a + bx ⇒Y = A + bx
=
where Y ln= y , A ln a
i x y Y=lny Yx x2 y=aebx ei ei 2
1 1 4.7 1.548 1.548 1 4.437 0.263 0.07
2 2 9 2.197 4.394 4
3 3 25 3.219 9.387 9
4 4 80 4.382 17.53 15
29
Y x −Y x 8.215 − 2.837 * 2.5
=b = = 0.8978
x 2 − ( x )2 7.5 − ( 2.5)
2
∴a =eA
H.W. For the same points in the last example, find the best power function? and
calculate the approximation error. Ans. y = 3.614 x1.954
H.W. Write a computer program to read n-points then ask for the kind of fitting
required, then perform the procedure of fitting and evaluate the total error
generated.
30
6. Numerical Solution of ODE
yn+1
Using Taylor series
yn
2 h
h
y n +1 =y n + hy n′ + y n′′ +
2! xn xn+1 x
For a small value of h, the highest powers h2, h3, .... are very small
y n +1 =y n + hy n′ =y n + hf ( x n , y n )
In the first step we compute
y=
1 y 0 + hf ( x 0 , y 0 )
In the second step we compute
y=
2 y 1 + hf ( x 1 , y 1 )
In general;
31
y n=
+1 y n + hf ( x n , y n ) n = 0, 1 , 2, ....
32
Step (2): Corrector
1
y n +1 =
yn + ( K n + Ln )
2
Hence the improved Euler method is a predictor–corrector method: In each
step we predict a value and then we correct it.
Ex. Solve the least example using modified Euler method.
y=' f ( x , y=
) 2x + 2 y , y(0) = 1, h = 0.1
=K n hf ( x n , y n ) = 0.1( 2x n + 2 y n )
L=
n hf ( x n + h , y n + K n )= 0.1 2 ( x n + 0.1) + 2 ( y n + K n )
1
y n + ( K n + Ln )
y n +1 =
2
K=
0 0.1( 2 * 0 + 2 *1=
) 0.2
L0 0.1 2 ( 0 + 0.1) + 2 (1 + 0.2=
= ) 0.26
1
1 + ( 0.2 + 0.26 ) =
y1 = 1.23
2
n xn yn Kn Ln yn+1
0 0 1 0.2 0.26 1.23
1 0.1 1.23 0.266 0.3392 1.533
2 0.2 1.533
3 0.3
4 0.4
5 0.5
6 0.6
7 0.7
8 0.8
9 0.9
10 1.0
33
(3) Runge - Kutta Method
A method of great practical importance and much greater accuracy than that of
the improved Euler method is the classical Runge–Kutta method of fourth order,
which we call briefly the Runge–Kutta method.
The following fourth order Runge-Kutta algorithm, with xn = x0 +nh.
Step (1): Calculate
A n = hf ( x n , y n )
h A
B n = hf x n + , y n + n
2 2
h B
C n = hf x n + , y n + n
2 2
D n = hf ( x n + h , y n + C n )
in the interval 0 ≤ x ≤ 1 .
Sol.
∴ y=′ f ( x , y=
) sin 3x − 2 y
=A n 0.2 * ( sin 3x n − 2 y n )
A
=B n 0.2 sin 3 ( x n + 0.1) − 2 y n + n
2
B
=C n 0.2 sin 3 ( x n + 0.1) − 2 y n + n
2
D n 0.2 [sin 3( x n + 0.2) − 2( y n + C n )]
=
34
0.2 * ( sin 3* 0 − 2 *1) =
A0 = −0.4
−0.4
0.2 sin 3 ( 0 + 0.1) − 2 1 +
B0 = = −0.2609
2
−0.2609
0.2 sin 3 ( 0 + 0.1) − 2 1 +
C0 = = −0.28872
2
0.2 [sin 3(0 + 0.2) − 2(1 − 0.28872)] =
D0 = −0.17158
1
∴ y 1 =1 + ( −0.4 ) + 2 ( −0.2609 ) + 2 ( −0.28872 ) + ( −0.17158) =0.72153
6
n xn yn An Bn Cn Dn yn+1
0 0 1 -0.4 -0.261 -0.288 -0.171 0.721
1 0.2 0.721 -0.175 -0.097 -0.112 -0.057 0.613
2 0.4 0.613 -0.058 -0.034 -0.038 -0.0348 0.573
3 0.6 0.573
4 0.8
5 1.0
H.W. Write a computer program to solve and first order differential equation using
Euler, modified Euler and Runge-Kutta methods.
35
h2 h3
y n +1 =y n + hy n′ + y n′′ + y n′′′ +
2! 3!
h2
y n +1 =y n + hy n′ + y n′′
2!
36
8 0.8
9 0.9
10 1.0
h K A A
B n = hf x n + , y n + , y n′ + n where =
K h y n′ + n
2 2 2 4
h K B
C n = hf x n + , y n + , y n′ + n
2 2 2
D n = hf ( x n + h , y n + L , y n′ + C n ) where =L h ( y n′ + C n )
K A A
B n 0.2 2 ( x n + 0.1) y n + + 4 y n′ + n where
= = K 0.2 y n′ + n
2 2 4
37
K Bn
C n 0.2 2 ( x n + 0.1) y n +
= + 4 y n′ +
2 2
Cn
D n 0.2 ( 2 ( x n + 0.2 )( y n + L ) + 4 ( y n′ + C n ) )
= where =L 0.2 y n′ +
2
A 0 0.2 [ 2 * 0 *1 + 4=
= *1] 0.8 0.8
K = 0.2 1 + = 0.24
4
0.24 0.8
=
B 0.2 2 ( 0 + 0.1) 1 + + 4 1 + = 1.165
2
n
2
0.24 1.165
=
C n 0.2 2 ( 0 + 0.1) 1 + + 4 1 + = 1.311
2 2
1.311
L =0.2 1 + =0.311
2
=
D n 0.2 2 ( 0 + 0.2 )(1 + 0.331) + 4 (1 + 1.311=
) 1.971
1
1 + 0.2 1 + ( 0.8 + 1.165 + 1.311) =
y1 = 1.309
6
1
1 + ( 0.8 + 2 *1.165 + 2 *1.311 + 1.971) =
y 1′ = 2.287
6
H.W. Write a computer program to solve any second order differential equation
using Euler and Runge-Kutta Nyström methods.
38
7. Numerical Solution of Partial Differential Equation
The PDEs serve as models for elliptic, parabolic, and hyperbolic. For example,
the Laplace equation is a representative example of an elliptic type of PDE, and so
forth.
A PDE is called quasilinear if it is linear in the highest derivatives. Hence a
second-order quasilinear PDE in two independent variables x, y is of the form;
Note:
∂u ∂ 2u
=ux = , u xx
F ( x , y ,u ,u x ,u y )
au xx + 2bu xy + cu yy = ∂x ∂x 2
∂u ∂ 2u
=uy = , u yy
∂y ∂y 2
∂ 2u
u is the unknown function. This equation is said to be of; u xy =
∂x ∂y
39
Subtract Eq.(2) from Eq.(1), neglect terms in h2, h3, ...,
u x (x , y ) ≈
1
2h
[u ( x + h , y ) − u ( x − h , y )] ⇒ u x=
j ,i
1
2h
(u i +1, j − u i −1, j )
Similarly
u y (x , y ) ≈
1
2k
[u ( x , y + k ) − u ( x , y − k )] ⇒ u=
y j ,i
1
2k
(u i , j +1 − u i , j +1 )
(x,y+k)
i,j+1
k k
h h h h
(x-h,y) (x+h,y)
(x,y) i-1,j i,j i+1,j
k k
i,j-1
(x,y-k)
1
u yy ( x , y ) ≈ [u ( x + k , y ) − 2u ( x , y ) + u ( x − k , y )]
k2
1
u xx = u i , j +1 − 2u i , j + u i , j −1 (5)
i,j
k2
Substitute Eqs. (4) &(5) into Laplace Equation, get;
40
1 1
u − 2u + u
k 2 u i , j +1 − 2u i , j + u i , j −1 =
+ 0
h2
i +1, j i , j i −1, j
* If h = k
u i +1, j + u i −1, j + u i , j +1 + u i , j −1 − 4u i , j =
0
u i +1, j + u i −1, j + u i , j +1 + u i , j −1
∴u i , j =
4
Note:
i, j+1 1
k (Called Stencil, pattern,
1 −4 1 molecule, or star)
h
i-1, j i+1, j
i, j
1
i, j-1
1
1 −4 1 u =0
1
Ex. The four sides of a square plate of side 12 cm made of homogeneous material are
kept at constant temperature 0 ºC and 100 ºC as shown figure. Using a grid mesh
4 cm. Find the (steady state) temperature at the mesh point.
y
0ºC 1,3 3,2
0,3 3,3
12
u=
0,1 u=
0,1 100 0C
u=
3,1 u=
3,2 100 oC
At point (1,1)
4u1,1 = u 2,1 + u 0,1 + u1,2 + u1,0 ⇒ 4u1,1 = u 2,1 + 100 + u1,2 + 100
4u 2,1 = u 3,1 + u1,1 + u 2,2 + u 2,0 ⇒ 4u 2,1 = 100 + u1,1 + u 2,2 + 100
42
Rearrangement above equations
(1) If using Gauss-Jordan elimination method (Practical for small system of equation)
as;
−4u11 + u 21 + u12 =
−200 −4 1 1 0 −200
u11 − 4u 21 + u 22 =
−200 1 −4 0 1 −200
u11 − 4u12 + u 22 =
−100 1 0 −4 1 −100
u 21 + u12 − 4u 22 =
−100 0 1 1 −4 −100
(2) If using Iteration method (Practical for large system of equation) as;
y
0ºC 1,3 3,2
0,3 3,3
12
u1,1 − u1,0
uy = = 0 ⇒ u1,1 = u1,0 ...(a )
1,0 2k
u −u
uy = 2,1 2,0 = 0 ⇒ u 2,1 = u 2,0 ...(b )
2,0 2k
43
At point (1,0)
4u1,0 = u 2,0 + u 0,0 + u1,1 + u1,−1 ⇒ 4u1,0 = u 2,0 + 100 + u1,1 + u1,1
4u1,0 − u 2,0 − 2u1,1 =
100 (1)
At point (2,0)
4u 2,0 = u 3,0 + u1,0 + u 2,1 + u 2,−1 ⇒ 4u 2,0 = 100 + u1,0 + u 2,1 + u 2,−1
4u 2,0 − u1,0 − 2u 2,1 =
100 (2)
The other points same the above example
H.W. Complete the other points and solution using Gauss-Seidel Method.
αk 2α k
=
u i , j +1 u
2 i +1, j
+ u i −1, j + 1 − 2 u i , j
h h
44
(1 − 2r )u i , j + r u i +1, j + u i −1, j
u i , j +1 =
αk
where r =
h2
i, j+1
t k
....
....
h
i-1, j i, j i+1, j
0,3 3,2 j=3
1,3 3,3
1 αk 1
r≤ ⇒ 2
≤
2 h 2
2
h
∴k ≤ time step
2α
45
Ex. A bar is initially at 100 ºC, it's ends are attached to a constant temperature body
at 0 ºC. Find the temperature distribution assuming α = 0.1, Take ∆x=h=0.25.
....
....
....
2
0.25
k ≤ → k ≤ 0.3125
2 * 0.1 4,3
0,3 1,3 3,2 3,3
Take k = 0.25
0,2 1,2 2,2 3,2 4,2
αk 0.1* 0.25
=
r = = 0.4
h2 0.252 0,1
1,1 2,1 3,1 4,1
t k
h
x 0,0
(1 − 2r )u i , j + r u i +1, j + u i −1, j
u i , j +1 =
1,0 2,0 3,0 4,0
At t = 0.25, j =1
u=
1,1 0.2 *100 + 0.4 (100 + 0=
0.2u1,0 + 0.4 u 2,0 + u 0,0= ) 60
u 2,1= 0.2u 2,0 + 0.4 u 3,0 + u1,0 = 0.2 *100 + 0.4 (100 + 100 )= 100
u 3,1= 0.2u 3,0 + 0.4 u 4,0 + u 2,0 = 0.2 *100 + 0.4(0 + 100)= 60
At t = 0.5, j =2
46
0.2 * 60 + 0.4 (100 + 0=
= 0.2u1,1 + 0.4 u 2,1 + u 0,1=
u 2,1 ) 52
u 2,2= 0.2u 2,1 + 0.4 u 3,1 + u1,1 = 0.2 *100 + 0.4 ( 60 + 60=
) 68
u 3,2 = 0.2u 3,1 + 0.4 u 4,1 + u 2,1 = 0.2 * 60 + 0.4(0 + 100)= 52
At t = 0.75, j = 3
continue the procedure for until the steady state condition that is not change the
value of u at increase the time t.
47
c 2k 2
u= u i +1, j − 2u i , j + u i −1, j + 2u i , j + u i , j −1
h2
i , j +1
ck
where r =
h
i, j+1
k
h
t i-1, j i, j i+1, j
....
....
i, j-1
0,3 3,2 j=3
1,3 3,3
0,1 j=1
1,1 2,1 3,1 −1
k 2
t =0
h
r 2 (1 − r 2 ) r2
0,0 1,0 3,0 .... x
2,0
−1
1,-1 2,-1 3,-1
(1 − r ) ≥ 0
2
⇒ r ≤1 ⇒
ck
h
≤ 1 ∴k ≤
h
c
u i ,0 − u i ,−1
v (=
x i ) u=
t i ,0 Using Backward difference
k
∴u i ,−1 =u i ,0 − kv ( x i )
48
Ex. For the string shown, find the motion using ∆x = 0.25, speed c = 1 m/s.
L=1
2 0.25 ≤ x ≤ 0.75
v (x ) =
0 otherwise
f(x)
1
Sol. h = ∆x = 0.25
0,3 1,3 3,2 3,3
h
r ≤ 1 ⇒ ∴k ≤
c 0,2 1,2 2,2 3,2
k ≤ 0.25 ⇒ take k =
0.2
ck 1* 0.2 0,1
=
r = = 0.8 1,1 2,1 3,1
h 0.25 k
h
t =0
r 2 = 0.64 0,0 1,0 2,0 3,0 4,0
u i , j +1= 0.72u i , j + 0.64 (u i +1, j + u i −1, j ) − u i , j −1 Figure; Grid and mesh points
* Boundary conditions;
u=
0,0 u=
0,1 u=
0,2 u=
0,3 = 0
......
u=
4,0 u=
4,1 u=
4,2 u=
4,3 = 0
......
At t = 0 , j = 0
49
u i=
, −1 u i ,0 − kv ( ih )
u1,0 − kv ( 0.25) =
u1,−1 = 0.5 − 0.2 * 2 =
0.1
u 2,0 − kv ( 0.5) =
u 2,−1 = 1 − 0.2 * 2 =
0.6
u 3,0 − kv ( 0.75) =
u 3,−1 = 0.5 − 0.2 * 2 =
0.1
At t = 0.2, j =2
u=
1,2 0.72u1,1 + 0.64 (u 2,1 + u 0,1 ) − =
u1,0 0.72 * 0.9 + 0.64(0.76 + 0) −=
0.5 0.6344
u 2,2 0.72u 2,1 + 0.64 (u 3,1 + u1,1 ) −=
= u 2,0 0.72 * 0.76 + 0.64 ( 0.9 + 0.9=
) − 1 0.6992
u=
3,2 0.72u 3,1 + 0.64 (u 4,1 + u 2,1 ) − u=
3,0 0.72 * 0.9 + 0.64 ( 0 + 0.76 ) − 0.5
= 0.6344
At t = 0.4, j = 3
Note: Can be calculate half points and using the symmetry to obtained the
others points
50