Practical C5 v6
Practical C5 v6
PRACTICAL FILE
CONTENTS
➢ Bisection Method
➢ Newton Raphson Method
➢ Secant Method
➢ Gauss-Jacobi Method
➢ Gauss-Seidal Method
➢ Lagrange Interpolation
➢ Simpson’s 1/3rd Rule
➢ Simpson’s 3/8th Rule
➢ Some Basic Programming Practicals
o Absolute value of an Integer
o Sum of N numbers
o Sum of Harmonic Series
o Sorting using Bubble Sort
2|Page
Numerical Methods Practicals
BISECTION METHOD
The Bisection Method is a simple and reliable numerical technique used to find the root
of a continuous function. It is also known as the Binary Chopping or Half-Interval
Method. This method is based on the repeated application of the Intermediate Value
Theorem, which ensures that if a function f(x) changes sign over an interval [a, b], then
there exists at least one root in that interval.
Mathematical Formulation:
➢ Choose initial interval [a, b] such that f(a)⋅ f(b)<0.
➢ Compute the midpoint: c=(a+b)/2
➢ Check the sign of f(c):
o If f(a)⋅ f(c)<0, then the root lies in [a, c], so set b=c.
o Else if f(c)⋅ f(b)<0, then the root lies in [c, b], so set a=c.
➢ Repeat until the |b−a∣<tolerance or ∣f(c)∣ is less than a desired error.
MATLAB Implementation and Output:
Use the Bisection Method to find a root of the equation 𝑓(𝑥) = 𝑥 3 − 4𝑥 − 9 in the
interval [2,3], correct to four decimal places. Use a tolerance of 10-4.
Code:
3|Page
Numerical Methods Practicals
NEWTON-RAPHSON METHOD
The Newton-Raphson Method is a fast and commonly used numerical method for
finding the root of a real-valued function. It is based on the idea of using tangents and
requires both the function f(x) and its derivative f′(x). The method starts with an initial
guess and iteratively improves it using a simple formula. However, the method fails if
f′(x)=0 during any iteration, as the next approximation becomes undefined.
Mathematical Formulation:
➢ Choose an initial guess x0
𝑓(𝑥 )
➢ Compute successive approximations using: 𝑥𝑛+1 = 𝑥𝑛 − 𝑓′ (𝑥𝑛 )
𝑛
➢ Repeat until |xn+1-xn |<tolerance or ∣f(xn+1)∣ is sufficiently small.
MATLAB Implementation and Output:
Use the Newton-Raphson Method to find a root of the equation 𝑓(𝑥) = 2𝑥 − 5𝑥 + 2
starting from an initial guess x0=0 with a tolerance of 10-4.
Code:
4|Page
Numerical Methods Practicals
Try finding a root of the 𝑓(𝑥) = cos(𝑥) using the Newton-Raphson Method, starting
with an initial guess 𝑥0 = 0 and using a tolerance of 10-4.
Code:
5|Page
Numerical Methods Practicals
SECANT METHOD
The Secant Method is a root-finding algorithm that uses a succession of secant lines to
approximate the root of a real-valued function. It starts with two initial approximations
and uses the slope of the line connecting these points (the secant) to estimate the next
value closer to the root. Unlike the Newton-Raphson method, it does not require the
analytical derivative of the function, making it useful when the derivative is difficult or
unavailable.
Mathematical Formulation:
➢ Choose an initial guess x0 and x1
𝑥 −𝑥𝑛−1
➢ Compute successive approximations using: 𝑥𝑛+1 = 𝑥𝑛 − 𝑓(𝑥𝑛 ). 𝑓(𝑥 𝑛)−𝑓(𝑥
𝑛 𝑛−1 )
➢ Repeat until |xn+1-xn |<tolerance.
Use the Secant Method to find a root of the equation 𝑓(𝑥) = 𝑐𝑜𝑠 𝑥 − 𝑥𝑒 𝑥 starting from
an initial guess 𝑥0 = 0 𝑎𝑛𝑑 𝑥1 = 1 with a tolerance of 10-4.
MATLAB Implementation and Output:
Code:
6|Page
Numerical Methods Practicals
GAUSS-JACOBI METHOD
The Jacobi Method is an iterative algorithm used to solve a system of linear equations of
the form Ax=b. It is particularly effective when the coefficient matrix A is diagonally
dominant. The method works by isolating each variable in every equation and using
values from the previous iteration to update the solution vector in the next step. The
update rule for the ith variable is:
1 𝑛
(𝑘+1) (𝑘)
𝑥𝑖 = (𝑏𝑖 − ∑𝑗=1 𝑎𝑖𝑗 𝑥𝑗 )
𝑎𝑖𝑖
𝑗≠𝑖
The iteration continues until the approximate solution converges within a specified
tolerance level.
MATLAB Implementation-Method 1 (Element-wise Jacobi):
This version of the Jacobi method updates each variable separately in a loop and uses
only the values from the previous iteration during the current update. It reflects the
traditional element-wise mathematical formulation of the Jacobi method.
Solve the following system of linear equations using the Jacobi Method with a
tolerance of 10-5
5𝑥 − 2𝑦 + 3𝑧 = −1; −3𝑥 + 9𝑦 + 𝑧 = 2; 2𝑥 − 𝑦 − 7𝑧 = 3
Code:
7|Page
Numerical Methods Practicals
8|Page
Numerical Methods Practicals
GAUSS-SEIDAL METHOD
The Gauss-Seidel Method is also an iterative technique for solving linear systems Ax=b,
similar to the Jacobi method. However, in Gauss-Seidel, the most recently updated
values of the solution vector are used immediately during the iteration, making the
convergence faster in many cases. Its update formula for the ith variable is:
1 𝑖−1 𝑛
(𝑘+1) (𝑘+1) (𝑘)
𝑥𝑖 = (𝑏𝑖 − ∑ 𝑎𝑖𝑗 𝑥𝑗 −∑ 𝑎𝑖𝑗 𝑥𝑗 )
𝑎𝑖𝑖 𝑗=1 𝑗=𝑖+1
9|Page
Numerical Methods Practicals
10 | P a g e
Numerical Methods Practicals
LAGRANGE INTERPOLATION
Lagrange Interpolation is a numerical method used to estimate the value of a function
given a set of known data points. It is particularly useful when the function itself is
unknown but its values at specific points are known.
The method constructs a polynomial that passes exactly through all the given data
points. Suppose we are given (n+1) data points (𝑥0 , 𝑦0 ), (𝑥1 , 𝑦1 ), … … … , (𝑥𝑛 , 𝑦𝑛 )the
Lagrange interpolation polynomial P(x) is defined as:
𝑛
𝑃(𝑥) = ∑ 𝑦𝑖 . 𝐿𝑖 (𝑥)
𝑖=0
MATLAB Implementation:
Using Lagrange interpolation, estimate the value log(x) at x=9.2 using the following
known data points:
x 9.0 9.5 10.0 11.0
y 2.197 2.251 2.302 2.397
Code:
11 | P a g e
Numerical Methods Practicals
𝑏 𝑛−1 𝑛−2
ℎ
∫ 𝑓(𝑥)𝑑𝑥 ≈ [𝑓(𝑥0 ) + 4 ∑ 𝑓(𝑥𝑖 ) + 2 ∑ 𝑓(𝑥𝑖 ) + 𝑓(𝑥𝑛 )]
𝑎 3
𝑖=1 𝑖=2
𝑜𝑑𝑑 𝑖 𝑒𝑣𝑒𝑛 𝑖
where 𝑥0 = 𝑎, 𝑥1 = 𝑎 + ℎ, … … ., 𝑥𝑛 = 𝑏
MATLAB Implementation:
Use Simpson’s 1/3 Rule to approximate the value of the integral .
2
∫ (cos 𝑥 − ln(𝑥) + 𝑒 𝑥 ) 𝑑𝑥
1
Divide the interval into 32 equal subintervals and calculate the approximate value of the
integral.
Code:
12 | P a g e
Numerical Methods Practicals
13 | P a g e
Numerical Methods Practicals
SUM OF N NUMBERS:
This program calculates the sum of the first n natural numbers, starting from 1. It
demonstrates a basic use of loops to perform cumulative addition.
Code (Using for loop):
14 | P a g e
Numerical Methods Practicals
15 | P a g e
Numerical Methods Practicals
16 | P a g e