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

Numerical Method V2

This lab manual covers numerical methods experiments in MATLAB. It includes 10 experiments on topics like introduction to MATLAB, root finding methods, solving non-linear equations, interpolation, differentiation, integration, and solving systems of linear equations. The document provides objectives, learning outcomes, theory, equipment and procedures for each experiment.

Uploaded by

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

Numerical Method V2

This lab manual covers numerical methods experiments in MATLAB. It includes 10 experiments on topics like introduction to MATLAB, root finding methods, solving non-linear equations, interpolation, differentiation, integration, and solving systems of linear equations. The document provides objectives, learning outcomes, theory, equipment and procedures for each experiment.

Uploaded by

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

Lab Manual

EEE-302
Numerical Method

Department of Electrical and Electronic Engineering


Green University of Bangladesh
Contents
Experiment 1 Introduction to MATLAB (Part A) ............................................................................... 3

Experiment 2 Introduction to MATLAB (Part B) ............................................................................... 7

Experiment 3 Iterative Processes for Root Finding (Iterative Method, Aitken’s ∆²


Acceleration Method, Bisection Method) .............................................................. 11

Experiment 4 Solutions to Non-Linear Equations (Secant Method & Regula Falsi .................
Method) .............................................................................................................................. 16

Experiment 5 Interpolation by Newton-Gregory forward difference formula ................... 22

Experiment 6 Interpolation by Newton-Gregory backward difference formula................ 27

Experiment 7 Numerical Differentiation (Based on Newton-Gregory Forward &


Backward Differences) ................................................................................................. 32

Experiment 8 Numerical Differentiation (Based on Lagrange Interpolation) & Numerical


Integration (Based on Simple Trapezium Rule) ................................................. 38

Experiment 9 Numerical Integration by Romberg Integration and Simpson's 1/3rd, 3/8th


Rule ...................................................................................................................................... 43

Experiment 10 Solution of system of Linear Equations By a direct method (LU


Decomposition) and an indirect method (Jacobi or Gauss Seidel Metho) 50
Experiment 1
Introduction to MATLAB (Part A)

1.1 Objectives
1. Explain basic features configurations and applications of some signals in digital
domain.
2. Familiar with the practical implementation of digital signals.
3. Explain different digital systems and their properties.
4. Explain a system through difference equation in digital domain.
5. Manipulate frequency response of a system in digital domain.
6. Justify the system transfer function through different transformation techniques in
digital domain.
7. Recognize the applications of Time and Frequency domain analysis and advanced
signal processing aspects.

1.2 Learning Outcome


After completing this experiment the students will be able to:

1. Explain different signals and systems in digital domain.


2. Explain features, configurations of various systems in digital domain.
3. Utilize system properties through Time-domain analysis and Frequency-domain
analysis in digital domain.
4. Measure system transfer function from the different transformations analysis in
digital domain.
5. Explain the applications of different transformations analysis in digital domain.
6. Recognize these concepts on courses like Advanced Numerical Methods, Control
Systems, communication theory etc.

Page 3 of 60
1.3 Theory
1.3.1 Starting MATLAB

1. You can start MATLAB R2020a on Microsoft Windows Platform (Win7/Win10) by


double clicking the MATLAB shortcut icon on your windows desktop or simply click
MATLAB R2020a from the start menu.
2. As an alternative method, click on the Start button then type matlab in the ‘search
field’ then press Enter. MATLAB will start immediately.
3. After MATLAB starts, you can change the directory in which MATLAB saves your
MATLAB files. To do this, click on ‘…’ button then select the new location.

1.3.2 Desktop Tools

1. The Command Window: To enter variables, execute commands and to run M-files.
2. Menus:
I. File: from the file menu, you can create a new M-file, figure…etc. You can also
open any file and you can access the preferences of MATLAB.
II. Edit: cut, copy, paste...etc.
III. Desktop: to control the desktop of MATLAB.
IV. Tip: to restore the default desktop go to Desktop →Desktop Layout→default
V. Window: to get access to the windows/files e.g. the open M-files documents.
3. The Current Directory Browser: any files you want to run must either be in the
current directory or on search path. The current directory browser enables you to
browse all the files saved in the current directory. You can run, rename, delete…etc.
4. Command History: in the command history you can view the previously used
functions and copy and execute selected lines.
5. Lunch Pad: provides easy access to tools, demos, and documentations.
6. Help Browser: to search and view documentations for all your MathWorks products.
7. Workspace Browser: the MATLAB workspace consists of the set of variables
(named arrays) built up during a MATLAB session and stored in memory. To view
the workspace and information about each variable, use the Workspace Browser,
or use the commands who and whos.

Page 4 of 60
8. Array Editor: double click on a variable in the Workspace Browser to see it in the Array
Editor.
9. Editor/Debugger: to create and debug M-files.

List of Equipment:

1. Desktop PC
2. Software MATLAB R2020a

1.4 Procedure
1.4.1 First Steps in MATLAB

When MATLAB starts, the special >> prompt (the command line) appears, MATLAB is ready
to receive your commands. Try to compute c=a+b, where a=10, and b=20.

MATLAB Command Details


Simple matrix generation
X=[1:10]
Ans: 1 2 3 4 5 6 7 8 9 10
single row matrix generation. [start: step:
X=[1:1:10] End]
Ans: 1 2 3 4 5 6 7 8 9 10
X= [1:2:10] Ans: 1 3 5 7 9
Single row matrix generation. Formation by
Y=linspace(0,10,5)
[start, end, no of data]
T=2*pi*50 for MATLAB, 𝜋 = 𝑝𝑖, T=2 𝜋f
MATLAB angle values are in Radians.

sin(30*pi/180) Radians to Degree conversion-


180
𝐷𝑒𝑔𝑟𝑒𝑒 = * Radian
𝜋

asin(1/2)*180*pi inverse trigonometry


10*exp(-2) ‘exp’ means exponential; 10𝑒 −2
9e-5 ‘e’ denoted power of 10; 9 ∗ 10−5
date date on MATLAB
calendar calendar command on MATLAB

Page 5 of 60
Logarithm Command on MATLAB-
(log10(10))^4 logBasePower; Example: log10 10
log10 means= log 𝑒 10 (default)
(log2(5))^4 2 Base Logarithm
(log(10))^4 e base logarithm
X= 0:2:16; Y=2*X; Matrix Algebraic Calculations
T=linspace(0,2*pi*100); Plotting on MATLAB
X=sin(T); Y=cos(T); plotting X and Y respect to T
plot(X)
plot(Y)

subplot(3,1,1) Subplot

plot(X) 1,1 1,2 1,3


subplot(3,1,2) 2,1 2,2 2,3
plot(Y) 3,1 3,2 3,3
subplot(3,1,3) Formation of subplot(Row, column,
plot(X,Y) position)
plot(X,’-r’) Styling of graph-
hold on plot(X,’linestyle_charecter_color_code’)
plot(Y,’-b’) “magenta”----------“m”
hold on “Yellow"------------ “y”
plot(X,Y,’:k’) “black”------------- “k”
legend(‘sin’,’cos’,circle,0) Graph notation
Report Question

1
1. sin(225°) + cot(30°) + tan−1 (2) + 10𝑒 −10 + 9 ∗ 10−2 + (log10 10)3 + (log 𝑒 10)5

2. Think about A and B are the last two digits of your ID-
𝐵
sin(𝐴𝐵°) + cot(𝐵𝐴°) + tan−1 + 𝐵𝑒 −𝐵𝐴 + 𝐴 ∗ 10−𝐴 + (log10 𝐵)𝐴𝐵 + (log 𝑒 𝐵𝐴) 𝐴
𝐴

Page 6 of 60
Experiment 2
Introduction to MATLAB (Part B)

2.1 Objectives
1. Explain basic features configurations and applications of some signals in digital
domain.
2. Familiar with the practical implementation of digital signals.
3. Explain different digital systems and their properties.
4. Explain a system through difference equation in digital domain.
5. Manipulate frequency response of a system in digital domain.
6. Justify the system transfer function through different transformation techniques in
digital domain.
7. Recognize the applications of Time and Frequency domain analysis and advanced
signal processing aspects

2.2 Learning Outcome


After completing this experiment, the students will be able to:

1. Explain different signals and systems in digital domain.


2. Explain features, configurations of various systems in digital domain.
3. Utilize system properties through Time-domain analysis and Frequency-domain
analysis in digital domain.
4. Measure system transfer function from the different transformations analysis in
digital domain.
5. Explain the applications of different transformations analysis in digital domain.
6. Recognize these concepts on courses like Advanced Numerical Methods, Control
Systems, communication theory etc.

Page 7 of 60
2.3 Theory
2.3.1 −File

M-files provide an easy way to write and excite your commands and programs. For a large
number of commands and complex problem-solving M-files is a must. It allows you to place
MATLAB command in a simple text file and then tell MATLAB to open the file and execute the
commands exactly as it would if you typed them at the MATLAB Command Window.

M-Files must end with the extension ‘.m’. For example, homework1.m

There are many ways to load M-file Editor:

1. Click on start → programs →MATLAB  R2020a →M-file Editor


2. From the MATLAB, chose New from the Home tab and select Script.

2.4 List of Equipment


1. Desktop PC
2. Software MATLAB R2020a

2.5 Procedure
2.5.1 Matrix Operations

To type a matrix into MATLAB you must:

• Begin with a “square bracket ( [ )”.


• Separate elements in a row with commas or spaces.
• Use a semicolon (;) to separate rows
• End the matrix with another “square bracket ( ] )”.

Operation Matrix Command Details


Simple Matrix a=[1,-2,0;10,-6,2;1,11,-9] or a=[1 -2 0; 1 −2 0
10 -6 2; 1 11 -9] 10 −6 2
1 11 −9
Matrix A=[1 10 200 ; -50 0 -20 ; 44 25 60 ];
B=[1 15 -32 ; 14 20 20 ; 2 5 3];
Operation C=[1 1 1 ; 25 -6 -6; 14 89 300];
A+B+C
A+B-C

