0% found this document useful (0 votes)
0 views16 pages

Practical C5 v6

The document is a practical file for a Numerical Methods course, detailing various numerical techniques such as the Bisection Method, Newton-Raphson Method, Secant Method, and others. It includes mathematical formulations, MATLAB implementations, and examples for each method, along with basic programming practices. The content is structured to assist students in understanding and applying numerical methods effectively.

Uploaded by

noterspoint12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views16 pages

Practical C5 v6

The document is a practical file for a Numerical Methods course, detailing various numerical techniques such as the Bisection Method, Newton-Raphson Method, Secant Method, and others. It includes mathematical formulations, MATLAB implementations, and examples for each method, along with basic programming practices. The content is structured to assist students in understanding and applying numerical methods effectively.

Uploaded by

noterspoint12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

NUMERICAL METHODS

PRACTICAL FILE

Exam Roll Number : 13420029


Class : B.Sc. 4th Sem
Registration Number : S2338335
Subject : Major (Practical)
Paper : Numerical Methods
Paper Code MTHC4A
Session : 2024-25
Numerical Methods Practicals

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:

Input and Result:

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:

Input and Result:

4|Page
Numerical Methods Practicals

Use the Newton-Raphson Method to find a root of the equation 𝑓(𝑥) = 𝑥 3 − 𝑥 − 1


starting from an initial guess x0=1.5 with a tolerance of 10-6.
Code:

Input and Result:

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:

Input and Result:

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:

Input and Result:

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:

Input and Result:

7|Page
Numerical Methods Practicals

MATLAB Implementation-Method 2 (Matrix Form with Diagonal check):


This second version uses a matrix-based implementation. Instead of updating each
variable separately, it forms the diagonal matrix D and computes updates in a vectorized
form using:
𝑥 (𝑘+1) = 𝑥 (𝑘) + 𝐷−1 (𝑏 − 𝐴𝑥 (𝑘) )
Before proceeding, it checks whether matrix A is square and diagonally dominant,
which are important for convergence.
Solve the following system of equations using the Jacobi Method with a tolerance of
10-5.
27𝑥 + 6𝑦 − 𝑧 = 85; 6𝑥 + 15𝑦 + 2𝑧 = 72; 𝑥 + 𝑦 + 54𝑧 = 110;
Code:

Input and Result:

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

MATLAB Implementation (User-Interactive)


Solve the system of equations using the Gauss-Seidel method with the following data.
27𝑥 + 6𝑦 − 𝑧 = 85; 6𝑥 + 15𝑦 + 2𝑧 = 72; 𝑥 + 𝑦 + 54𝑧 = 110;
Use an initial guess and tolerance as per your input.
Code:

Input and Result:

9|Page
Numerical Methods Practicals

MATLAB Implementation (Pre-Defined)


Solve the system of equations using the Gauss-Seidel method with the following data.
10𝑥 + 3𝑦 − 𝑧 = 19; 3𝑥 + 10𝑦 + 2𝑧 = 29; 𝑥 + 2𝑦 + 10𝑧 = 31;
Use zero initial guess and a tolerance of 10-5.
Code:

Input and Result:

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

Where 𝐿𝑖 (𝑥) is the Lagrange basis polynomial given by:


𝑛
(𝑥 − 𝑥𝑗 )
𝐿𝑖 (𝑥) = ∏
(𝑥𝑖 − 𝑥𝑗 )
𝑗=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:

Input and Result:

11 | P a g e
Numerical Methods Practicals

SIMPSON’S 1/3RD RULE


Simpson’s 1/3 Rule is based on approximating the integrand by a second-degree
(quadratic) polynomial. The interval [a,b] is divided into an even number n of
𝑏−𝑎
subintervals of equal width ℎ = 𝑛 ,The general formula is:

𝑏 𝑛−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:

Input and Result:

12 | P a g e
Numerical Methods Practicals

SIMPSON’S 3/8TH RULE


Simpson’s 3/8 Rule is another method that approximates the function using a cubic
polynomial, Simpson’s 3/8 Rule applies when the number of subintervals n is a multiple
𝑏−𝑎
of 3. The interval [a,b] is again divided into n equal parts of width ℎ = 𝑛 .The general
form of the Simpson’s 3/8 Rule is:
𝑏 𝑛−1 𝑛−3
3ℎ
∫ 𝑓(𝑥)𝑑𝑥 ≈ 𝑓(𝑥0 ) + 3 ∑ 𝑓(𝑥𝑖 ) + 2 ∑ 𝑓(𝑥𝑖 ) + 𝑓(𝑥𝑛 )
𝑎 8
𝑖=1 𝑖=3
[ 𝑖≢0(𝑚𝑜𝑑 3) 𝑖≡0(𝑚𝑜𝑑 3) ]
where 𝑥0 = 𝑎, 𝑥1 = 𝑎 + ℎ, … … ., 𝑥𝑛 = 𝑏
MATLAB Implementation:
Use Simpson’s 3/8 Rule to approximate the value of the integral.
1
1

0 1 + 𝑥2
Divide the interval into 30 equal subintervals and calculate the approximate value of the
integral.
Code:

Input and Result:

13 | P a g e
Numerical Methods Practicals

SOME BASIC PROGRAMMING PRACTICALS

ABSOLUTE VALUE OF AN INTEGER:


The absolute value of a number is its non-negative value without regard to its sign. In
programming, the absolute value is useful when dealing with distances, errors, or
comparisons where only the magnitude matters. This code takes an integer input from
the user and returns its absolute value.
Code:

Input and Result:

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):

Input and Result:

14 | P a g e
Numerical Methods Practicals

Code (Using while loop):

Input and Result:

SUM OF HARMONIC SERIES:


The harmonic series is the sum of the reciprocals of natural numbers, expressed as:
1 1 1 1
𝐻𝑛 = 1 + + + + ⋯ +
2 3 4 𝑛

Code (using for loop): Input and Result:

Code (using while loop): Input and Result:

15 | P a g e
Numerical Methods Practicals

SORTING USING BUBBLE SORT:


Bubble Sort is a simple comparison-based sorting algorithm used to arrange elements in
a specified order, typically ascending or descending. It works by repeatedly stepping
through the list, comparing adjacent elements, and swapping them if they are in the
wrong order. This process continues until the list is completely sorted

Code (Bubble sort): Input and Result:

16 | P a g e

You might also like