NM Total
NM Total
Institute of Engineering,
Tribhuvan University, Nepal
1
Why Numerical Methods?
• Solving a mathematical model representing a real world problem may be very hard or
impossible to solve analytically
• May need to find approximate numerical solutions ⇒ Numerical Methods
• Numerical Methods:
• Obtain numerical solutions to mathematical problems involving continuous variables
• By performing numerical manipulations rather than using analytical relations
• Applying mathematical principle(s) in tricky ways, usually iteratively until an approximate
solution is found within some tolerable error bounds
• Easier than analytical techniques to implement as computer algorithms
• Same method may be applied accross different problems of same nature
• A branch of mathematics and computer science that deals with creating, analyzing, and
implementing algorithms for obtaining numerical solutions to problems involving
continuous variables.
• Problem Domains:
Natural Sciences and Engineering, Social Sciences, Medicine, Business, etc.
2
Non-linear Equations
• Any single variable function in the form of f (x) = 0 can be classified as a non-linear equation if
we cannot identify it clearly as a linear equation. E.g., Polynomial equations, transcendental
equations (equations composed of trigonometric functions, logarithmic functions, etc.)
• Solution ⇔ Root ⇔ Zero of the function ⇔ value(s) of the varialbe x which satisfies f (x) = 0
• Non-linear equations may have multiple or infinite number of roots or they may not possess any
real root at all.
• Analytical solution of non-linear equations demand different approach for each new form of the
problem whereas the same numerical technique may be employed for any such problem.
• Numerical techniques generally employ some mathematical principle iteratively in some tricky
manner to obtain one root at a time, numerically.
• Numerical techniques may be cumbersome than analytical techniques and produce only
approximate results, but may be the only alternate when analytical techniques are hard or
impossible to implement.
• Compared to analytical techniques, numerical techniques are far more easier to implement as
computer algorithms.
3
Bisection Method
Bisection Method
f(x)
If a function f (x) = 0 is continuous between a
an interval (a, b) such that f (a) and f (b) are
of opposite signs, then there exists at least
one real root in the interval (a, b).
1
f (x) = + sin(x)
x3
4
Intermediate Value Theorem
b a
f(x)
f(x)
a b
x x
Multiple roots may exist if f (a) and f (b) are of Even if f (a) and f (b) are of opposite signs,
opposite signs and the function is continuous in existence of root cannot be guaranteed in (a, b)
(a, b). if the function is not continuous in (a, b).
5
Intermediate Value Theorem
f(x)
f(x)
a b a b
x x
No root in (a, b) if f (a) and f (b) are of same Even number of roots in (a, b) if f (a) and
signs (both +ve). f (b) are of same signs (both +ve).
6
Intermediate Value Theorem
a b a b
f(x)
f(x)
x x
No root in (a, b) if f (a) and f (b) are of same Even number of roots in (a, b) if f (a) and
signs (both -ve). f (b) are of same signs (both -ve).
7
Bisection Method
f(a)
f(c)
b
f(x)
a c f(b)
=(a+b)/2
b
f(x)
a (old) a (new)
Step 3: If f (c) and f (a) are of same signs, take (c, b) as new interval; i.e., regard c as new a.
Otherwise take (a, c) as new interval; i.e, regard c as new b. 9
Bisection Method
b
f(x)
a c
c b
f(x)
c b
f(x)
|
a
Absolute Error:
|b − a| ≤ ε
Relative Error:
|b − a| |b − a| |b − a|
≤ε OR ≤ε OR ≤ε
|a| |b| |c|
13
No. of iterations (n)
Error Tolerance = ε
Initial Interval = (a, b)
Regarding the size of interval (a, b) as error:
|b−a|
Error at nth iteration = 2n
|b − a| |b − a|
∴ ≤ε ⇒ 2n ≥
2n ε
|b − a|
log(2n ) ≥ log
ε
Find a real root of x sin(x) + cos(x) = 0 correct to three decimals using the Bisection Method.
Solution: Calculation Table:
Linear Search: i a+ b− c= a+b
2 f (c)
x 0 1 2 3 4 5 6 7 1 2 3 2.5 0.69504
f (x) 1 1.38 1.40 -0.57 -3.68 -4.51 -0.72 5.35 2 2.5 3 2.75 0.12527
3 2.75 3 2.875 -0.20727
Since f (2) = +ve and f (3) = −ve 4 2.75 2.875 2.8125 -0.03738
5 2.75 2.8125 2.7813 0.04475
and the function seems to be continuous in any interval, 6 2.7813 2.8125 2.7969 0.00391
there is at least one real root between x = 2 and x = 3. 7 2.7969 2.8125 2.8047 -0.01668
8 2.7969 2.8047 2.8008 -0.00637
Thus, let the initial interval be (a, b) = (2, 3). 9 2.7969 2.8008 2.7989 -0.00135
10 2.7969 2.7989 2.7979 0.00128
Successive computations are shown in the following 11 2.7979 2.7989 2.7984 -0.00004
table.
15
Rate of Convergence
ε0 = |b − a|
|b − a| ε0
ε1 = =
2 2
|b − a| ε1
ε2 = 2
=
2 2
εn
εn+1 =
2
Linear Relationship between εn and εn+1 =⇒ Linear Convergence ! 16
Algorithm/Pseudocode for Bisection Method
Define: f(x)
Read: a, b, E
if f(a) × f(b) > 0 then
Print: Error
else
repeat
c ← (a + b)/2
if f(c) × f(a) < 0 then
b←c
else
a←c
end if
until |f(c)| ≤ E
Print: c
end if
17
Assignment
1. Find the +ve real root between 6 and 7 correct to 2 decimal places and a negative real
root correct to four decimal places of x sin(x) + cos(x) = 0 using the Bisection Method.
2. While finding a real root of a non-linear equation using bisection method, if the maximum
tolerable absolute error is 0.0005 (accuracy of three decimal places) and the initial starting
interval used is 1 (i.e., |a − b| = 1 initially) what would be the approximate number of
iterations required to obtain the specified accuracy of the root? What if the initial starting
interval used is 0.1?
3. Use bisection method to find the cube root of 7 correct to 4 decimal places by taking the
initial bracket with an interval of 0.1. [Hint: x3 − 7 = 0]
4. (Practical) Develop a simple algorithm and program code in C/C++ to find a real root of
a non-linear equation using the Bisection Method for a given function from user input
values of the interval (a, b) and error tolerance (E). Check your program by changing your
equation and also for different initial intervals of the same equation.
18
Numerical Methods
Solution of Non-Linear Equations
å Newton-Raphson Method
Institute of Engineering,
Tribhuvan University, Nepal 1
Newton-Raphson Method
(x0 , f (x0 ))
• x1 = First approximation of the root
root
x1 x0
2
x
Newton-Raphson Method
at (x1 , f (x1 ))
• x2 = Second approximation of the
root
root
• Proceed similarly to obtain x3 , x4 , . . .
until xi+1 ≈ xi
x2 x1 x0
3
x
Newton-Raphson Method
f (x0 )
or, f 0 (x0 ) =
x0 − x1
f(x)
f(x0) f (x0 )
∴ x1 = x0 −
f 0 (x0 )
f (x1 )
similarly, x2 = x1 −
root f 0 (x1 )
f (xi )
In general, xi+1 = xi −
x2 x1 x0 f 0 (xi )
4
x
Derivation via Taylor’s Series (Analytical approach)
The value of x1 thus obtained may not represent the exact root as mentioned earlier but may
be regarded as a better approximation than x0 .
Better approximations can be achieved from:
f (x1 ) f (x2 ) f (xi )
x2 = x1 − f 0 (x1 ) x3 = x2 − f 0 (x2 ) ... xi+1 = xi − f 0 (xi )
5
Limitations of NR method
• Oscillation or divergence near inflection point • Root jumping: Resulting into the computation of
[f 00 (xi ) = 0] an unintended root
6
Example
Find a real root of x sin(x) + cos(x) = 0 with an accuracy of 99.999% using the Newton
Raphson Method.
Solution: Calculation table:
Accuracy required = 99.999% |xi+1 −xi |
Tolerable error = (100 - 99.999)% i xi xi+1 Rel. Err. = |xi+1 |
Let xn be an approximate root near α and xn+1 be the successive better approximation
1 f 00 (xn )
xn+1 − α = (xn − α)2 0
2 f (xn )
Setting εn = xn − α and εn+1 = xn+1 − α
1 2 f 00 (α)
εn+1 ≈ ε
2 n f 0 (α)
Hence, the Newton-Raphson method has a quadratic (second order) convergence.
8
Assignment
1. Rework with the given example taking x0 = 1 and explain about the result thus
obtained.
2. Determine a real root of ex + cos(x) − 5 = 0 correct to 6 decimals (absolute error
< 0.0000005) using the Newton-Raphson Method.
3. (Practical) Develop an algorithm and program in C/C++ to find a real root of a
polynomial equation of any degree given it’s co-efficients. Hint: construct
different functions for the evaluation of polynomial and it’s derivative at a given
point given the co-efficients. For n degree polynomial:
Xn n
X
f (x) = a0 + ai xi f 0 (x) = a1 + i × ai xi−1
i=1 i=2
where a0 is the constant term and a1 , a2 , . . . , an are the coefficients of
x, x2 , . . . , xn of the polynomial equation of degree n
9
Numerical Methods
Solution of Non-Linear Equations
å Method of False Position (Regula Falsi Method)
å Secant Method
Institute of Engineering,
Tribhuvan University, Nepal 1
Method of False Position
(Regula Falsi Method)
Method of False Position (Regula Falsi)
• Despite of deciding the new sub-interval using the Intermediate Value Theorem, it
can be observed that one of the brackets might remain fixed mostly, resulting in
slow convergence.
• Convergence rate can be improved by taking the latest two x-values to compute
the next approximation, completely avoiding the intermediate value theorem - the
Secant method.
• This improves the convergence rate, however, at the cost of reliability.
6
Secant Method
Secant Method
7
Secant Method
x2 x3
x0 x1
8
Secant Method
x2 x3
x4 x0 x1
9
Secant Method
x2 x3
x4 x0 x1
10
Secant Method
f (x0 )
=
x0 − x2
f(x1) Also,
f (x1 )
f(x0) =
x1 − x2
f(x)
x2 f (x0 ) f (x1 )
i.e., =
x0 x1 x0 − x2 x1 − x2
Similarly,
x1 f (x2 ) − x2 f (x1 )
x3 =
f (x2 ) − f (x1 )
In General,
x2 x3
xi−1 f (xi ) − xi f (xi−1 )
x0 x1 xi+1 =
f (xi ) − f (xi−1 )
f(x2) or,
af (b) − bf (a)
c=
f (b) − f (a)
x where,
a = xi−1 b = xi c = xi+1
12
Example
Find a real root of ex + sin(x) − 4 = 0 correct to 5 decimals using the Secant Method.
Here, Calculation Table:
f (x) = ex + sin(x) − 4
i a b c f (c)
f (1) = −0.4402 (-ve)
[xi−1 ] [xi ] [xi+1 ]
f (2) = 4.2984 (+ve)
1 1.5 1.7 1.200093 0.252498
Let the initial interval be
2 1.7 1.200093 1.143058 0.046251
(x0 , x1 ) = (a, b) = (1.5, 1.7)
3 1.200093 1.143058 1.130268 0.001013
Secant formula:
4 1.143058 1.130268 1.129982 5 × 10−6
af (b) − bf (a)
c= 5 1.130268 1.129982 1.12998 −2 × 10−6
f (b) − f (a)
∵ |f (1.12998)| < 0.000005
∴ a root of the required accuracy = 1.12998
13
Rate of Convergence
εn+1 ∝ εnk
where, √
1+ 5
k= = 1.618 (Golden Ratio)
2
• Super-linear convergence
• Much more faster than Bisection Method (linear convergence)
• Almost comparable to that of Newton-Raphson Method (quadratic convergence)
14
Assignments
Institute of Engineering,
Tribhuvan University, Nepal
1
Fixed Point Iteration Method
Mechanism: Example:
f (x) = 0 cos(x) + 3x − 2 = 0
⇓ ⇓
x = g(x) 2 − cos(x)
x=
3
⇓ ⇓
Iteration formula Iteration formula
⇓ ⇓
xi+1 = g(xi ) 2 − cos(xi )
xi+1 =
3
2
Example 1
Find a real root of cos(x) + 3x − 2 = 0 correct to 5 decimal places using the fixed point
iteration method.
Solution: x1 = g(x0 ) = [2 − cos(x0 )]/3 = 0.374139
Background:
4
y=x
y
x0
g(x0)
x1 x0
g(x0)
g(x1)
x2 x1 x0
g(x0)
g(x1)
g(x2)
x3 x2 x1 x0
x1 = g(x0) y=x
x2 = g(x1) Near the root:
x3 = g(x2) 0 < g 0 (x) < 1
Resulting in:
Monotonic Convergence
y
g(x0)
g(x1)
g(x2)
x3 x2 x1 x0
x0
g(x0)
x0 x1
g(x1)
g(x0)
x0 x1 x2
g(x2)
y
g(x1)
g(x0)
x0 x1 x2 x3
Resulting in:
g(x2) Monotonic Divergence
y
g(x1)
g(x0)
x0 x1 x2 x3
x0
g(x0)
x0 x1
g(x0)
g(x1)
x0 x2 x1
g(x0)
g(x2)
g(x1)
x0 x2 x3 x1
g(x0)
g(x2)
g(x3)
g(x1)
x0 x2 x4 x3 x1
Resulting in:
Oscillating Convergence
y
g(x0)
g(x2)
g(x3)
g(x1)
x0 x2 x4 x3 x1
x0
g(x0)
x0 x1
g(x0)
g(x1)
x2 x0 x1
g(x2)
g(x0)
g(x1)
x2 x0 x1 x3
Resulting in:
Oscillating Divergence
y
g(x2)
g(x0)
g(x1)
x2 x0 x1 x3
Find a real root of x3 + x2 − 1 = 0 correct to 6 decimals using the fixed point method.
Solution:
f (x) = x3 + x2 − 1
f (0) = −ve and f (1) = +ve
x = g(x) g 0 (x) g 0 (0) g 0 (1) g 0 (0.5) Useful?
1 2x
x = (1 − x2 ) 3 − 0 −∞ −0.4038 7
3(1 − x2 )2/3
1 3x2
x = (1 − x3 ) 2 − 0 −∞ −0.4009 7
3(1 − x3 )1/2
x = x3 + x2 + x − 1 3x2 + 2x + 1 1 6 2.75 7
− 12 1
x = (x + 1) − -0.5 −0.1768 −0.2722 3
2(x + 1)3/2
27
[Please complete this problem]
28
Finding Square Root of a positive real number using basic math operations
√
Example: 5 =?
√
Let x = 5
∴ x2 = 5 ⇒ x2 − 5 = 0 ⇒ f (x) = x2 − 5
Writing f (x) = 0 as x = g(x):
x = 5/x ⇒ Not useful
2x = 5/x + x
5/x+x
x= 2
⇒ Standard form
√
Standard form for N :
N/x+x
x= 2
30
Numerical Methods
Solution of System of Linear Algebraic equations
å Gauss Jordan Method
Institute of Engineering,
Tribhuvan University, Nepal
Gauss Jordan Method
Given the Linear System: Procedure:
• Reduce the augmented coefficient matrix to diagonal
a11 x1 + a12 x2 + a13 x3 = b1
form via row equivalent technique:
a21 x1 + a22 x2 + a23 x3 = b2 • eliminate the non-diagonal coefficients column-wise
a31 x1 + a32 x2 + a33 x3 = b3 • by taking the column diagonal as pivot
• and row corresponding to column diagonal as pivotal row
In matrix form:
• Obtain the solution vector from diagonalized matrix
a11 a12 a13 x1 b1 • by dividing the constant term of each row from row
a21 a22 a23 x2 = b2
diagonal (pivot element)
a31 a32 a33 x3 b3
a11 a12 a13 : b1 R1 ⇒ Pivotal Row
a21 a22 a23 : b2 R2 ← R2 − aa21 R1 [To eliminate a21 ]
11
a31 a32 a33 : b3 R3 ← R3 − aa11
31
R1 [To eliminate a31 ]
a11 a12 a13 : b1 R1 ← R1 − aa12
22
R2 [To eliminate a12 ]
∼ 0 a22 0 a23 0 : b2 0 R2 ⇒ Pivotal Row
0 a32 0 a33 0 : b3 0 R3 ← R3 − aa32
22
R2 [To eliminate a32 ]
a11 0 a13 0 : b1 0 R1 ← R1 − aa33
13
R3 [To eliminate a13 ]
∼ 0 a22 0
a23 0 : b2 0 a23
R2 ← R2 − a33 R3 [To eliminate a23 ]
00
0 0 a33 00 : b3 R3 ⇒ Pivotal Row
a11 0 0 : b1 00 x1 = b1 /a11
∼ 0 a22 0 0 : b2 00 ⇒ Solution ⇒ x2 = b2 /a22
0 0 a33 00 : b3 00 x3 = b3 /a33
JRS\IOE 2/5
In summary
In the row operation formula
Eliminate all ai,j
where: ai,j ⇒ Element being eliminated
j = 1, 2, . . . , n Ri ⇒ Row corresponding to ai,j
i = 1, 2, . . . , n
aj,j ⇒ Pivot element
i 6= j
Rj ⇒ Pivotal Row
n = No. of unknowns
using the row operation:
ai,j Limitations
Ri ← Ri − Rj
aj,j
• The pivot element aj,j should not be zero.
Finally, obtain the solution: • When aj,j ≪ ai,j during row-operation,
bi small round-offs may result in large errors.
xi =
ai,i
JRS\IOE
where: i = 1, 2, . . . , n 3/5
Gauss Jordan Method: Example
Solve:
5 4 3 : 28
5x1 + 4x2 + 3x3 = 28 R2 ← R2 − (7/5)R1
∼ 0 −8/5 −26/5 : −96/5
R3 ← R3 − (8/5)R1
7x1 + 4x2 − x3 = 20 0 −57/5 −4/5 : −104/5
8x1 − 5x2 + 4x3 = 24
5 0 −10 : −20
R1 ← R1 + (5/2)R2
∼ 0 −8/5 −26/5 : −96/5
Augmented Coefficient Matrix: R3 ← R3 − (57/8)R2
0 0 145/4 : 116
5 4 3 : 28
7 4 −1 : 20
5 0 0 : 12
8 −5 4 : 24 R1 ← R1 + (8/29)R3
∼ 0 −8/5 0 : −64/25
R2 ← R2 + (104/725)R3
0 0 145/4 : 116
x1 12/5 2.4
−64/25
Solution: = =
2 −8/5 1.6
x
JRS\IOE 116 4/5
x3 145/4
3.2
Assignments
1. Construct a system of linear equations in four unknowns of your own (using predetermined
values for the unknowns) and show its solution by Gauss Jordan Method. Make sure that
the system so constructed has a unique set of solution (coefficient matrix should be
non-singular).
2. Write an algorithm/pseudo-code for solving a system of linear equations in any number of
unknowns using the Gauss-Jordan Method, taking care of error condition(s) that may arise
during the computation. Check your program for the systems comprising of two, three,
four, and five unknowns.
3. Find out an expression to determine the total number of scalar calculations required to
solve a system of n unknowns using the Gauss-Jordan Method.
JRS\IOE 5/5
Numerical Methods
Solution of System of Linear Algebraic equations
å Gauss Elimination Method
Institute of Engineering,
Tribhuvan University, Nepal
Gauss Elimination Method
Given the Linear System: Procedure:
• Reduce the augmented coefficient matrix to upper
a11 x1 + a12 x2 + a13 x3 = b1
triangular form via row equivalent technique:
a21 x1 + a22 x2 + a23 x3 = b2 • column-wise, eliminate the elements below the diagonal
a31 x1 + a32 x2 + a33 x3 = b3 • by taking the column diagonal as pivot
• and row corresponding to column diagonal as pivotal row
In matrix form:
• Obtain the solution vector
a11 a12 a13 x1 b1 • via back-substitution
a21 a22 a23 x2 = b2
a31 a32 a33 x3 b3
a11 a12 a13 : b1 R1 ⇒ Pivotal Row
a21 a22 a23 : b2 R2 ← R2 − aa21 R1 [To eliminate a21 ]
11
a31 a32 a33 : b3 R3 ← R3 − aa11
31
R1 [To eliminate a31 ]
a11 a12 a13 : b1
∼ 0 a22 0 a23 0 : b2 0 R2 ⇒ Pivotal Row
0 a32 0 a33 0 : b3 0 R3 ← R3 − aa32
22
R2 [To eliminate a32 ]
a11 a12 a13 : b1
∼ 0 a22 0 a23 0 : b2 0
00
0 0 a33 00 : b3
x
x1 = (b1 − a12 x2 − a13 x3 )/a11
Back-substitution ⇒ x2 = (b2 − a23 x3 )/a22 ⇒ Solution
JRS\IOE x3 = b3 /a33 2/6
In summary
In the row operation formula:
Eliminate all ai,j
where: ai,j ⇒ Element being eliminated
j = 1, . . . , (n-1) Ri ⇒ Row corresponding to ai,j
i = (j+1), . . . , n
aj,j ⇒ Pivot element
n = No. of unknowns
Rj ⇒ Pivotal Row
using the row operation:
ai,j
Ri ← Ri − Rj
aj,j Limitations
Perform back substitution for solution: • The pivot element aj,j should not be zero.
• When aj,j ≪ ai,j during row-operation,
xn = bn /an,n small round-offs may result in large errors.
bi − nj=i+1 ai,j xj
P
xi =
ai,i
JRS\IOE 3/6
where: i = n-1, n-2, . . . , 1
Gauss Elimination Method: Example
Solve:
5 4 3 : 28
5x1 + 4x2 + 3x3 = 28 R2 ← R2 − (7/5)R1
∼ 0 −8/5 −26/5 : −96/5
R3 ← R3 − (8/5)R1
7x1 + 4x2 − x3 = 20 0 −57/5 −4/5 : −104/5
8x1 − 5x2 + 4x3 = 24
5 4 3 : 28
R3 ← R3 − (57/8)R2 ∼ 0 −8/5 −26/5 : −96/5
Augmented Coefficient Matrix:
0 0 145/4 : 116
5 4 3 : 28
Back Substitution:
7 4 −1 : 20
116
8 −5 4 : 24 x3 = 145/4
= 3.2 x1 2.4
−96/5−(−26/5)x3
x2 = = −96/5−(−26/5)(3.2) = 1.6 ⇒ x2 = 1.6
−8/5 −8/5
28−4x2 −3x3 28−4(1.6)−3(3.2)
x1 = 5
= 5
= 2.4 x3 3.2
JRS\IOE 4/6
Pivoting
1. Construct a system of linear equations in four unknowns of your own (using predetermined values
for the unknowns) and show its solution by Gauss Elimination Method. Make sure that the
system so constructed has a unique set of solution (coefficient matrix should be non-singular).
2. Write an algorithm/pseudo-code for solving a system of linear equations in any number of
unknowns using the Gauss Elimination Method, taking care of error condition(s) that may arise
during the computation. Check your program for the systems comprising of two, three, four, and
five unknowns.
3. Find out an expression to determine the total number of scalar calculations required to solve a
system of n unknowns using the Gauss Elimination Method.
JRS\IOE 6/6
Numerical Methods
Solution of System of Linear Algebraic equations
å Matrix Inverse via Gauss-Jordan Method
å Matrix Inverse via Gauss-Elimination Method
Institute of Engineering,
Tribhuvan University, Nepal
Matrix Inverse via Gauss Jordan Method
i.e.,
a11 a12 a13 x11 x12 x13 1 0 0
a21 a22 a23 x21 x22 x23 = 0 1 0
a31 a32 a33 x31 x32 x33 0 0 1
JRS\IOE 1
Augmented coefficient matrices: Procedure:
x11 a11 a12 a13 : 1 a11 a12 a13 : 1 0 0
a21 a22 a23 : 0 1 0
x ⇒ 21 a22 a23
a : 0
21
x31 a31 a32 a33 : 0 a31 a32 a33 : 0 0 1
x12 a11 a12 a13 : 0
x22 ⇒ a21 a22 a23
: 1
⇓
x32 a31 a32 a33 : 0
a11 0 0 0 : a14 a15 a16
x13 a11 a12 a13 : 0 0 a22 0 0 : a24 a25 a26
x
23 ⇒ a21 a22 a23
: 0
0 0 a33 0 : a34 a35 a36
x33 a31 a32 a33 : 1
⇓
Combining:
1 0 0 : x11 x12 x13
a11 a12 a13 : 1 0 0
0 1 0 : x21 x22 x23
a21 a22 a23 : 0 1 0
0 0 1 : x31 x32 x33
a31 a32 a33 : 0 0 1
JRS\IOE 2
Example
2 −3 −2
h i
Find the inverse of the following matrix using the Gauss Jordan Method: −1 −1 −3 .
3 −2 3
Solution:
2 −3 −2
A = −1 −1 −3
3 −2 3
x11 x12 x13
Let A−1 = X = x21 x22 x23
x31 x32 x33
∴ AX = I
2 −3 −2 x11 x12 x13 1 0 0
−1 −1 −3 x21 x22 x23 = 0 1 0
JRS\IOE 3 −2 3 x31 x32 x33 0 0 1 3
2 −3 −2 : 1 0 0
Augmented coefficient matrix: −1 −1 −3 : 0 1 0
3 −2 3 : 0 0 1
2 −3 −2 : 1 0 0
R2 ← R2 + (1/2) ∗ R1
∼ 0 −5/2 −4 : 1/2 1 0
R3 ← R3 − (3/2) ∗ R1
0 5/2 6 : −3/2 0 1
2 0 14/5 : 2/5 −6/5 0
R1 ← R1 − (6/5) ∗ R2
∼ 0 −5/2 −4 : 1/2 1 0
R3 ← R3 + R2
0 0 2 : −1 1 1
2 0 0 : 9/5 −13/5 −7/5
R1 ← R1 − (7/5) ∗ R3
∼ 0 −5/2 0 : −3/2 3 2
R2 ← R2 + (2) ∗ R3
0 0 2 : −1 1 1
R1 ← R1/2 1 0 0 : 9/10 −13/10 −7/10 9/10 −13/10 −7/10
−1
R2 ← (−2/5)R2 ∼ 0 1 0 : 3/5 −6/5 −4/5 ∴ A = 3/5 −6/5 −4/5
R3 ← R3/2 0 0 1 : −1/2 1/2 1/2 −1/2 1/2 1/2
JRS\IOE 4
Matrix Inverse Using Gauss Elimination Method
Thus, we have:
2 −3 −2 : 1 0 0
2 −3 −2 x11 1
Augmented coefficient matrix: −1 −1 −3 : 0 1 0
0 −5/2 −4 x21 = 1/2
3 −2 3 : 0 0 1
0 0 2 x31 −1
2 −3 −2 : 1 0 0
R2 ← R2 + (1/2) ∗ R1 ∼ 0 −5/2 −4 : 1/2 1 0
2 −3 −2 x12 0
R3 ← R3 − (3/2) ∗ R1 0 5/2 6 : −3/2 0 1 0
−5/2 −4 x22 = 1
0 0 2 x32 1
2 −3 −2 : 1 0 0
∼ 0 −5/2 −4 : 1/2 1 0
2 −3 −2 x13 0
R3 ← R3 + R2 0 0 2 : −1 1 1
0 −5/2 −4 x23 = 0
0 0 2 x33 1
JRS\IOE 5
Performing Back Substitutions on each system:
x31 = −1/2 x11 9/10
x21 = (1/2)−(−4)(x 31 )
= (1/2)+4(−1/2)
= 3/5 V x21 = 3/5
(−5/2) (−5/2)
1−(−3)(x21 )−(−2)(x31 ) 1+(3)(3/5)+(2)(−1/2) x31 −1/2
x11 = 2 = 2 = 9/10
x32 = 1/2 x12 −13/10
x22 = 1−(−4)(x 32 ) 1+4(1/2) V x22 = −6/5
(−5/2) = (−5/2) = −6/5
x12 = 0−(−3)(x22 )−(−2)(x32 )
= (3)(−6/5)+(2)(1/2) = −13/10 x32 1/2
2 2
x33 = 1/2 x31 −7/10
x23 = 0−(−4)(x 33 ) 4(1/2) V x32 = −4/5
(−5/2) = (−5/2) = −4/5
x13 = 0−(−3)(x23 )−(−2)(x33 )
= (3)(−4/5)+(2)(1/2)
= −7/10 x33 1/2
2 2
9/10 −13/10 −7/10
∴ A−1 = X = 3/5 −6/5 −4/5
−1/2 1/2 1/2
JRS\IOE 6
Assignments
1. Construct a 4 × 4 non-singular square matrix of your own and compute its inverse using:
a) Gauss Jordan Method
b) Gauss Elimination Method
also verify the correctness of your result.
2. Write algorithm/pseudo-code and program to find the inverse of a given square matrix of
any order using the Gauss-Jordan Method. Verify your program with second, third, fourth,
and fifth order matrices.
JRS\IOE 7
Numerical Methods
Solution of System of Linear Algebraic equations
å Factorization Method (LU Decomposition)
Institute of Engineering,
Tribhuvan University, Nepal
Matrix Factorization
A square matrix (A) can be written as the product of a lower triangular matrix (L) and an upper
triangular matrix (U), taking either of L or U as unit triangular, if all the leading principal minors of A
are non-singular.
a11 a12 · · · a1n
a
11 a
12
a
21 a22 · · · a2n
i.e., a11 6= 0 6= 0 ··· 6= 0
.
a21 a22 . .
. .. .
.
.
. . .
an1 an2 · · · ann
a11 a12 a13 1 0 0 u11 u12 u13 Do-Little’s Method
a21 a22 =
a23 l21 1 0 0 u22 u23 ⇒ L = Unit lower triangular
a31 a32 a33 l31 l32 1 0 0 u33 U = Upper traingular
a11 a12 a13 l11 0 0 1 u12 u13 Crout’s Method
a21 a22 a23 = l21 l22 0 0 1 u23 ⇒ L = Lower triangular
a31 a32 a33 l31 l32 l33 0 0 1 U = Unit Upper traingular
JRS\IOE 1
Factorization via Do-Little’s method
a11 a12 a13
Suppose A = a21 a22 a23
a31 a32 a33
Let A = LU , where:
1 0 0 u11 u12 u13
L = l21 1 0and U = 0 u22 u23
l31 l32 1 0 0 u33
u11 u12 u13 a11 a12 a13
LU = A ⇒ l21 u11 l21 u12 + u22 l21 u13 + u23 = a21 a22 a23
l31 u11 l31 u12 + l32 u22 l31 u13 + l32 u23 + u33 a31 a32 a33
Equating term by term:
u11 = a11 u12 = a12 u13 = a13
l21 = a21 /u11 u22 = a22 − l21 u12 u23 = a23 − l21 u13
l31 = a31 /u11 l32 = (a32 − l31 u12 )/u22 u33 = a33 − l31 u13 − l32 u23
JRS\IOE 2
Solution of linear system via Factorization
Procedure:
Given: A X = B
Factorize: A = L U
AX = B ⇒ LU X = B
Let, U X = Y
LU X = B ⇒ LY = B
Obtain Y from L Y = B via forward substitution
Substitute Y in U X = Y
Obtain X from U X = Y via backward substitution
JRS\IOE 3
Example
Solve:
LU = A
5x1 + 4x2 + 3x3 = 28
7x1 + 4x2 − x3 = 20
8x1 − 5x2 + 4x3 = 24 ⇓
The given system expressed as AX = B:
u11 u12 u13
l21 u11 l21 u12 + u22 l21 u13 + u23
5 4 3 x1 28
A = 7
4 −1 X = x2 B = 20
l31 u11 l31 u12 + l32 u22 l31 u13 + l32 u23 + u33
8 −5 4 x3 24
5 4 3
= 7 4 −1
Let A = LU, where:
8 −5 4
1 0 0 u11 u12 u13
L = l21 1 0 U = 0 u22 u23
l31 l32 1 0 0 u33
JRS\IOE 4
u11 u12 u13 5 4 3
l21 u11 l21 u12 + u22 l21 u13 + u23 =
7 4 −1
l31 u11 l31 u12 + l32 u22 l31 u13 + l32 u23 + u33 8 −5 4
Equating term by term:
u11 u12 u13
= a11 = a12 = a13
=5 =4 =3
l21 u22 u23
= a21 /u11 = a22 − l21 u12 = a23 − l21 u13
= 4 − (7/5)(4) = −1 − (7/5)(3)
= 7/5 = −8/5 = −26/5
l31 l32 u33
= a31 /u11 = (a32 − l31 u12 )/u22 = a33 − l31 u13 − l32 u23
= (−5 − (8/5)(4))/(−8/5) = 4 − (8/5)(3) − (57/8)(−26/5)
= 8/5 = 57/8 = 145/4
JRS\IOE 5
1 0 0 ∴ y1 = 28
∴ L = 7/5 1 0
y2 = 20 − (7/5)(y1 )
8/5 57/8 1
= 20 − (7/5)(28)
5 4 3 = −96/5
and U = 0 −8/5 −26/5
y3 = 24 − (8/5)(y1 ) − (57/8)(y2 )
0 0 145/4
= 24 − (8/5)(28) − (57/8)(−96/5)
∴ AX = B ⇒ LU X = B = 116
y1
Let U X = Y , where Y = y2
28
Substituting Y = −96/5 in U X = Y
y3
116
then LU X = B ⇒ LY = B
1 0 0 y1 28 5 4 3 x1 28
0 −8/5 −26/5 x2 = −96/5
i.e., 7/5 1 0 y2 = 20
8/5 57/8 1 y3 24 0 0 145/4 x3 116
JRS\IOE 6
Back Substitution: Hence, the required solution is:
116
∴ x3 = = 3.2 2.4
145/4
X = 1.6
−96/5 − (−26/5)(3.2)
x2 = = 1.6 3.2
−8/5
28 − (4)(1.6) − (3)(3.2)
x1 = = 2.4
5
JRS\IOE 7
Do-Little Decomposition of a 4 × 4 matrix
i−1
X
Uij = Aij − Lik Ukj
k=1
j−1
!,
X
Lij = Aij − Lik Ukj Ujj
k=1
JRS\IOE 8
Pseudo-code
for i = 1 to n
for j = 1 to n
if i ≤ j
i−1
X
Uij = Aij − Lik Ukj
k=1
Uij = 0
end if
end for
end for
JRS\IOE 9
Assignments
1. Construct a system of linear equations in 4 unknowns of your own and solve it using the
LU factorization method.
2. Write algorithm / pseudo-code and program to solve a system of linear equations using
the LU factorization method using Do-Little decomposition.
JRS\IOE 10
Numerical Methods
Solution of System of Linear Algebraic equations
å Iterative Methods:
à Jacobi’s Method
à Gauss-Seidal Method
Institute of Engineering,
Tribhuvan University, Nepal
Jacobi’s Method
In Summary
a11 x1 + a12 x2 + a13 x3 = b1
a21 x1 + a22 x2 + a23 x3 = b2 n
X
,
xi (k+1) = (k)
a31 x1 + a32 x2 + a33 x3 = b3 bi − aij xj aii
j=1
j6=i
⇓ Transform
where, i = 1 to n
x1 = (b1 − a12 x2 − a13 x3 )/a11 k = iteration number
In Summary
a11 x1 + a12 x2 + a13 x3 = b1
a21 x1 + a22 x2 + a23 x3 = b2 i−1
X n
X
!,
(k+1) (k+1) (k)
a31 x1 + a32 x2 + a33 x3 = b3 xi = bi − aij xj − aij xj aii
j=1 j=i+1
⇓ Transform where, i = 1 to n
x1 = (b1 − a12 x2 − a13 x3 )/a11 k = iteration number
w + x + 2y + z = 5
6w + x − y + 2z = 4
2w + 5x − 4y + 6z = −5
w + 4x + 3y − z = 2
JRS\IOE 5/5
Numerical Methods
Linear Algebra
å Power Method
Institute of Engineering,
Tribhuvan University, Nepal
Power Method
AX = λX
• A convenient iterative technique to compute the numerically largest (dominant) Eigen
value and the corresponding Eigen vector of a square matrix, as required in many
engineering problems
• Much suitable for machine computations (programming)
JRS\IOE 1/6
Mechanism
2 1 7 8.817 1
1
(0)
4.955 0.566
Let the initial guess vector be X = 1
AX (4) = 5.132 = 8.753 0.586 = λ(5) X (5)
1
8.753 1
7 0.7
AX (0) = 8 = 10 0.8 = λ(1) X (1)
4.89 0.561
AX (5) = 5.042 = 8.718 0.578 = λ(6) X (6)
10 1
8.718 1
5.8 0.63
AX (1) = 6.3 = 9.2 0.685 = λ(2) X (2)
4.856 0.558
AX (6) = 4.995 = 8.7 0.574 = λ(7) X (7)
9.2 1
8.7 1
5.315 0.594
AX (2) = 5.63 = 8.945 0.629 = λ(3) X (3)
4.838 0.557
AX (7) = 4.97 = 8.69 0.572 = λ(8) X (8)
JRS\IOE
8.945 1 3/6
8.69 1
For an accuracy of 2 decimal places:
∵ max X (8) − X (7) < 0.005
0.56
and Corresponding Eigen Vector ≈ X (8) = 0.57
1.00
JRS\IOE 4/6
Computing the smallest Eigen Value
1
∵ AX = λX =⇒ A−1 X = X
λ
JRS\IOE 5/6
Assignment
1. Find the dominant and the smallest Eigen values and corresponding Eigen vectors of the
following matrix using the power method.
−2 3 2
4 −2 1
−1 2 −9
2. Write a high level algorithm, detailed pseudo-code, and program code in C/C++ for
computing the dominant Eigen value and corresponding Eigen vector of a square matrix
using the power method.
JRS\IOE 6/6
6/4/2021
Interpolation
• Interpolation is the technique of estimating the value of a
function for any intermediate value of the independent
variable.
Interpolation • The process of computing or finding the value of a function
for any value of the independent variable outside the given
range is called extrapolation.
B. D. Mulmi • Here interpolation denotes the method of computing the value
of the function 𝑦 = 𝑓 (𝑥) for any given value of the
independent variable x when a set of values of y = f (x) for
September, 2020 certain values of x are known or given.
1 2
Interpolation
1. Interpolation with equally spaced interval x 2 4 6 8 10 12 14
a. Newton's Forward Interpolation Formula y 12 13 16 19 23 56 60
b. Newton’s Backward Interpolation Formula
Central Difference interpolation Formula
c. Stirling’s Formula 𝑦(2.5) =? (Forward Interpolation)
d. Bessel’s Formula 𝑦 14.5 =? (Backward Interpolation)
𝑦(0.5) = ? (Forward Interpolation)
𝑦 (2.2) = ?(Forward Interpolation)
2. Interpolation with unequally spaced interval 𝑦 (7.9) = ? (Central Interpolation)
a. Lagrange’s Interpolation Formula
b. Newton’s Divided Difference Formula
3 4
6/4/2021
𝑥 𝑥
b. Newton’s Backward Interpolation Formula
𝑥 𝑥 𝑥 𝑥
∇ 𝑦 ∇ 𝑦
𝑦 = 𝑦 + 𝑝∇𝑦 + 𝑝 𝑝 + 1 +𝑝 𝑝+1 𝑝+2
𝑥 =𝑥 +ℎ 2! 3!
𝑥 =𝑥 + 2ℎ 𝑥−𝑥 ∇ 𝑦
𝑥 =𝑥 + 3ℎ 𝑝= + ………+ 𝑝 𝑝 + 1 … 𝑝 + 𝑛 − 1
…… …. … ℎ 𝑛!
𝑥 =𝑥 + 𝑛ℎ
5 6
Cont’d… Example:
2. Backward Difference Table: # Construct the forward and backward
difference table from the following data set.
𝒙 𝒚 ∇ ∇ ∇ ∇
𝑥 0 1 2 3 4
𝑥 𝑦 𝑦 -5 -6 -1 16 51
𝑥 𝑦 ∇y
𝑥 𝑦 ∇y ∇ y
𝑥 𝑦 ∇y ∇ y ∇ y
𝑥 𝑦 ∇y ∇ y ∇ y ∇ y
∇y =𝑦 − 𝑦 ∇ 𝑦 = ∇𝑦 − ∇𝑦 9 10
11 12
6/4/2021
Proof: Proof:
Proof: Example:
# Using interpolation formula, find the polynomial which
∆𝑦 satisfies the following data.
𝑦=𝑦 + 𝑥−𝑥 +𝑎 𝑥−𝑥 𝑥−𝑥 𝒙 0 1 2 3 4
ℎ
+a 𝑥 − 𝑥 𝑥 − 𝑥 𝑥 − 𝑥 + ⋯ 𝒚 -5 -6 -1 16 51
+𝑎 𝑥 − 𝑥 𝑥 − 𝑥 𝑥 − 𝑥 … … And also evaluate 𝑦(5).
… 𝑥−𝑥 𝑥=𝑥
𝑥 = 4 ,ℎ = 1 Forward:
𝑦 = 𝑦 + 𝑝∆𝑦 + 𝑎 𝑥 − 𝑥 𝑥 − 𝑥 𝑊𝑒 ℎ𝑎𝑣𝑒, 𝑥 =0
+a 𝑥−𝑥 𝑥−𝑥 𝑥−𝑥 +⋯ 𝑥−𝑥 𝑥=𝑥
+𝑎 𝑥−𝑥 𝑥−𝑥 𝑥−𝑥 …… 𝑝= =𝑥−4
ℎ ℎ=1
… 𝑥−𝑥 𝑓 𝑥 = 𝑥 − 2𝑥 − 5 𝑤𝑒 ℎ𝑎𝑣𝑒,
……… 𝑓 5 = 5 −2∗5 −5 𝑥−𝑥
= 110 𝑝= =𝑥
ℎ
15 16
6/4/2021
Example: Example:
# Evaluate 𝑦(0.5) and 𝑦(5) using appropriate The forward difference table is
interpolation formula from the following data.
𝒙 0 1 2 3 4
𝒙 𝒚 ∆ ∆𝟐 ∆𝟑 ∆𝟒
𝒚 -5 -6 -1 16 51 0 −5 -1 6 6 0
1 −6 5 12 6
Solution: 2 −1 17 18
Here the point 𝑥 = 0.5 lies near the beginning 3 16 35
of the table. So we use Newton’s forward
interpolation formula. The forward difference 4 51
table is 17 18
Example: Exercise:
Given, # From the following data, evaluate 𝑦(0.71)
𝑥 =0
𝑥 =1 using appropriate interpolation formula.
𝑥 = 0.5 𝑥 0.70 0.72 0.74 0.76 0.78
∴ℎ =𝑥 −𝑥 =1
𝑊𝑒 ℎ𝑎𝑣𝑒, 𝑦 0.8423 0.8771 0.9131 0.9505 0.9893
𝑥−𝑥
𝑝= = 0.5
ℎ
The Newton’s Forward interpolation formula is
𝑦 = 𝑦 +… 𝐵𝑎𝑐𝑘𝑤𝑎𝑟𝑑
∴ 𝑦 0.5 = −5.875 𝑥−𝑥
∴ 𝑦 5 = 110 𝑝=
ℎ
19 20
6/4/2021
∴ 𝑓 9 = 810 25 26
Example: Exercise:
# Given the values,
# Find the polynomial 𝑓(𝑥) by using interpolation formula
𝒙 5 7 11 13 17 which satisfies the following data and hence find 𝑓(9).
𝑓(𝑥) 150 392 1452 2366 5202
Evaluate 𝑓 9 , using Newton’s Divided Difference Interpolation formula. 𝒙 5 7 11 13 17
Exercise
# Apply Stirling formula to evaluate log 337.50,
from the following data.
𝑥 310 320 330 340 350 360
log(𝑥) 2.4914 2.5052 2.5185 2.5315 2.5441 2.5563
35
5/12/2021
Cont’d …. Cont’d…
If we write:
1. Linear Curve Fitting
S= [y1 – f(x1)]2 + [y2 – f(x2)]2 + [y3 – f(x3)]2 + … + [yn – f(xn)]2
2. Non - Linear Curve Fitting
S = e12 + e22 + e32 + … + en2 ----(2) a) Exponential Function
b) Power Function
c) Polynomial of the nth degree
3 4
5/12/2021
y
i 1
i n a b xi
i 1
(4)
5 6
Cont’d… Example:
n n
y n a bxi
Fit the curve of the form y = a + bx to the following data set.
i (4) x 2 3 4 5 6
i1 i 1 y 3.2 4.4 6.5 8.62 10.8
n n n
7 8
5/12/2021
Example: Example:
The values of x, x , xy , y are calculate shown in
2
On solving, we get
the following table:
a = 1.6
x y xy x2
1 3 3 b = 1.2
2 4 8
3 5 Hence, the required linear best fit
4 6 relation is y = 1.6 + 1.2x
5 8
∑x =15 26 90 55
11 12
5/12/2021
15 16
5/12/2021
17 18
Example
For the following set of data, fit a parabolic curve using Least Squares Method and find y(2)
a) y=a+bx2 ------(1)
Solution: y=a+bX ---- (2) c) y=alogex +b (1)
Given data (n) = 6
Parabolic curve : y = a+bx+cx^2 -(1)
Normal equation of eqn (1)
Where, y=aX+b
𝑦 =𝑎∗𝑛+𝑏 𝑥+𝑐 𝑥
X = x2 X = logex
𝑥𝑦 = 𝑎 ∗ 𝑥+𝑏 𝑥 +𝑐 𝑥
𝑥 𝑦=𝑎 𝑥 +𝑏 𝑥 +𝑐 𝑥
b)y=a+b*1/x ------(1) d) y=ax+b+c/x (1)
xy =ax+b xy =ax2+bx+c
a = , b= ,c = Y = ax+b Y= ax2+bx+c
y(2) =
Where,
Where,
Y = xy
Y = xy
19 20
Cubic Spline Interpolation
B. D. Mulmi
Institute of Engineering
Feb, 2016
Linear Interpolation
Polynomial Interpolation
Cubic Spline Interpolation
Polynomial Interpolation is not always bad!
Given n+1 data points (xi , yi ) for i = 0 to n, develop n cubic
equations between (xi , yi ) and (xi+1 , yi+1 ) for i = 0 to n − 1, such
that the 1st and 2nd derivatives at common points are continuous.
f0,1 (x) for x0 ≤ x ≤ x1
f1,2 (x) for x1 ≤ x ≤ x2
f (x) =
... ... ...
fn−1,n (x) for xn−1 ≤ x ≤ xn
Given by:
(x − xi+1 )3
Mi
fi,i+1 (x) = hi (x − xi+1 ) −
6 hi
(x − xi )3
Mi+1
− hi (x − xi ) −
6 hi
yi+1 (x − xi ) − yi (x − xi+1 )
+
hi
Where, Mi = yi00 00
Mi+1 = yi+1 hi = xi+1 − xi
To find the second derivatives (for i = 1 to n − 1):
Mi−1 (xi − xi−1 ) + 2Mi (xi+1 − xi−1 ) + Mi+1 (xi+1 − xi )
yi+1 − yi yi − yi−1
=6 −
xi+1 − xi xi − xi−1
or,
∆yi ∆yi−1
Mi−1 (hi−1 ) + 2Mi (hi−1 + hi ) + Mi+1 (hi ) = 6 −
hi hi−1
If n = 4:
∆y1 ∆y0
For i = 1 : M0 (h0 ) + 2M1 (h0 + h1 ) + M2 (h1 ) = 6 −
h1 h0
∆y2 ∆y1
For i = 2 : M1 (h1 ) + 2M2 (h1 + h2 ) + M3 (h2 ) = 6 −
h2 h1
∆y3 ∆y2
For i = 3 : M2 (h2 ) + 2M3 (h2 + h3 ) + M4 (h3 ) = 6 −
h3 h2
M0
h0 2(h0 + h1 ) h1 0 0
M1
0 h1 2(h1 + h2 ) h2 0 M2
0 0 h2 2(h2 + h3 ) h3 M3
M4
∆y1 /h1 − ∆y0 /h0 [x1 , x2 ] − [x0 , x1 ]
= 6 ∆y2 /h2 − ∆y1 /h1 = 6 [x2 , x3 ] − [x1 , x2 ]
∆y3 /h3 − ∆y2 /h2 [x3 , x4 ] − [x2 , x3 ]
If x is equally spaced:
6 2
Mi−1 + 4Mi + Mi+1 = ∆ yi−1
h2
If n = 4,
6 2
M0 + 4M1 + M2 = ∆ y0
h2
6
M1 + 4M2 + M3 = 2 ∆2 y1
h
6
M2 + 4M3 + M4 = 2 ∆2 y2
h
or,
M0 2
1 4 1 0 0 M1 ∆ y0
0 1 4 1 0
6 ∆ 2 y1
M2 =
h2
0 0 1 4 1 M3 ∆ 2 y2
M4
In Natural Cubic Spline, M0 = Mn = 0
2
4 1 0 M1 ∆ y0
6
1 4 1 M2 = 2 ∆ 2 y 1
h
0 1 4 M3 ∆ 2 y2
Example
x 2 4 6 8 10
y 7 6 9 11 8
Solution:
Here, n = 4
and x is in equally-spaced interval i.e., h = 2
Let, M0 , M1 , ..., Mn be the 2nd derivatives at x = x0 , x1 , ..., xn
Thus, we have,
M0 2
1 4 1 0 0 M 1 ∆ y0
0 1 4 1 0 M2 = 6 ∆ 2 y 1
h2
0 0 1 4 1 M3 ∆ 2 y2
M4
In Natural Cubic Spline, M0 = Mn = 0
So, the system of equations reduces to:
2
4 1 0 M1 ∆ y0
1 4 1 M2 = 6 ∆ 2 y 1
h2
0 1 4 M3 ∆ 2 y2
or,
4 1 0
M1
4
x y ∆y ∆2 y
6
1 4 1 M2 = −1 2 7
4 -1
0 1 4 M3 −5
4 6 4
or, 3
6 9 -1
4 1 0 M1 6.0
1 4 1 M2 = −1.5 2
8 11 -5
0 1 4 M3 −7.5
-3
10 8
On solving, we get,
M1 = 1.5804 M2 = −0.3214 M3 = −1.7946
Thus We now have,
i 0 1 2 3 4
x 2 4 6 8 10
y 7 6 9 11 8
M 0 1.5804 -0.3214 -1.7946 0
To compute y(5), i.e., to compute y at x=5:
Since x = 5 lies between x1 and x2 , we compute y(5) using:
(x − x2 )3
M1
f1,2 (x) = h(x − x2 ) −
6 h
(x − x1 )3
M2 y2 (x − x1 ) − y1 (x − x2 )
− h(x − x1 ) − +
6 h h
(5 − 6)3
1.5804
∴ f (5) = 2(5 − 6) −
6 2
(5 − 4)3
−0.3214 9(5 − 4) − 6(5 − 6)
− 2(5 − 4) − +
6 2 2
= 7.1839
6/14/2021
Numerical
NumericalIntegration
Integration
y Y = f(x)
Numerical Integration
y0 y1 y2 y3 yn
October, 2020 h
a = x0 x1 x2 x3 b = x0+nh=xn x
1 2
5 6
𝐼 = 𝑦𝑑𝑥 𝐼 = 𝑦𝑑𝑥
𝑌 𝑦 = 𝑓(𝑥)
1 = ℎ(𝑦 + ∆𝑦 )
= ℎ(𝑦 + ∆𝑦 )
2
= (2𝑦 + 𝑦 − 𝑦 ) = (2𝑦 + 𝑦 − 𝑦 )
𝑦 𝑦
= (𝑦 + 𝑦 ) = (𝑦 + 𝑦 )
𝑥 𝑥 𝑋
= area of Trapezium
7 8
6/14/2021
9 10
𝐼 = 𝑦𝑑𝑥
𝐼 = 𝑦𝑑𝑥
= 2ℎ 𝑦 + ∆𝑦 + ∆ 𝑦 = 2ℎ 𝑦 + ∆𝑦 + ∆ 𝑦
= 2ℎ 𝑦 + (𝑦 − 𝑦 ) + ∆𝑦 − ∆𝑦 = 2ℎ 𝑦 + (𝑦 − 𝑦 ) + ∆𝑦 − ∆𝑦
1 = 2ℎ[𝑦 + (𝑦 − 𝑦 − 𝑦 − 𝑦 ]
= 2ℎ[𝑦 + 𝑦 −𝑦 − 𝑦 −𝑦
6
ℎ = (6𝑦 + 𝑦 − 2𝑦 + 𝑦 )
= 6𝑦 + 𝑦 − 2𝑦 + 𝑦
3
= 𝑦 + 4𝑦 + 𝑦
= 𝑦 + 4𝑦 + 𝑦 11 12
6/14/2021
= 𝑦 + 4𝑦 + 𝑦 + 𝑦 + 4𝑦 + 𝑦 + 𝑦 + 4𝑦 + 𝑦 + ⋯ + (𝑦 +
Similarly, for the last interval [𝑥 , 𝑥 ], we get, 4𝑦 +𝑦 )
𝐼 = 𝑦𝑑𝑥 = [ 𝑦 + 4𝑦 + 𝑦 + 𝑦 + 4𝑦 + 𝑦 + 𝑦 + 4𝑦 + 𝑦 + ⋯ + (𝑦 +
( ) 4𝑦 + 𝑦 )]
= 𝑦 + 4𝑦 +𝑦
13 14
3ℎ
= [ 𝑦 +𝑦 +3 𝑦 + 𝑦 +𝑦 + 𝑦 + 𝑦 +⋯+𝑦 + 2(𝑦 + 𝑦 + 𝑦 + ⋯
8
+ 𝑦 )]
3ℎ
= 𝐹𝑖𝑟𝑠𝑡 + 𝑙𝑎𝑠𝑡 + 3 𝑚𝑜𝑑 𝑜𝑓 3 + 2(𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑒 𝑜𝑓 3)
8
15 16
6/14/2021
Example Solution
# Evaluate Solution:
1 Given,
𝑑𝑥 1
1+𝑥 𝐼= 𝑑𝑥
By using i) Trapezoidal Rule, ii) Simpson’s 1/3 – Rule and 1+𝑥
iii) Simpson’s 3/8 – Rule. [Take n = 6] 𝑎 = 0 = 𝑥0
𝑏 = 6 = 𝑥𝑛
𝑛 = 6
𝑏 −𝑎
∴ℎ=
𝑛
ℎ = 1
17 18
Cont’d… Exercise
The values of y = are given below: a) Evaluate the integral
𝑥 0 1 2 3 4 5 6 𝐼= sin 𝑥 (1 + 𝑒 ) 𝑑𝑥
𝑦 1 0.5 0.2 0.1 0.0588 0.0385 0.027
y0 y1 y2 y3 y4 y5 y6 Using Simpson’s 1/3 – rule. [Take n = 6]
i) By using Trapezoidal Rule. We have, A1 = 1 – 4 h =1
I= 𝑦 +𝑦 + 2(𝑦 + 𝑦 + 𝑦 + 𝑦 + 𝑦 ) b) Evaluate the integral A2 = 4 – 14 h =2
A = A1 + A2
= 1.4108
ii) Simpson’s one – third rule = 1.3662 𝐼= sin 𝑥 𝑑𝑥 = 2
ii) Simpson’s three – eighth rule = 1.3571
Using Simpson’s 3/8 – rule. [Take n = 6]
19 20
x 1 2 3 4 6 8 10 12 14
6/14/2021
Exercise: Exercise
a) Derive Newton- Cotes quadrature formula for integration and use it
to obtain the Simpson’s 3/8th – rule. # Evaluate the integral
b) Evaluate 1
𝑒 𝑑𝑥
𝐼= 𝑑𝑥
1+𝑥
By using i) Trapezoidal Rule, ii) Simpson’s 1/3 – rule and iii)
Simpson’s 3/8 – Rule. [Take n = 6] Using Simpson’s one – third rule. Taking h = 0.1.
c) Evaluate the integral
Hence find an approximate value of 𝜋 (pie)
correct to 3 decimal places.
sin 𝑥 𝑑𝑥
Using Simpson’s 1/3 – rule. [Take n = 6]
21 22
Romberg’s Method
𝐼 = 0.7853
1 𝐼 ℎ 4
Now, 𝐼 ℎ, ⁄ 6
1
𝐼= 𝑑𝑥 2 𝐼 ℎ 2 𝐼 ℎ, ℎ 2 , ℎ 4
1+𝑥 5
𝐼 ⁄ , ⁄
0.7853 = [tan−1(x)] 3 𝐼 ℎ 4
= tan-1(1) – tan-1(0)
= 1
𝐼= (4I − I )
𝜋 = 4 * 0.7853 3
= 3.1412
23 24
6/14/2021
31 32
6/14/2021
Cont’d….. Example
Gauss - Legendre Formula (n = 2) Solution:
𝑓(𝑥) = 𝑒𝑥
This formula will give correct value for integral
Gauss formula for 𝑛 = 2, we have
of 𝑓(𝑥) in the range (-1, 1) for any function.
𝑤1 = 𝑤2 = 1,
1
𝑥 =−
# Compute ∫ 𝑒 𝑑𝑥 using 2 – point Gauss – 3
Legendre formula. x2 =
I = 2.3504 Ig =∫ 𝑓(𝑥) 𝑑𝑥
= ∑ 𝑤 𝑓(𝑥 )
33 =𝑤 𝑓 𝑥 +𝑤 𝑓 𝑥 34
Example Solution:
𝑓 𝑥 = 3𝑥 + 8
Gauss 2 point. We have,
𝐼 =1∗𝑒 +1∗𝑒 𝑊 =𝑊 =1
1
Ig = 1*exp(-1/sqrt(3))+1*exp(1/sqrt(3)) 𝑥 =−
Ig = 2.3427 3
1
Hence, the integral value of ∫ 𝑒 𝑑𝑥 = 2.3427 𝑥 =
3
𝑛=2
# Compute ∫ 𝑒 𝑑𝑥 using Gaussian 3 – point formula.
I = 2.35034 ∴𝐼 = 𝑓 𝑥 𝑑𝑥
# Compute ∫ (3𝑥 + 8)𝑑𝑥 using Gaussian 2 – point formula. =∑ 𝑊𝑓 𝑥
=𝑤 𝑓 𝑥 +𝑤 𝑓 𝑥
Calculated value : 16.0000 = 3𝑥 + 8 + 3𝑥 + 8
True value: 16.0000
= (3 ∗ − + 8) + (3 ∗ + 8)
Absolute error = |𝑡𝑟𝑢𝑒 𝑣𝑎𝑙𝑢𝑒 − 𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒𝑑 𝑣𝑎𝑙𝑢𝑒|
= 7.4226 +8.5774
35 = 16.00 36
6/14/2021
𝑏−𝑎 𝑏−𝑎
𝑑𝑥 = 𝑑𝑧 𝑐=
2 2
37 38
Example Example::Solution
# Compute the integral Solution:
We first change the limits of the integration (-2,2) to (-1,1) by
transformation.
𝑒 𝑑𝑥
Given,
𝑎 = −2, 𝑏 = 2
Using Gaussian 2 – point formula.
𝑓(𝑥) = 𝑒
We have,
𝑏−𝑎 𝑎+𝑏
𝑥= 𝑧+
2 2
𝑥 = 2𝑧 + 0
𝑑𝑥 = 2𝑑𝑧
39 40
6/14/2021
Example::Solution Example::Solution
For Gaussian 2 – point formula, we have
Now, n = 2, 𝑤 = 𝑤 = 1
𝑔 𝑧 =𝑓 𝑥 𝑧 = − ,𝑧 =
𝑏−𝑎
𝑐= =2
=𝑒 2
Then, the integral becomes,
=𝑒
𝐼 =𝑐 𝑤 𝑔(𝑧 )
=𝑒
=𝑐 𝑤 𝑔(𝑧 )
= 𝑐 𝑤 ∗ 𝑔 𝑧 + 𝑤 ∗ 𝑔(𝑧 )
= 2 1∗𝑒 +1∗𝑒
41 = 4.6854 42
Exercise
# Use Gauss – Legendre 2 – point formula to
evaluate
𝑥 + 1 𝑑𝑥
43
6/4/2021
Numerical Differentiation
• Numerical differentiation deals with the
following problem:
Numerical Differentiation given the function 𝑦 = 𝑓 (𝑥) find one of its
derivatives at the point 𝑥 = 𝑥 .
B. D. Mulmi • Here, the term given implies that we either have
an algorithm for computing the function, or
possesses a set of discrete data points
September, 2020 (𝑥𝑖, 𝑦𝑖), 𝑖 = 1, 2, … . , 𝑛.
1 2
3 4
6/4/2021
𝑦𝑖 = 𝑓 (𝑥 ), 𝑖 = 0, 1, … . , 𝑛.
𝑝 −𝑝 𝑝 − 3𝑝 + 2𝑝
Let 𝑥𝑖 = 𝑥0 + 𝑖ℎ, and 𝑝 = (𝑥 – 𝑥0)/ℎ, 𝑦 = 𝑦 + 𝑝∆𝑦 +
2!
∆ 𝑦 +
3!
∆ 𝑦
where h is the spacing. +
𝑝 − 6𝑝 + 11𝑝 − 6𝑝 ∆ 𝑦
+⋯ 2
4!
The Newton’s forward interpolation formula is
Differentiate eqn(2) 𝑤. 𝑟. 𝑡 𝑝, we get,
5 6
Cont’d… Cont’d…
Differentiating eqn (2) 𝑤. 𝑟. 𝑡 𝑝. We get, Now,
𝑑𝑦 2𝑝 − 1 3𝑝 − 6𝑝 + 2 𝑑𝑦 𝑑𝑦 𝑑𝑝
= ∆𝑦 + ∆ 𝑦 + ∆ 𝑦 + = ∗
𝑑𝑝 2! 3! 𝑑𝑥 𝑑𝑝 𝑑𝑥
∆
4𝑝 − 18𝑝 + 22𝑝 − 6 ∆ 𝑦 = ∆𝑦 + ∆ 𝑦 + ∆ 𝑦 + +⋯ (4)
+⋯ ! ! !
4!
Since, At 𝑥 = 𝑥 , 𝑝 = 0. Hence putting 𝑝 = 0, in eqn (4)
𝑥−𝑥 = ∆𝑦 − ∆ 𝑦 + ∆ 𝑦 − ∆ 𝑦 + ∆ 𝑦 − ⋯ . (5)
𝑝=
ℎ
𝑑𝑝 1
∴ =
𝑑𝑥 ℎ
7 8
6/4/2021
Cont’d… Cont’d…
Again, Since ,
x x0
Differentiating Eqn (4) 𝑤. 𝑟. 𝑡. 𝑝. We get, p
h
dp 1
𝑑 𝑑𝑦 1 6𝑝 − 6 12𝑝 − 36𝑝 + 22
= [∆ 𝑦 + ∆ 𝑦 + ∆ 𝑦 + ⋯] dx h
𝑑𝑝 𝑑𝑥 ℎ 3! 4!
Now,
d 2 y dp d dy
x
dx 2 dx dp dx
1 1 2 2 6p 6 3 12 p 2 36 p 22 4
x y0 y0 y 0 .....
h h 2! 3! 4!
1 2 6 p 2
18 p 11
2 y 0 ( p 1) 3 y 0 4 y 0 ..... (6 )
h 12
9 10
# Derive the expression to find the first and second derivative based on Newton’s
Backward Interpolation formula.
11 12
6/4/2021
Cont’d… Example
dy 1 ( 2 p 1) 2 3p2 6 p 2 3 4 p 3 18 p 2 22 p 6 4
# From the following table, find 𝑑𝑦/𝑑𝑥 and
y n
dx h 2!
yn
3!
yn
4!
y n .....
( 2)
𝑑2𝑦/𝑑𝑥2 at 𝑥 = 1 , 2 and 4.2
At x x n , p 0. Hence putting p 0,
dy 1 1 2 1 3 1 4
y n y n y n y n ..... (3)
𝑥 1 2 3 4 5
dx xn h 2 3 4
𝒚 11 30 85 194 375
d2y 1 11 5 137 6
2 2 2 y n 3 y n 4 y n 5 y n y n ... ( 4) Solution: 𝑑𝑦
dx xn h 12 6 180 The Difference table is: 𝑥 = 1 = 156.76
𝑑𝑥 .
𝑥 = 1, ℎ = 1
∴𝑝=0 𝑦 = 3𝑥 − 2𝑥 + 10
𝑊𝑒 ℎ𝑎𝑣𝑒,
𝑑𝑦 1 1 1
= ∆𝑦 − ∆ 𝑦 + ∆ 𝑦 𝑑 𝑦
𝑑𝑥 ℎ 2 3 = 18
𝑑𝑥
𝑑𝑦
13 =7 14
𝑑𝑥
Example Exercise:
# A slider in a machine moves along a fixed straight rod. Its # A rod is rotating in a plane. The following table
distance 𝑥(𝑚) along the rod are given in the following table for
various values of the time 𝑡 (𝑠𝑒𝑐𝑜𝑛𝑑𝑠). gives the angle 𝜃 (radians) through which the
rod is turned for various values of the time
𝒕(𝒔𝒆𝒄. ) 1 2 3 4 5 6
𝒙(𝒎) 0.0201 0.0844 0.3444 1.0100 2.3660 4.7719 𝑡 second:
Find the velocity and acceleration of the slider at time 𝒕 0.0 0.2 0.4 0.6 0.8 1.0 1.2
𝑡 = 6 sec. 𝜽 0 0.12 0.49 1.12 2.02 3.20 4.67
𝑑𝑥
= 3.0693 𝑚/𝑠 Calculate the angular velocity and the angular acceleration of
𝑑𝑡
𝑑 𝑥 the rod, when 𝑡 = 0.2 and 1.0 second.
= 1.4777𝑚/𝑠
𝑑𝑡
15 16
6/4/2021
17 18
19 20
6/4/2021
Example Exercise:
On solving,
𝑝 = 2.6875 Find 𝑥 correct to 4 decimal places for which 𝑦 is
𝑊𝑒 ℎ𝑎𝑣𝑒, maximum for the following data.
𝑥−𝑥
𝑝 = x 1.0 1.2 1.4 1.6 1.8
ℎ
𝑥 = 5.6875 y 0 0.128 0.544 1.298 2.44
To find maximum 𝑦.
Newton’s forward interpolation formula. We have,
( ) ( ) ( )
𝑦 = 𝑦 + 𝑝∆𝑦 + ∆ 𝑦 + ∆ 𝑦 + ∆ 𝑦 +
! ! !
…
𝑦 = 0.2628
21 22