Page 8 of 60
A+B-C-10
A/C
A*B*C
2*A
A^2
A.^2
Matrix zeros(3,3)
ones(3,2)
Generation rand(3,3)
Matrix Index B=[0 20 90; 12 -34 45]
B(2,3)
B(1,2)
B(2,2)
Transpose, W=[1 2 3; -4 -5 -6; 0 1 0] W’=Transpose Matrix
W'
Determinant det(W) det(W)=Determinant of W
and Inverse inv(W) inv(W)= inverse matrix of W
Matrix
For Loop for i = 1:2:10 For index = expression
x = i^2 Statement group x
end End

The output of this code: x = 1,


9,25,49,81.
Nested For m=3; The output of this code: f =
n=5; 1 1 1 1 1
Loop for i=1:m 2 2 2 2 2
for j=1:n 3 3 3 3 3
f(i,j)=i;
end
end
f

While Loop i=1 Constant value 1 (i)


d=0 constant value 2 (d)
while i<5 % i>1 while condition
d=i+1 operation
i=d end
end
disp('loop ended') The output of this code: d=5, i = 5
d
i
Relation a=5; Rational operator description
b=6; < Less than.
Operator & If if a~=b <= Less than or equal.

Page 9 of 60
Else disp('unequal') > Greater than.
else >= Greater than or equal to.
Statement disp('equal') == Equal to.
end ~= Not equal to.

Logical x=4; y=6; Logical Operators with IF Else


if x<1 & y<1 Statement:
Operator z=0 Symbol Meaning
elseif x>1 & y<1 & AND
z=1 | OR
elseif x>1 | y<1 ˜ NOT
z=2
end
The output of this code: z = 2
Solving Linear 3𝑥 + 10𝑦 − 𝑧 = 0 The output of this code: x = 3.5000, -
−2𝑥 + 𝑦 − 10𝑧 = 2 1.1111, -0.6111
Equation
𝑥+𝑦−𝑧 =3

Method 1
A=[3 10 -1;-2 1 -10; 1 1 -1];
b=[0;-2;3];
x=inv(A)*b
x=A\b

Method 2
syms x y z
[x,y,z]=solve([3*x+10*y-z==0,-
2*x+y-10*z==-2,x+y-z==3],[x,y,z])

Page 10 of 60
Experiment 3
Iterative Processes for Root Finding (Iterative Method, Aitken’s
∆² Acceleration Method, Bisection Method)

3.1 Objective
1. Explain basic features configurations and applications of some signals in digital
domain.
2. Familiar with the practical implementation of Iterative Method.
3. Familiar with the practical implementation of Aitken’s ∆² Acceleration Method.
4. Familiar with the practical implementation of Bisection Method.
5. Explain different digital systems and their properties.
6. Explain a system through difference equation in digital domain.
7. Manipulate frequency response of a system in digital domain.

3.2 Learning Outcome


After completing this experiment the students will be able to:

1. Define Middle Term Breaking Method


2. Define Fixed Point Iteration to Root Finding
3. Define Iterative Process to Locate the System Root.
4. Define Aitken’s ∆² Acceleration Method to Locate the System Root.
5. Define Bisection Process to Locate the System Root.

3.3 Theory
Script file:

Page 11 of 60
A formula can be developed for simple fixed-point iteration by arranging the function 𝑓(𝑥) =
0so that 𝑥 is on the left hand side of the equation:

𝑥 = 𝑔(𝑥)

Eq. (1) can be used to compute a new estimate 𝑥𝑖+1 as expressed by the iterative formula

𝑥𝑖+1 = 𝑔(𝑥𝑖 )

The approximate error for this eq. (2) can be determined using the error estimator:
𝑥𝑖+1 − 𝑥𝑖
|e𝑎 | = | | 100%
𝑥𝑖+1

3.3.1 Aitken's Method for Acceleration (Δ2Method)


(∆𝑥𝑛 )2
𝐴𝑖𝑡𝑘𝑒𝑛, 𝛼 = 𝑥𝑛+1 −
∆2 𝑥𝑛−1

Here,
∆𝑥𝑛 = 𝑥𝑛+1 − 𝑥𝑛

∆2 𝑥𝑛−1 = 𝑥𝑛+1 − 2𝑥𝑛 + 𝑥𝑛−1


3.3.2 Bisection Method

The Bisection method is one of the simplest procedures for finding root of a function in a
given interval. The procedure is straightforward. The approximate location of the root is first
determined by finding two values that bracket the root (a root is bracketed or enclosed if the
function changes sign at the endpoints). Based on these a third value is calculated which is
closer to the root than the original two value. A check is made to see if the new value is a root.
Otherwise a new pair of bracket is generated from the three values, and the procedure is
repeated.

Page 12 of 60
Consider a function d ( x) and let there be two values of x , xlow and xup ( xup > xlow ), bracketing

a root of d ( x) . The first step is to use the brackets xlow and xup to generate a third value that

is closer to the root. This new point is calculated as the mid-point between xlow and, namely

xlow + xup
xmid = . The method therefore gets its name from this bisecting of two values. It is
2
also known as interval halving method. Test whether xmid is a root of d ( x) by evaluating the

function at xmid . If xmid is not a root, then check if d ( xlow ) and d ( xmid ) have opposite signs i.e.

d ( xlow ) . d ( xmid ) <0, root is in left half of interval. Or if d ( xlow ) and d ( xmid ) have same signs i.e.

d ( xlow ) . d ( xmid ) >0, root is in right half of interval. Continue subdividing until interval width
𝑥𝑢𝑝 −𝑥𝑙𝑜𝑤
( ) has been reduced to a size < tolerance. Tips: tolerance shall be 1x10-4 .
2

3.4 Procedure
3.4.1 Iterative Process Steps

1. The first step choice an initial approximation 𝑥𝑖𝑛𝑖𝑡𝑖𝑎𝑙 = 𝑥𝑜𝑙𝑑


2. Evaluate 𝑓(𝑥) equation and determine the value of 𝑓(𝑥𝑜𝑙𝑑 )
3. Let 𝑥𝑛𝑒𝑤 = 𝑓(𝑥𝑜𝑙𝑑 )
𝑥𝑛𝑒𝑤 −𝑥𝑜𝑙𝑑
4. Check %𝑒𝑟𝑟𝑜𝑟 = | | ∗ 100%
𝑥𝑛𝑒𝑤

5. If %𝑒𝑟𝑟𝑜𝑟 > 1% , then put 𝑥𝑜𝑙𝑑 = 𝑥𝑛𝑒𝑤 and repeat steps (2) to (4)
6. Continue evaluating steps (2) to (4) until %𝑒𝑟𝑟𝑜𝑟 has been reduced to a value  1
7. If %𝑒𝑟𝑟𝑜𝑟 < 1% , then 𝑅𝑜𝑜𝑡 = 𝑥𝑛𝑒𝑤 and stop evaluating iterations.

Page 13 of 60
3.4.2 Aitken's Method for Acceleration (Δ2Method) Steps

1. The first step choice an initial approximation 𝑥𝑖𝑛𝑖𝑡𝑖𝑎𝑙 = 𝑥1


2. Evaluate 𝑓(𝑥) equation and determine the values 𝑥2 = 𝑓(𝑥1 ) , 𝑥3 = 𝑓(𝑥2 )
3. Determine the value of ∆𝑥2 = 𝑥3 − 𝑥2
4. Determine the value of ∆2 𝑥1 = 𝑥3 − 2𝑥2 + 𝑥1
(∆𝑥2 )2
5. Determine the value of 𝐴𝑖𝑡𝑘𝑒𝑛, 𝑎 = 𝑥3 − ∆2 𝑥1

6. Check accuracy = |𝑎 − 𝑥3 |
7. If 𝑎𝑐𝑐𝑢𝑟𝑎𝑐𝑦 > 1 , then put 𝑥1 = 𝑎 and repeat steps (2) to (6)
8. Continue evaluating steps (2) to (6) until accuracy has been reduced to a value < 1
9. If 𝑎𝑐𝑐𝑢𝑟𝑎𝑐𝑦 < 1 , then Aitken’s 𝑅𝑜𝑜𝑡 = 𝑎 and stop evaluating iterations.

3.4.3 Bisection method Steps

1. The first step choice the initial approximations 𝑥𝑢𝑝 and 𝑥𝑙𝑜𝑤
𝑥𝑢𝑝 + 𝑥𝑙𝑜𝑤
2. Determine the value of 𝑥𝑚𝑖𝑑𝑜𝑙𝑑 = 2

3. Determine the values 𝑓(𝑥𝑙𝑜𝑤 ) & 𝑓(𝑥𝑚𝑖𝑑𝑜𝑙𝑑 )


4. Check if 𝑓(𝑥𝑙𝑜𝑤 ) ∗ 𝑓(𝑥𝑚𝑖𝑑𝑜𝑙𝑑 ) > 0 then change 𝑥𝑙𝑜𝑤 = 𝑥𝑚𝑖𝑑𝑜𝑙𝑑
5. OR if 𝑓(𝑥𝑙𝑜𝑤 ) ∗ 𝑓(𝑥𝑚𝑖𝑑𝑜𝑙𝑑 ) < 0 then change 𝑥𝑢𝑝 = 𝑥𝑚𝑖𝑑𝑜𝑙𝑑
𝑥𝑢𝑝 + 𝑥𝑙𝑜𝑤
6. Determine the value of 𝑥𝑚𝑖𝑑𝑛𝑒𝑤 = 2
𝑥𝑢𝑝 − 𝑥𝑙𝑜𝑤
7. Check 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2

8. If 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 > 1 then change 𝑥𝑚𝑖𝑑𝑜𝑙𝑑 = 𝑥𝑚𝑖𝑑𝑛𝑒𝑤 and repeat steps (2) to (6)
9. Continue evaluating steps (2) to (6) until difference has been reduced to a value < 1
10. If 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 < 1 then 𝑅𝑜𝑜𝑡 = 𝑥𝑚𝑖𝑑𝑛𝑒𝑤 and stop evaluating iterations.

3.5 Report Question


1. Write MATLAB Program of Iterative and Aitken’s Δ2 Method for the following systems:
a) 2𝑥 − 𝑙𝑜𝑔10 (𝑥) − 7 = 0; 𝑥0 = 0.34
1/3
b) 𝑥 = −(1 − 𝑥) ; 𝑥0 = −1.2
c) 2𝑥 2 + 3𝑥 − 1 = 0 ; 𝑥0 = 0.29
1
d) 𝑥 = (4) [(2𝑥 2 − 1)/(𝑥 − 1)] ; 𝑥0 = 0.32
e) 𝑥 − 𝑒 −𝑥 = 0 ; 𝑥0 = 0.56

Page 14 of 60
2. Write MATLAB Program of Bisection Method for the following systems:
a) 𝑥 5 + 𝑥 + 1 = 0; 𝑥𝑢𝑝 = 5, 𝑥𝑙𝑜𝑤 = −5
b) 3𝑥 3 + 5𝑥 2 + 𝑥 − 1 = 0; 𝑥𝑢𝑝 = 6, 𝑥𝑙𝑜𝑤 = −7
c) 𝑥 3 − 3𝑥 − 5 = 0; 𝑥𝑢𝑝 = 5, 𝑥𝑙𝑜𝑤 = −5
d) 𝑥 3 − 0.39𝑥 2 − 10.5𝑥 + 11.0 = 0; 𝑥𝑢𝑝 = 5, 𝑥𝑙𝑜𝑤 = −5

Page 15 of 60
Experiment 4
Solutions to Non-Linear Equations (Secant Method & Regula
Falsi Method)

4.1 Objectives
1. Familiar with the practical implementation of Precision of Root Finding for a System.
2. Familiar with the practical implementation of Regula Falsi Method.
3. Familiar with the practical implementation of Newton Raphson Method.
4. Familiar with the practical implementation of Secant Method.
5. Explain a system through difference equation in digital domain.

4.2 Learning Outcome


After completing this experiment, the students will be able to:

1. Define Precision of Root Finding for a System.


2. Define Regula Falsi Method.
3. Define Newton Raphson Method.
4. Define Secant Method.

4.3 Theory
4.3.1 Regula Falsi Method

A shortcoming of the bisection method is that, in dividing the interval from xlow to xup into

equal halves, no account is taken of the magnitude of f ( xlow ) and f ( xup ) . For example, if

f ( xlow ) is much closer to zero than f ( xup ) , it is likely that the root is closer to xlow than to xup

. An alternative method that exploits this graphical insight is to join f ( xlow ) and f ( xup ) by a

straight line. The intersection of this line with the x axis represents an improved estimate of
the root. The fact that the replacement of the curve by a straight line gives the false position
of the root is the origin of the name, method of false position, or in Latin, Regula Falsi. It is
also called the Linear Interpolation Method.

Page 16 of 60
Using similar triangles, the intersection of the straight line with the x axis can be estimated

f ( xlow ) f ( xup )
=
x − xlow x − xup
That is

f ( xup )( xlow − xup )


x = xup −
f ( xlow ) − f ( xup )
This is the False Position formulae. The value of x then replaces whichever of the two initial
guesses, xlow or xup , yields a function value with the same sign as f (x) . In this way, the

values of xlow and xup always bracket the true root. The process is repeated until the root is

estimated adequately.

4.3.2 Newton-Raphson method


Newton–Raphson method, named after Isaac Newton and Joseph Raphson, is a method for finding
successively better approximations to the roots (or zeroes) of a real-valued function. It is one
example of a root-finding algorithm.

𝑥: 𝑓(𝑥) = 0

Page 17 of 60
The method starts with a function f defined over the real numbers x, the function's derivative f ′,
and an initial guess x0 for a root of the function f. If the function satisfies the assumptions made in
the derivation of the formula and the initial guess is close, then a better approximation x1 is

𝑓(𝑥0 )
𝑥1 = 𝑥0 −
𝑓 ′ (𝑥0 )

Geometrically, (x1, 0) is the intersection of the x-axis and the tangent of the graph of f at (x0,
f (x0)).

The process is repeated as

𝑓(𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
𝑓 ′ (𝑥𝑛 )
Until a sufficiently accurate value is reached.

4.3.3 The Secant Method

The secant method can be coded so that only one new function evaluation is required per
iteration. The formula for the secant method is the same one that was used in the Regula
Falsi method, except that the logical decisions regarding how to define each succeeding term
are different.

Page 18 of 60
In the Secant method, the derivative can be approximated by a backward finite divided
difference, as in the figure,

f ( x k −1 ) − f ( x k )
f ( x k ) 
x k −1 − x k

Using Newton-Raphson method,

f ( xk )
x k +1 = x k −
f ( x k )

Substituting, f ( xk )

f ( x k )( x k −1 − x k )
x k +1 = x k −
f ( x k −1 ) − f ( x k )

Notice that the approach requires initial estimates of 𝑥𝑘 , 𝑥𝑘−1.

4.4 Procedure
4.4.1 Regula Falsi Method Steps

1. At first choice initial approximation 𝑥1 (bigger one) and 𝑥2 (smaller one)


2. Evaluate 𝑓(𝑥) equation and determine the values 𝑓(𝑥1 ) & 𝑓(𝑥2 )
𝑓(𝑥1 )∗(𝑥2 −𝑥1 )
3. Determine the value, 𝑥 = 𝑥1 − 𝑓(𝑥2 )−𝑓(𝑥1 )

Page 19 of 60
4. Check if |𝑓(𝑥1 )| < |𝑓(𝑥2 )| then change 𝑥2 = 𝑥1 , 𝑥1 = 𝑥
if |𝑓(𝑥1 )| > |𝑓(𝑥2 )| then change 𝑥1 = 𝑥2 , 𝑥2 = 𝑥
5. Find 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = |𝑥1 − 𝑥2 |
6. If 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 > 1 then repeat steps (2) to (5)
7. Continue evaluating steps (2) to (5) until difference has been reduced to a value < 1
8. If 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 < 1 then 𝑅𝑜𝑜𝑡 = 𝑥 and stop evaluating iterations.

4.4.2 Newton-Raphson method Steps

1. At first choice initial approximation 𝑥1


2. Evaluate 𝑓(𝑥) equation and determine the values 𝑓(𝑥1 ) & 𝑓 ′ (𝑥1 )
𝑓(𝑥 )
3. Determine the value, 𝑥2 = 𝑥1 − 𝑓′ (𝑥1 )
1

4. Find 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = |𝑥2 − 𝑥1 |


5. If 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 > 1 then repeat steps (2) to (4)
6. Continue evaluating steps (2) to (4) until difference has been reduced to a value < 1
7. If 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 < 1 then 𝑅𝑜𝑜𝑡 = 𝑥2 and stop evaluating iterations.

4.4.3 The Secant Method Steps

1. At first choice initial approximation 𝑥1 (bigger one) and 𝑥2 (smaller one)


2. Evaluate 𝑓(𝑥) equation and determine the values 𝑓(𝑥1 ) & 𝑓(𝑥2 )
𝑓(𝑥2 )∗(𝑥1 −𝑥2 )
3. Determine the value, 𝑥3 = 𝑥2 − 𝑓(𝑥1 )−𝑓(𝑥2 )

4. Find 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = |𝑥3 − 𝑥2 |


5. If 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 > 1 then repeat steps (2) to (4) and change 𝑥1 = 𝑥2 , 𝑥2 = 𝑥3
6. Continue evaluating steps (2) to (4) until difference has been reduced to a value < 1
7. If 𝑑𝑖𝑓𝑓𝑒𝑟𝑒𝑛𝑐𝑒 < 1 then 𝑅𝑜𝑜𝑡 = 𝑥3 and stop evaluating iterations.

4.5 Report Question

1. Write MATLAB Program of Regula Falsi, Secant, Newton-Raphson Methods for the
following systems: (here, 𝑥1 , 𝑥2 for Regula Falsi and Secant; 𝑥𝑖𝑛𝑖𝑡𝑖𝑎𝑙 for Newton-
Raphson)
i. 𝑥 5 + 𝑥 + 1 = 0; 𝑥1 = 5, 𝑥2 = −5 & 𝑥𝑖𝑛𝑖𝑡𝑖𝑎𝑙 = −0.8

Page 20 of 60
ii. 3𝑥 3 + 5𝑥 2 + 𝑥 − 1 = 0; 𝑥1 = 6, 𝑥2 = −7 & 𝑥𝑖𝑛𝑖𝑡𝑖𝑎𝑙 = −0.7
iii. 𝑥 3 − 3𝑥 − 5 = 0; 𝑥1 = 5, 𝑥2 = −5 & 𝑥𝑖𝑛𝑖𝑡𝑖𝑎𝑙 = 2
iv. 𝑥 3 − 0.39𝑥 2 − 10.5𝑥 + 11.0 = 0; 𝑥1 = 5, 𝑥2 = −5 & 𝑥𝑖𝑛𝑖𝑡𝑖𝑎𝑙 = 1.8

Page 21 of 60
Experiment 5
Interpolation by Newton-Gregory forward difference formula

5.1 Objectives
1. Familiar with the practical implementation of Precision of Root Finding for a System.
2. Familiar with the practical implementation of Interpolation by Newton-Gregory
forward difference formula.
3. Familiar with the Merits and Demerits of Interpolation by Newton-Gregory forward
difference formula.
4. Explain a system through difference equation in digital domain.

5.2 Learning Outcome


After completing this experiment the students will be able to:

1. Define Precision of Root Finding for a System.


2. Define Interpolation by Newton-Gregory forward difference formula.

5.3 Theory
We are familiar with the analytical method of finding the derivative of a function when the
functional relation between the dependent variable y and the independent variable x is
known. However, in practice, most often functions are defined only by tabulated data, or the
values of y for specified values of x can be found experimentally. Also in some cases, it is not
possible to find the derivative of a function by analytical method. In such cases, the analytical
process of differentiation breaks down and some numerical process have to be invented. The
process of calculating the derivatives of a function by means of a set of given values of that
function is called numerical differentiation. This process consists in replacing a complicated
or an unknown function by an interpolation polynomial and then differentiating this
polynomial as many times as desired.

Page 22 of 60
5.3.1 Forward Difference Formula

All numerical differentiation are done by expansion of Taylor series

𝑓′′(𝑥𝑖 ) 2
𝑓(𝑥𝑖−1 ) = 𝑓(𝑥𝑖 ) + 𝑓′(𝑥𝑖 )ℎ + ℎ
2!

From (1)

𝑓(𝑥𝑖+1 ) − 𝑓(𝑥𝑖 )
𝑓′(𝑥𝑖 ) ≅ + 𝑂(ℎ)

Where, O(h) is the truncation error, which consists of terms containing h and higher order
terms of h.

𝑓(𝑥𝑖+1 )−𝑓(𝑥𝑖 )
Total or True error = |𝑓 ′ (𝑥) − |………………..(3)

5.3.2 Newton- Gregory Forward Difference Approach

Very often it so happens in practice that the given data set (𝑥𝑖 , 𝑦𝑖 ), 𝑖 = 0,1,2, … , 𝑛 correspond
to a sequence {𝑥𝑖 } of equally spaced points. Here we can assume that

𝑥𝑖 = 𝑥0 + 𝑖ℎ, 𝑖 = 0,1,2, … , 𝑛

where 𝑥0 is the starting point (sometimes, for convenience, the middle data point is taken as
𝑥0 and in such a case the integer 𝑖 is allowed to take both negative and positive values.) and
ℎ is the step size. Further it is enough to calculate simple differences rather than the
divided differences as in the non-uniformly placed data set case. These simple differences
can be forward differences (∆𝑓𝑖 ) or backward differences (∇𝑓𝑖 ). We will first look at forward
differences and the interpolation polynomial based on forward differences.

The first order forward difference ∆𝑓𝑖 is defined as

∆𝑓𝑖 = 𝑓𝑖+1 − 𝑓𝑖

The second order forward difference ∆2 𝑓𝑖 is defined as

Page 23 of 60
∆2 𝑓𝑖 = ∆𝑓𝑖+1 − ∆𝑓𝑖

The third order forward difference ∆3 𝑓𝑖 is defined as

∆3 𝑓𝑖 = ∆2 𝑓𝑖+1 − ∆2 𝑓𝑖

The fourth order forward difference ∆4 𝑓𝑖 is defined as

∆4 𝑓𝑖 = ∆3 𝑓𝑖+1 − ∆3 𝑓𝑖

The kth order forward difference ∆𝑘 𝑓𝑖 is defined as

∆𝑘 𝑓𝑖 = ∆𝑘−1 𝑓𝑖+1 − ∆𝑘−1 𝑓𝑖

This is known as Newton-Gregory forward difference interpolation polynomial. For


convenience while constructing (10) one can first generate a forward difference table and
use the ∆𝑘 𝑓𝑖 from the table. Suppose we have data set (𝑥𝑖 𝑓𝑖 ), 𝑖 = 0,1,2,3,4 then forward
difference table looks as follows:

5.4 Procedure
Given the following data, estimate 𝑓(4.12)using Newton-Gregory forward difference
interpolation polynomial:

Page 24 of 60
Solution

Here we have six data points i.e. 𝑖 = 0,1,2,3,4,5 Let us first generate the forward difference
table.

Here,

We have Newton-Gregory forward difference interpolation formula

𝑘(𝑘 − 1) 2 𝑘(𝑘 − 1)(𝑘 − 2) 3 𝑘(𝑘 − 1)(𝑘 − 2)(𝑘 − 3) 4


𝑓𝑥 = 𝑓0 + 𝑘∆𝑓0 + ∆ 𝑓0 + ∆ 𝑓0 + ∆ 𝑓0
2! 3! 4!
𝑘(𝑘 − 1)(𝑘 − 2)(𝑘 − 3)(𝑘 − 4) 5
+ ∆ 𝑓0
5!

𝑓(4.12) = 17.391338127360001(𝐴𝑛𝑠)

5.4.1 Steps

1. At first create initial approximation matrix 𝑖, 𝑥𝑖 & 𝑓𝑖


2. Evaluate the matrices ∆𝑓𝑖 , ∆2 𝑓𝑖 , ∆3 𝑓𝑖 , ∆4 𝑓𝑖 & ∆5 𝑓𝑖 as required to solve the equation
3. Write MATLAB expression of the formula 𝑓𝑥 with inputs 𝑥, 𝑥0 , ℎ, 𝑘
4. Determine the value, 𝑓(4.12) using MATLAB Program

Page 25 of 60
5.5 Report Question
Determine the value, 𝑓(4.12)using MATLAB Program for Newton-Gregory forward difference
interpolation formula for the following systems, where h=1.

a) 𝑖 = 1,2,3,4; 𝑥𝑖 = 𝑖 + 1; 𝑓𝑖 = (𝑥𝑖 2 + 1) Ans. 17.9744


b) 𝑖 = 2,4,8,12; 𝑥𝑖 = 𝑖/2; 𝑓𝑖 = (𝑥𝑖 2 + |𝑥𝑖 |) Ans. 45.0826

Page 26 of 60
Experiment 6
Interpolation by Newton-Gregory backward difference formula

6.1 Objectives
1. Familiar with the practical implementation of Precision of Root Finding for a System.
2. Familiar with the practical implementation of Interpolation by Newton-Gregory
backward difference formula.
3. Familiar with the Merits and Demerits of Interpolation by Newton-Gregory backward
difference formula.
4. Explain a system through difference equation in digital domain.

6.2 Learning Outcome


After completing this experiment the students will be able to:

1. Define Precision of Root Finding for a System.


2. Define Interpolation by Newton-Gregory backward difference formula.

6.3 Theory
We are familiar with the analytical method of finding the derivative of a function when the
functional relation between the dependent variable y and the independent variable x is
known. However, in practice, most often functions are defined only by tabulated data, or the
values of y for specified values of x can be found experimentally. Also in some cases, it is not
possible to find the derivative of a function by analytical method. In such cases, the analytical
process of differentiation breaks down and some numerical process have to be invented. The
process of calculating the derivatives of a function by means of a set of given values of that
function is called numerical differentiation. This process consists in replacing a complicated
or an unknown function by an interpolation polynomial and then differentiating this
polynomial as many times as desired.

Page 27 of 60
6.3.1 Backward Difference Formula

All numerical differentiation are done by expansion of Taylor series

𝑓′′(𝑥𝑖 )
𝑓(𝑥𝑖−1 ) = 𝑓(𝑥𝑖 ) − 𝑓′(𝑥𝑖 )ℎ + ℎ2 −…………..(1)
2!

From (1)

𝑓(𝑥𝑖 )−𝑓(𝑥𝑖−1 )
𝑓′(𝑥𝑖 ) ≅ + 𝑂(ℎ)……………..(2)

Where, O(h) is the truncation error, which consists of terms containing h and higher order
terms of h.

𝑓(𝑥+ℎ)−𝑓(𝑥)
Total or True error = |𝑓 ′ (𝑥) − |………………..(3)

6.3.2 Newton- Gregory Backward Difference Approach

The first order backward difference ∇𝑓𝑖 is defined as

∇𝑓𝑖 = 𝑓𝑖 − 𝑓𝑖−1

The second order backward difference ∆2 𝑓𝑖 is defined as

∇2 𝑓𝑖 = ∇𝑓𝑖 − ∇𝑓𝑖−1

The third order backward difference ∇3 𝑓𝑖 is defined as

∇3 𝑓𝑖 = ∇2 𝑓𝑖 − ∇2 𝑓𝑖−1

The fourth order backward difference ∇4 𝑓𝑖 is defined as

∇4 𝑓𝑖 = ∇3 𝑓𝑖 − ∇3 𝑓𝑖−1

The kth order backward difference ∇𝑘 𝑓𝑖 is defined as

Page 28 of 60
∇𝑘 𝑓𝑖 = ∇𝑘−1 𝑓𝑖 − ∇𝑘−1 𝑓𝑖−1

This is known as Newton-Gregory backward difference interpolation polynomial. For


convenience while constructing (10) one can first generate a backward difference table and
use the ∇𝑘 𝑓𝑖 from the table. Suppose we have data set (𝑥𝑖 𝑓𝑖 ), 𝑖 = 0,1,2,3,4 then backward
difference table looks as follows:

6.4 Procedure
Given the following data, estimate 𝑓(4.12)using Newton-Gregory backward difference
interpolation polynomial:

Solution:

Here we have six data points i.e. 𝑖 = 0,1,2,3,4,5 Let us first generate the backward difference
table (next page).

Page 29 of 60
Here,

We have Newton-Gregory backward difference interpolation formula

𝑘(𝑘 + 1) 2 𝑘(𝑘 + 1)(𝑘 + 2) 3 𝑘(𝑘 + 1)(𝑘 + 2)(𝑘 + 3) 4


𝑓𝑥 = 𝑓5 + 𝑘∇𝑓5 + ∇ 𝑓5 + ∇ 𝑓5 + ∇ 𝑓5
2! 3! 4!
𝑘(𝑘 + 1)(𝑘 + 2)(𝑘 + 3)(𝑘 + 4) 5
+ ∇ 𝑓5
5!

𝑓(4.12) = 17.391338127360001 (𝐴𝑛𝑠)


6.4.1 Steps

1. At first create initial approximation matrix 𝑖, 𝑥𝑖 & 𝑓𝑖


2. Evaluate the matrices ∇𝑓𝑖 , ∇2 𝑓𝑖 , ∇3 𝑓𝑖 , ∇4 𝑓𝑖 & ∇5 𝑓𝑖 as required to solve the equation
3. Write MATLAB expression of the formula 𝑓𝑥 with inputs 𝑥, 𝑥𝑛 , ℎ, 𝑘
4. Determine the value, 𝑓(4.12) using MATLAB Program

6.5 Report Question


1. Write MATLAB Program of Newton-Gregory backward difference interpolation
formula for the following systems
a) 𝑖 = 1,2,3,4; 𝑥𝑖 = 𝑖 + 1; 𝑓𝑖 = (𝑥𝑖 2 + 1) Ans. 17.9744
b) 𝑖 = 2,4,8,12; 𝑥𝑖 = 𝑖/2; 𝑓𝑖 = (𝑥𝑖 2 + |𝑥𝑖 |) Ans. 7.1914

Page 30 of 60
Page 31 of 60
Experiment 7
Numerical Differentiation (Based on Newton-Gregory Forward
& Backward Differences)

7.1 Objectives
1. Familiar with the practical implementation of numerical differentiation for a System.
2. Familiar with the practical implementation of numerical differentiation by Newton-
Gregory backward difference formula.
3. Familiar with the merits and demerits of numerical differentiation by Newton-
Gregory backward difference formula.
4. Explain a system through difference equation in digital domain.

7.2 Learning Outcome


After completing this experiment the students will be able to:

1. Define Precision of numerical differentiation for a System.


2. Define numerical differentiation by Newton-Gregory backward difference formula.

7.3 Theory
We are familiar with the analytical method of finding the derivative of a function when the
functional relation between the dependent variable y and the independent variable x is
known. However, in practice, most often functions are defined only by tabulated data, or the
values of y for specified values of x can be found experimentally. Also in some cases, it is not
possible to find the derivative of a function by analytical method. In such cases, the analytical
process of differentiation breaks down and some numerical process have to be invented. The
process of calculating the derivatives of a function by means of a set of given values of that
function is called numerical differentiation. This process consists in replacing a complicated
or an unknown function by an interpolation polynomial and then differentiating this
polynomial as many times as desired.

Page 32 of 60
7.4 Forward Difference Formula
All numerical differentiation are done by expansion of Taylor series

𝑓′′(𝑥𝑖 )
𝑓(𝑥𝑖−1 ) = 𝑓(𝑥𝑖 ) + 𝑓′(𝑥𝑖 )ℎ + ℎ2 +…………..(1)
2!
From (1)
𝑓(𝑥𝑖+1 )−𝑓(𝑥𝑖 )
𝑓′(𝑥𝑖 ) ≅ + 𝑂(ℎ)……………..(2)

Where, O(h) is the truncation error, which consists of terms containing h and higher order
terms of h.

𝑓(𝑥𝑖+1 )−𝑓(𝑥𝑖 )
Total or True error = |𝑓 ′ (𝑥) − |………………..(3)

7.4.1 Newton- Gregory Forward Difference Approach

Very often it so happens in practice that the given data set (𝑥𝑖 , 𝑦𝑖 ), 𝑖 = 0,1,2, … , 𝑛 correspond
to a sequence {𝑥𝑖 } of equally spaced points. Here we can assume that

𝑥𝑖 = 𝑥0 + 𝑖ℎ, 𝑖 = 0,1,2, … , 𝑛

where 𝑥0 is the starting point (sometimes, for convenience, the middle data point is taken as
𝑥0 and in such a case the integer 𝑖 is allowed to take both negative and positive values.) and
ℎ is the step size. Further it is enough to calculate simple differences rather than the divided
differences as in the non-uniformly placed data set case. These simple differences can be
forward differences (∆𝑓𝑖 ) or backward differences (∇𝑓𝑖 ). We will first look at forward
differences and the interpolation polynomial based on forward differences.

The first order forward difference ∆𝑓𝑖 is defined as

∆𝑓𝑖 = 𝑓𝑖+1 − 𝑓𝑖

The second order forward difference ∆2 𝑓𝑖 is defined as

∆2 𝑓𝑖 = ∆𝑓𝑖+1 − ∆𝑓𝑖

Page 33 of 60
The third order forward difference ∆3 𝑓𝑖 is defined as

∆3 𝑓𝑖 = ∆2 𝑓𝑖+1 − ∆2 𝑓𝑖

The fourth order forward difference ∆4 𝑓𝑖 is defined as

∆4 𝑓𝑖 = ∆3 𝑓𝑖+1 − ∆3 𝑓𝑖

The kth order forward difference ∆𝑘 𝑓𝑖 is defined as


∆𝑘 𝑓𝑖 = ∆𝑘−1 𝑓𝑖+1 − ∆𝑘−1 𝑓𝑖

This is known as Newton-Gregory forward difference interpolation polynomial. For


convenience while constructing (10) one can first generate a forward difference table and
use the ∆𝑘 𝑓𝑖 from the table. Suppose we have data set (𝑥𝑖 𝑓𝑖 ), 𝑖 = 0,1,2,3,4 then forward
difference table looks as follows:

7.5 Procedure
Given the following data, estimate 𝑓 ′ (4.12)using Newton-Gregory forward difference
interpolation polynomial:

Solution

Page 34 of 60
Here we have six data points i.e. 𝑖 = 0,1,2,3,4,5 Let us first generate the forward difference
table.

Here,

We have Newton-Gregory forward difference interpolation formula

𝑘(𝑘 − 1) 2 𝑘 (𝑘 − 1)(𝑘 − 2) 3
𝑓𝑥 = 𝑓0 + 𝑘∆𝑓0 + ∆ 𝑓0 + ∆ 𝑓0
2! 3!
𝑘 (𝑘 − 1)(𝑘 − 2)(𝑘 − 3) 4 𝑘 (𝑘 − 1)(𝑘 − 2)(𝑘 − 3)(𝑘 − 4) 5
+ ∆ 𝑓0 + ∆ 𝑓0
4! 5!

𝑓(4.12) = 17.391338127360001

Now, [ From, "Numerical Methods" by S.Balachandra Rao, Equation - 5.5 ]

𝑑 𝑑 (𝑓𝑥 ) 𝑑𝑘 1 𝑑 (𝑓𝑥 )
(𝑓𝑥 ) = ∙ = ∙
𝑑𝑥 𝑑𝑘 𝑑𝑥 ℎ 𝑑𝑘

Page 35 of 60
𝑑 𝑑
1 𝑑 (𝑘(𝑘 − 1)) (𝑘(𝑘 − 1)(𝑘 − 2))
∴ 𝑓𝑥′ = [ (𝑘) ∙ ∆𝑓0 + 𝑑𝑘 ∙ ∆2 𝑓0 + 𝑑𝑘 ∙ ∆3 𝑓0
ℎ 𝑑𝑘 2 6

𝑑 𝑑
(𝑘(𝑘 − 1)(𝑘 − 2)(𝑘 − 3)) (𝑘(𝑘 − 1)(𝑘 − 2)(𝑘 − 3)(𝑘 − 4))
+ 𝑑𝑘 4
∙ ∆ 𝑓0 + 𝑑𝑘
24 120

∙ ∆5 𝑓0 ]

1 1 1 1 1 3 11 1
𝑓𝑥′ = [∆𝑓0 + (𝑘 − ) ∆2 𝑓0 + ( 𝑘 2 − 𝑘 + ) ∆3 𝑓0 + ( 𝑘 3 − 𝑘 2 + 𝑘 − ) ∆4 𝑓0
ℎ 2 2 3 6 4 12 4
1 1 7 5 1
+ ( 𝑘 4 − 𝑘 3 + 𝑘 2 − 𝑘 + ) ∆5 𝑓0 ]
24 3 8 6 5

𝑓 ′ (4.12) = 12.085339306666667 (Ans.)

Similarly for Newton-Gregory backward difference interpolation formula

1 1 1 1 1 3 11 1
𝑓𝑥′ = [∇𝑓0 + (𝑘 + ) ∇2 𝑓0 + ( 𝑘 2 + 𝑘 + ) ∇3 𝑓0 + ( 𝑘 3 + 𝑘 2 + 𝑘 + ) ∇4 𝑓0
ℎ 2 2 3 6 4 12 4
1 1 7 5 1
+ ( 𝑘 4 + 𝑘 3 + 𝑘 2 + 𝑘 + ) ∇5 𝑓0 ]
24 3 8 6 5

𝑓 ′ (4.12) = 2.153514459733333 ∗ 102 (Ans. )

7.5.1 Steps

1. At first create initial approximation matrix 𝑖, 𝑥𝑖 & 𝑓𝑖


2. Evaluate the matrices ∇𝑓𝑖 , ∇2 𝑓𝑖 , ∇3 𝑓𝑖 , ∇4 𝑓𝑖 & ∇5 𝑓𝑖 as required to solve the equation

3. Write MATLAB expression of the formula 𝑓𝑥 with inputs 𝑥, 𝑥𝑛 , ℎ, 𝑘
4. Determine the value, 𝑓 ′ (4.12) using MATLAB Program

7.6 Report Question


1. Write MATLAB Program of Numerical Differentiation for Newton-Gregory forward &
backward difference interpolation formula for the following systems

Page 36 of 60
a) 𝑖 = 1,2,3,4; 𝑥𝑖 = 𝑖 + 1; 𝑓𝑖 = (𝑥𝑖 2 + 1) Ans. 8.24 (forward), 8.24 (backward)
b) 𝑖 = 2,4,8,12; 𝑥𝑖 = 𝑖/2; 𝑓𝑖 = (𝑥𝑖 2 + |𝑥𝑖 |) Ans. 26.038933(forward),
10.5189(backward)

Page 37 of 60
Experiment 8
Numerical Differentiation (Based on Lagrange Interpolation) &
Numerical Integration (Based on Simple Trapezium Rule)

8.1 Objectives
1. Familiar with the practical implementation of numerical differentiation and
numerical integration for a System.
2. Familiar with the practical implementation of numerical differentiation based on
Lagrange Interpolation for a System.
3. Familiar with the practical implementation of numerical integration based on Simple
Trapezium Rule for a System.
4. Explain a system through difference equation in digital domain.

8.2 Learning Outcome


After completing this experiment the students will be able to:

1. Define Precision of numerical differentiation and numerical integration for a System.


2. Define numerical differentiation based on Lagrange Interpolation formula.
3. Define numerical integration based on Simple Trapezium Rule formula.

8.3 Theory
8.3.1 Lagrange Polynomial

The Lagrange interpolating polynomial is the polynomial P( x) of degree (n − 1) that passes


through the n points , , ..., , and is given by

Where,

Page 38 of 60
When constructing interpolating polynomials, there is a tradeoff between having a better fit
and having a smooth well-behaved fitting function. The more data points that are used in the
interpolation, the higher the degree of the resulting polynomial, and therefore the greater
oscillation it will exhibit between the data points. Therefore, a high-degree interpolation may
be a poor predictor of the function between points, although the accuracy at the data points
will be "perfect."

For points,

Note that the function P( x) passes through the points , as can be seen for the case ,

8.3.2 Simple Trapezium Rule

We know that higher order differences are negligible for small ℎ in the case of well behaved
functions.

𝑥0 +𝑘
1 ℎ
∫ 𝑦𝑑𝑥 = ℎ (𝑓0 + ∆𝑓0 ) = (𝑓1 + 𝑓0 )
2 2
𝑥0

The above formula is called the Trapezium Rule.

8.4 Procedure

Find 𝒇′ (𝟎. 𝟏𝟐) from the following data using Lagrange Polynomial Interpolation:

Page 39 of 60
x 0.05 0.10 0.20 0.26

𝑓(𝑥) 0.05000 0.0999 0.1987 0.2571


Solution:

Using Lagrange Polynomial Interpolation we get,

(𝑥 − 𝑥2 )(𝑥 − 𝑥3 )(𝑥 − 𝑥4 ) (𝑥 − 𝑥1 )(𝑥 − 𝑥3 )(𝑥 − 𝑥4 )


𝑓(𝑥) = 𝑦1 + 𝑦
(𝑥1 − 𝑥2 )(𝑥1 − 𝑥3 )(𝑥1 − 𝑥4 ) (𝑥2 − 𝑥1 )(𝑥2 − 𝑥3 )(𝑥2 − 𝑥4 ) 2
(𝑥 − 𝑥1 )(𝑥 − 𝑥2 )(𝑥 − 𝑥4 ) (𝑥 − 𝑥1 )(𝑥 − 𝑥2 )(𝑥 − 𝑥3 )
+ 𝑦3 + 𝑦
(𝑥3 − 𝑥1 )(𝑥3 − 𝑥2 )(𝑥3 − 𝑥4 ) (𝑥4 − 𝑥1 )(𝑥4 − 𝑥2 )(𝑥4 − 𝑥3 ) 4

𝑓(𝑥) = −0.119𝑥 3 − 0.025𝑥 2 + 1.004𝑥

Differentiating 𝑓(𝑥) with respect to 𝑥 we have

𝑓 ′ (𝑥) = −0.357𝑥 2 − 0.05𝑥 + 1.004

When 𝑥 = 0.12, we get

𝑓 ′ (0.12) = 0.9927

Steps for the Lagrange Polynomial:

For a given x and f(x), two sets of (N+1) data pairs, (xi , fi), i= 0, 1, . ….. N:

Set SUM=0

N=length of x

DO FOR i=1 to N

Set Y=1

DO FOR j=1 to N

IF j~=i

Set Y=Y*(x-x(j))/(x(i)-x(j))

End DO(j)

Y=Y*f

Page 40 of 60
SUM=SUM+Y

𝟎.𝟖 𝟐
a) Evaluate Numerically ∫𝟎 (𝒆−𝒙 )𝒅𝒙 using Simple Trapezium Rule

Table 2
2
X 𝑓 = 𝑒 −𝑥
0 1.0000
0.1 0.9900
0.2 0.9608
0.3 0.9139
0.4 0.8521
0.5 0.7788
0.6 0.6977
0.7 0.6126
0.8 0.5273

From Simple Trapezium Rule:

We know that higher order differences are negligible for small ℎ in the case of well-behaved
functions.
𝑥0 +𝑘
1
∫ 𝑦𝑑𝑥 = ℎ (𝑓0 + ∆𝑓0 )
2
𝑥0


= (𝑓 + 𝑓0 )
2 1

Here, ℎ = 𝑥𝑛 − 𝑥0 = 0.8 − 0 = 0.8, for one strip


𝑥 −𝑥 0.8−0
ℎ = 𝑛 2 0 = 2 = 0.4, for two strip
𝑥𝑛 −𝑥0 0.8−0
ℎ= = = 0.2, for four strip
4 4
𝑥𝑛 −𝑥0 0.8−0
ℎ= = = 0.1, for eight strip
8 8

For one strip (i.e. h=0.8),

ℎ 0.8
𝐼11 = (𝑓0 + 𝑓1 ) = (1 + 0.5273) = 0.6109
2 2
For two strip (i.e. h=0.4),

ℎ 0.4
𝐼21 = (𝑓0 + 2𝑓1 + 𝑓2 ) = (1 + 2 ∗ 0.8521 + 0.5273) = 0.6463
2 2

Page 41 of 60
For four strips (i.e. h=0.2),


𝐼31 = (𝑓 + 2𝑓1 + 2𝑓2 + 2𝑓3 + 𝑓4 )
2 0
0.2
= (1 + 2 ∗ 0.9608 + 2 ∗ 0.8521 + 2 ∗ 0.6977 + 0.5273) = 0.6549
2

For eight strips (i.e. h=0.1),


𝐼41 = (𝑓 + 2𝑓1 + 2𝑓2 + 2𝑓3 + 2𝑓4 + 2𝑓5 + 2𝑓6 + 2𝑓7 + 𝑓8 )
2 0
0.1
= (1 + 2 ∗ 0.9900 + 2 ∗ 0.9608 + 2 ∗ 0.9139 + 2 ∗ 0.8521 + 2 ∗ 0.7788
2
+ 2 ∗ 0.6977 + 2 ∗ 0.6126 + 0.5273) = 0.6570

8.4.1 Steps

1. At first evaluate initial approximation matrices 𝑥𝑖 & 𝑓𝑖


2. Evaluate the matrices 𝑓0 𝑡𝑜 𝑓8 as required to solve the equation
3. Write MATLAB expression of the formula 𝐼11 , 𝐼21 , 𝐼31 & 𝐼41

8.5 Report Question


1. Write MATLAB Program of Numerical Differentiation for Lagrange Polynomial
interpolation formula for the following systems to find 𝑓 ′ (0.12)
I. 𝑖 = 1,2,3,4; 𝑥𝑖 = 𝑖 + 1; 𝑓𝑖 = (𝑥𝑖 2 + 1) Ans. 0.24
II. 𝑖 = 2,4,8,12; 𝑥𝑖 = 𝑖/2; 𝑓𝑖 = (𝑥𝑖 2 + |𝑥𝑖 |) Ans. 1.24
2. Write MATLAB Program of Numerical Integration using Simple Trapezium Rule for
𝝅/𝟒
∫𝒐 𝒄𝒐𝒔𝒙𝒅𝒙 up to eight strips. Ans. 0.6704, 0.6980, 0.7048, 0.7065

Page 42 of 60
Experiment 9
Numerical Integration by Romberg Integration and Simpson's
1/3rd, 3/8th Rule

9.1 Objectives
1. Familiar with the practical implementation of numerical integration for a System.
2. Familiar with the practical implementation of numerical integration based on
Romberg Integration for a System.
3. Familiar with the practical implementation of numerical integration based on
Simpson's 1/3rd rule for a System.
4. Familiar with the practical implementation of numerical integration based on
Simpson's 3/8th rule for a System.
5. Explain a system through difference features in digital domain.

9.2 Learning Outcome


After completing this experiment the students will be able to:

1. Define Precision of numerical integration for a System.


2. Define numerical integration based on Romberg Integration formula.
3. Define numerical integration based on Simpson's 1/3rd rule formula.
4. Define numerical integration based on Simpson's 3/8th rule formula.

9.3 Theory
There are two cases in which engineers and scientists may require the help of numerical
integration technique. (1) Where experimental data is obtained whose integral may be
required and (2) where a closed form formula for integrating a function using calculus is
difficult or so complicated as to be almost useless. For example, the integral

x t3
(t ) =  dt.
0 et − 1

Page 43 of 60
Since there is no analytic expression for ( x) , numerical integration technique must be used to
obtain approximate values of ( x) .

Formulae for numerical integration called quadrature are based on fitting a polynomial
through a specified set of points (experimental data or function values of the complicated
function) and integrating (finding the area under the fitted polynomial) this approximating
function. Any one of the interpolation polynomials studied earlier may be used.

9.3.1 Romberg Integration


In the case of Trapezoidal rule:
4𝑛−1 𝐼𝑗(𝑛−1) − 𝐼(𝑗−1)(𝑛−1)
𝐼𝑗𝑛 = ; 𝑗 = 𝑛, 𝑛 + 1, …
4𝑛−1 − 1
In the case of Simpson’s rule:

4𝑛 𝐼𝑗(𝑛−1) − 𝐼(𝑗−1)(𝑛−1)
𝐼𝑗𝑛 = ; 𝑗 = 𝑛, 𝑛 + 1, …
4𝑛 − 1

9.3.2 Simpson’s 1/3 Rule

This is based on approximating the function f(x) by fitting quadratics through sets of three
points. For only three points it can be written as:

x1 + 2 h
h

x1
f ( x)dx = ( f1 + 4 f 2 + f3 )
3

This is called second-degree Newton-Cotes formula.

It is evident that the result of integration between x1 and x1+nh can be written as

x1 + nh
h
 f ( x)dx = 
i =1,3,5,..., n −1 3
( fi + 4 fi +1 + fi + 2 )
x1

h
= ( f1 + 4 f 2 + 2 f3 + 4 f 4 + 2 f 5 + 4 f 6 + ... 4 f n + f n +1 )
3

In using the above formula it is implied that f is known at an odd number of points (n+1 is
odd, where n is the no. of subintervals).

Page 44 of 60
9.3.3 Simpson’s 3/8 Rule

This is based on approximating the function f(x) by fitting cubic interpolating polynomial
through sets of four points. For only four points it can be written as:

x1 +3 h
3h

x1
f ( x)dx =
8
( f1 + 3 f 2 + 3 f3 + f 4 )

This is called third-degree Newton-Cotes formula.

It is evident that the result of integration between x1 and x1+nh can be written as

x1 + nh
h
 f ( x)dx = 
i =1,4,7,..., n − 2 3
( f i + 3 f i +1 + 3 f i + 2 + f i +3 )
x1

3h
= ( f1 + 3 f 2 + 3 f3 + 2 f 4 + 3 f 5 + 3 f 6 + 2 f 7 + ... + 2 f n −2 + 3 f n −1 + 3 f n + f n +1 )
8

In using the above formula it is implied that f is known at (n+1) points where n is divisible by 3.

9.4 Procedure
9.4.1 Romberg Integration
0.8 2
Using the values given in the following Table 2, find ∫0 (𝑒 −𝑥 )𝑑𝑥 by Romberg’s integration.

Solution: Table 1
2
X 𝑓 = 𝑒 −𝑥
0 1
0.1 0.9901
0.2 0.9608
0.3 0.9139
0.4 0.8521
0.5 0.7788
0.6 0.6977
0.7 0.6126
0.8 0.5273

From Simple Trapezium Rule, we know that higher order differences are negligible for small ℎ in
the case of well behaved functions.

Page 45 of 60
𝑥0 +𝑘
1
∫ 𝑦𝑑𝑥 = ℎ (𝑓0 + ∆𝑓0 )
2
𝑥0


(𝑓 + 𝑓0 )
=
2 1
Here, ℎ = 𝑥𝑛 − 𝑥0 = 0.8 − 0 = 0.8, for one strip
𝑥 −𝑥 0.8−0
ℎ = 𝑛 2 0 = 2 = 0.4, for two strip
𝑥𝑛 −𝑥0 0.8−0
ℎ= = = 0.2, for three strip
4 4
𝑥𝑛 −𝑥0 0.8−0
ℎ= = = 0.1, for four strip
8 8

For one strip (i.e. h=0.8),


ℎ 0.8
𝐼11 = (𝑓0 + 𝑓1 ) = (1 + 0.5273) = 0.6109
2 2
For two strip (i.e. h=0.4),

ℎ 0.4
𝐼21 = (𝑓0 + 2𝑓1 + 𝑓2 ) = (1 + 2 ∗ 0.8521 + 0.5273) = 0.6463
2 2

For four strips (i.e. h=0.2),



𝐼31 = (𝑓0 + 2𝑓1 + 2𝑓2 + 2𝑓3 + 𝑓4 )
2
0.2
= (1 + 2 ∗ 0.9608 + 2 ∗ 0.8521 + 2 ∗ 0.6977 + 0.5273) = 0.6549
2

For eight strips (i.e. h=0.1),


𝐼41 = (𝑓 + 2𝑓1 + 2𝑓2 + 2𝑓3 + 2𝑓4 + 2𝑓5 + 2𝑓6 + 2𝑓7 + 𝑓8 )
2 0
0.1
= (1 + 2 ∗ 0.9900 + 2 ∗ 0.9608 + 2 ∗ 0.9139 + 2 ∗ 0.8521 + 2 ∗ 0.7788
2
+ 2 ∗ 0.6977 + 2 ∗ 0.6126 + 0.5273) = 0.6570

Table 2

h 𝐼𝑗1 (𝑗 = 1,2,3,4)
0.8 𝐼11 = 0.6109
0.4 𝐼21 = 0.6463
0.2 𝐼31 = 0.6549
0.1 𝐼41 = 0.6570

Now From Romberg’s Formula:

Page 46 of 60
4𝑛−1 𝐼𝑗(𝑛−1) − 𝐼(𝑗−1)(𝑛−1)
𝐼𝑗𝑛 = ; 𝑗 = 𝑛, 𝑛 + 1, …
4𝑛−1 − 1
4𝐼21 − 𝐼11 4𝐼31 − 𝐼21 4𝐼41 − 𝐼31
𝐼22 = , 𝐼32 = , 𝐼42 =
3 3 3

42 𝐼32 − 𝐼22 42 𝐼42 − 𝐼32


𝐼33 = , 𝐼43 =
15 15

43 𝐼43 − 𝐼33
𝐼44 =
63

ℎ2 ℎ4 ℎ6 ℎ8
𝐼11
𝐼21 𝐼22
𝐼31 𝐼32 𝐼33
𝐼41 𝐼42 𝐼43 𝐼44

Ans.: I22 = 0.6581, I33 = 0.6577, I44 = 0.6577

9.4.2 Steps

1. At first evaluate initial approximation matrices 𝑥𝑖 & 𝑓𝑖


2. Evaluate corresponding ℎ and matrices 𝑓0 𝑡𝑜 𝑓8 as required to solve the equation
3. Write MATLAB expression of the formula 𝐼11 , 𝐼21 , 𝐼31 & 𝐼41
4. Determine 𝐼22 , 𝐼32 , 𝐼42 , 𝐼33 , 𝐼43 , 𝐼44 using MATLAB.

9.4.3 Simpson’s 1/3 Rule

7 1
Evaluate ∫1 (𝑥) 𝑑𝑥 by using Simpson’s composite 1/3 rule.

Solutiuon

Compare the values with the exact value of the integral

Table 1

X 1
𝑓(𝑥) =
𝑥
1 1

Page 47 of 60
2 0.5
3 0.33
4 0.25
5 0.2
6 0.17
7 0.14

𝑥𝑛 − 𝑥0
ℎ=
𝑛−1
7
1 ℎ
∫ ( ) 𝑑𝑥 = (𝑓1 + 4𝑓2 + 2𝑓3 + 4𝑓4 + 2𝑓5 + 4𝑓6 + 𝑓7 )
𝑥 3
1

Ans.: (a) 1.9587

9.4.4 Simpson’s 3/8 Rule

7 1
Evaluate ∫1 (𝑥) 𝑑𝑥 by using Simpson’s composite 3/8 rule.

Solutiuon: Compare the values with the exact value of the integral

Table 2

X 1
𝑓(𝑥) =
𝑥
1 1
2 0.5
3 0.33
4 0.25
5 0.2
6 0.17
7 0.14

𝑥𝑛 − 𝑥0
ℎ=
𝑛−1
7
1 3ℎ
∫ ( ) 𝑑𝑥 = (𝑓 + 3𝑓2 + 3𝑓3 + 2𝑓4 + 3𝑓5 + 3𝑓6 + 𝑓7 )
𝑥 8 1
1

Ans.: 1.9661

9.4.5 Steps:
An algorithm for integrating a tabulated function using composite trakpezoidal rule:

Page 48 of 60
Remarks: f1, f2,………, fn+1 are the tabulated values at x1, x1+h,………x1+nh (n+1 points)
1 Read 𝑥1→𝑛+1
2 n=length(x)
𝑥 −𝑥 𝑥𝑛 −𝑥0
3 ℎ = 𝑛+1𝑛 0 𝑂𝑅 𝑛−1
4 Read 𝑓(𝑥) = 𝑦
5 𝑓𝑜𝑟 𝑖 = 2 𝑡𝑜 𝑛 + 1 𝑅𝑒𝑎𝑑 𝑓𝑖 𝑒𝑛𝑑 𝑓𝑜𝑟
6 𝑠𝑢𝑚1 ← (𝑓𝑖 + 𝑓𝑛+1 )
7 𝑓𝑜𝑟 𝑖 = 3 𝑡𝑜 𝑛 − 1 𝑅𝑒𝑎𝑑 𝑓𝑖 𝑒𝑛𝑑 𝑓𝑜𝑟
8 𝑠𝑢𝑚2 ← (𝑓𝑖 + 𝑓𝑛−1 )
9 𝑠𝑢𝑚 ← (𝑠𝑢𝑚1 + 𝑠𝑢𝑚2)

10 𝑖𝑛𝑡𝑒𝑔𝑟𝑎𝑙 ← 3 . 𝑠𝑢𝑚

9.5 Report Question


1. Write MATLAB Program of Numerical Integration using Romberg Integration for
𝝅/𝟒
∫𝒐 𝒄𝒐𝒔𝒙𝒅𝒙 up to eight strips. Ans. I22=0.7072, I33=0.7071, I44=0.7071

2. Write MATLAB Program of Numerical Differentiation for Simpson’s composite 1/3 &
3/8 rules for the following systems:
a) 𝑖 = 1,2,3,4; 𝑥𝑖 = 𝑖 + 1; 𝑓𝑖 = (𝑥𝑖 2 + 1) Ans. 23.6667, 11.6250
b) 𝑖 = 2,4,8,12; 𝑥𝑖 = 𝑖/2; 𝑓𝑖 = (𝑥𝑖 2 + |𝑥𝑖 |) Ans. 37.7778, 27.5000

Page 49 of 60
Experiment 10
Solution of system of Linear Equations By a direct method (LU
Decomposition) and an indirect method (Jacobi or Gauss Seidel
Method)

10.1 Objectives
1. Familiar with the practical implementation of the different techniques of finding
solution of a set of n linear algebraic equations in n unknowns.
2. Familiar with the practical implementation of LU Decomposition for system solution
of linear equations.
3. Familiar with the practical implementation of Jacobi or Gauss Seidel method for
system solution of linear equations.
4. Explain a system through difference features in digital domain.

10.2 Learning Outcome


After completing this experiment the students will be able to:

1. Define Precision of system solution for linear equations.


2. Define LU Decomposition for system solution of linear equations.
3. Define Jacobi or Gauss Seidel method for system solution of linear equations.

10.3 Theory

Concept of linear equations and their solution


A set of linear algebraic equations looks like this:

a11 x1 + a12 x2 + ...a1N xN = b1

a21 x1 + a22 x2 + ...a2 N xN = b2 (1)

… … … …

aM 1 x1 + aM 2 x2 + ...aMN xN = bM

Page 50 of 60
Here the N unknowns xj , j = 1, 2, . . .,N are related by M equations. The coefficients aij with i = 1,
2, . . .,M and j = 1, 2, . . .,N are known numbers, as are the right-hand side quantities bi, i = 1, 2, .
. .,M.

Existence of solution

If N = M then there are as many equations as unknowns, and there is a good chance of solving
for a unique solution set of xj’s. Analytically, there can fail to be a unique solution if one or
more of the M equations is a linear combination of the others (This condition is called row
degeneracy), or if all equations contain certain variables only in exactly the same linear
combination(This is called column degeneracy). (For square matrices, a row degeneracy
implies a column degeneracy, and vice versa.) A set of equations that is degenerate is called
singular.

Numerically, at least two additional things can go wrong:

• While not exact linear combinations of each other, some of the equations may be so close
to linearly dependent that round off errors in the machine renders them linearly dependent
at some stage in the solution process. In this case your numerical procedure will fail, and it
can tell you that it has failed.

• Accumulated round off errors in the solution process can swamp the true solution. This
problem particularly emerges if N is too large. The numerical procedure does not fail
algorithmically. However, it returns a set of x’s that are wrong, as can be discovered by direct
substitution back into the original equations. The closer a set of equations is to being singular,
the more likely this is to happen.

10.3.1 Matrices

Equation (1) can be written in matrix form as

A·x=b (2)

Here the raised dot denotes matrix multiplication, A is the matrix of coefficients, x is the
column vector of unknowns and b is the right-hand side written as a column vector,

Page 51 of 60
 a11 a12 ... a1N   x1   b1 
a a22 ... a2 N  x  b 
A =  21 x= 2 b= 2
 ... ... ... ...   ..   .. 
     
 aM 1 aM 2 ... aMN   xN  bM 

10.3.2 Finding Solution


There are so many ways to solve this set of equations. Below are some important methods.

10.3.2.1 Using the backslash and pseudo-inverse operator


In MATLAB, the easiest way to determine whether Ax = b has a solution, and to find such a
solution when it does, is to use the backslash operator. Exactly what A \ b returns is a bit
complicated to describe, but if there is a solution to A · x = b, then A \ b returns one.
Warnings: (1) A \ b returns a result in many cases when there is no solution to A · x = b. (2)
A \ b sometimes causes a warning to be issued, even when it returns a solution. This means
that you can't just use the backslash operator: you have to check that what it returns is a
solution. (In any case, it's just good common sense to check numerical computations as you
do them.) In MATLAB this can be done as follows:

Using backslash operator:

x = A\b;

You can also use the pseudo-inverse operator:

x=inv(A)*b; % it is also guaranteed to solve Ax = b, if Ax = b has a solution.

As with the backslash operator, you have to check the result.

10.3.2.2 Using Gauss-Jordan Elimination and Pivoting


To illustrate the method let us consider three equations with three unknowns:

a11 x1 + a12 x2 + a13 x3 = a14 (A)

a21 x1 + a22 x2 + a23 x3 = a24 (B)

a31 x1 + a32 x2 + a33 x3 = a34 (C)

Page 52 of 60
Here the quantities bi, i = 1, 2, . . .,M’s are replaced by aiN+1, where i=1,2, ….M for simplicity of
understanding the algorithm.

The First Step is to eliminate the first term from Equations (B) and (C). (Dividing (A) by a11 and
multiplying by a21 and subtracting from (B) eliminates x1 from (B) as shown below)

a11 a a a
(a21 − a21 ) x1 + (a22 − 12 a21 ) x2 + (a23 − 13 a21 ) x3 = (a24 − 14 a21 )
a11 a11 a11 a11

a21
Let, = k2 , then
a11

(a21 − k2 a11 ) x1 + (a22 − k2a12 ) x2 + (a23 − k2a13 ) x3 = (a24 − k2a14 )

a31
Similarly multiplying equation (A) by = k3 and subtracting from (C), we get
a11

(a31 − k3a11 ) x1 + (a32 − k3a12 ) x2 + (a33 − k3a13 ) x3 = (a34 − k3a14 )

Observe that (a21 − k2 a11 ) and (a31 − k3a11 ) are both zero.

In the steps above it is assumed that a11 is not zero. This case will be considered later in this
experiment.

The above elimination procedure is called triangularization.

10.4 Procedure
For triangularizing n equations in n unknowns

1 for i = 1 to n and j = 1 to (n + 1) in steps of 1 do read aij endfor

2 for k = 1 to (n −1) in steps of 1 do

3 for i = (k + 1) to n in steps of 1 do

4 u  aik / akk

5 for j = k to (n + 1) in steps of 1 do

Page 53 of 60
6 aij  aij − uakj endfor

endfor

endfor

The reduced equations are:

a11 x1 + a12 x2 + a13 x3 = a14

a22 x2 + a23 x3 = a24

a32 x2 + a33 x3 = a34

The next step is to eliminate a32 from the third equation. This is done by multiplying second

equation by u = a32 / a22 and subtracting the resulting equation from the third. So, same algorithm

can be used.

Finally the equations will take the form:

a11 x1 + a12 x2 + a13 x3 = a14

a22 x2 + a23 x3 = a24

a33 x3 = a34

The above set of equations are said to be in triangular (Upper) form.

From the above upper triangular form of equations, the values of unknowns can be obtained by
back substitution as follows:

x3 = a34 / a33

x2 = (a24 − a23 x3 ) / a22

x2 = (a14 − a12 x2 − a13 x3 ) / a11

Page 54 of 60
Algorithmically, the back substitution for n unknowns is shown below:

1 xn  an ( n +1) / ann

2 for i = (n −1) to 1 in step of -1 do

3 sum  0

4 for j = (i + 1) to n in steps of 1 do

5 sum  sum + aij x j endfor

6 xi  (ai ( n +1) − sum) / aii

endfor

10.5 Pivoting

In the triangularization algorithm we have used,

u  aik / akk

Here it is assumed that akk is not zero. If it happens to be zero or nearly zero, the algorithm will
lead to no results or meaningless results. If any of the akk is small it would be necessary to
reorder the equations. It is noted that the value of akk would be modified during the elimination
process and there is no way of predicting their values at the start of the procedure.

The elements akk are called pivot elements. In the elimination procedure the pivot should not be
zero or a small number. In fact for maximum precision the pivot element should be the largest in
absolute value of all the elements below it in its column, i.e. akk should be picked up as the
maximum of all amk where, m  k

So, during the Gauss elimination, amk elements should be searched and the equation with the
maximum value of amk should be interchanged with the current position. For example if during
elimination we have the following situation:

x1 + 2 x2 + 3x3 = 4
0.3x2 + 4 x3 = 5
−8 x2 + 3x3 = 6

Page 55 of 60
As −8  0.3, 2nd and 3rd equations should be interchanged to yield:

x1 + 2 x2 + 3x3 = 4
−8 x2 + 3x3 = 6
0.3x2 + 4 x3 = 5

It should be noted that interchange of equations does not affect the solution.

The algorithm for picking the largest element as the pivot and interchanging the equations is
called pivotal condensation.

10.5.1 Procedure

For pivotal condensation

1 max  akk

2 pk

3 for m = (k + 1) to n in steps of 1 do

4 if ( amk  max) then

5 max  amk

6 pm

7 endif

endfor

8 if ( p ~= k )

9 for q = k to (n + 1) in steps of 1 do

10 temp  akq

11 akq  a pq

Page 56 of 60
12 a pq  temp

endfor

endif

(3) Using Gauss-Seidel Iterative Method

There are several iterative methods for the solution of linear systems. One of the efficient
iterative methods is the Gauss-Seidel method.

Let us consider the system of equations:

4 x1 − x2 + x3 = 7
4 x1 − 8 x2 + x3 = −21
−2 x1 + x2 + 5 x3 = 15

The Gauss-Seidel iterative process is suggested by the following equations:

7 + x2k − x3k
x1k +1 =
4
21 + 4 x1k +1 + x3k
x2k +1 =
8
15 + 2 x1k +1 − x2k +1
x3k +1 =
5

The very first iteration, that is x20 , x30 ,.....xn0 (for n equations) are set equal to zero and x11 is

calculated. The main point of Gauss-Seidel iterative process to observe is that always the
latest approximations for the values of variables are used in an iteration step.

It is to be noted that in some cases the iteration diverges rather than it converges. Both the
divergence and convergence can occur even with the same set of equations but with the
change in the order. The sufficient condition for the Gauss-Seidel iteration to converge is
stated below.

The Gauss-Seidel iteration for the solution will converge (if there is any solution) if the matrix
A (as defined previously) is strictly diagonally dominant matrix.

A matrix A of dimension N  N is said to be strictly diagonally dominant provided that

Page 57 of 60
N
akk   akj for k = 1, 2,...N
j =1
j k

10.6 Report Question


1. Given the simultaneous equations shown below (i) triangularize them (ii) use back
substitution to solve for x1 , x2 , x3 .

2 x1 + 3x2 + 5 x3 = 23
3x1 + 4 x2 + x3 = 14
6 x1 + 7 x2 + 2 x3 = 26

For generalization, you will have to write a program for triangularizing n equations in n
unknowns with back substitution.

2. Modify the MATLAB program written in exercise 1 to include pivotal condensation.


3. Try to solve the following systems of equations (i) Gauss-Jordan elimination (ii)
Gauss-Jordan elimination with pivoting

2 x1 + 4 x2 − 6 x3 = −4 x1 + x2 + 6 x3 = 7
(A) x1 + 5 x2 + 3x3 = 10 (B) − x1 + 2 x2 + 9 x3 = 2
x1 + 3x2 + 2 x3 = 5 x1 − 2 x2 + 3x3 = 10

4 x1 + 8 x2 + 4 x3 = 8
x1 + 5 x2 + 4 x3 − 3x4 = −4
(C)
x1 + 4 x2 + 7 x3 + 2 x4 = 10
x1 + 3x2 − 2 x4 = −4

4. Solve the following equations using Gauss-Seidel iteration process:

8 x1 − 3x2 = 10 4 x − y = 15
(A) (B)
− x1 + 4 x2 = 6 x + 5y = 9

Page 58 of 60
5 x1 − x2 + x3 = 10 2 x + 8 y − z = 11
(C) 2 x1 + 8 x2 − x3 = 11 (D) 5 x − y + z = 10
− x1 + x2 + 4 x3 = 3 −x + y + 4z = 3

Page 59 of 60
Updated Lab Manual
Numerical Method
Lab-in-Charge: Nahian Rabbi Ushan
Updated Date: 23 Aug 2023

Page 60 of 60

You might also like