Lab NM
Lab NM
Name:Riwaj Basnet
Faculty : BCE(III/I)
Roll number : 64
Group : C
Practical 1:
➢ To implement and understand the bisection method for
finding the root of a given function.
Theory:
The bisection method is a numerical root-finding algorithm
used to approximate the roots of a continuous function. It
operates by repeatedly dividing an interval in half and
determining which half contains a root of the function. The
method is based on the intermediate value theorem, which
states that if a continuous function changes sign over an
interval, it must have at least one root within that
interval.
Algorithm for Bisection Method
• Start
• Input initial guess X1 and X2 and initialize
error E.
• Compute f(X1) & f(X2).
• If f(X1) f(X2)> 0 , go to step 2.
(X1+X2)
• Compute 𝑋𝑜 = and f(Xo).
2
• If f(X1) x f(X0) < 0
Set X2 = Xo
Else
Set X1 = Xo
𝑋1+𝑋2
• If | | < 𝐸 , root = Xo
𝑋0
Else
Got to step 5
• Stop.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
/*
Defining equation to be solved.
Change this equation to solve another problem.
*/
#define f(x) x*x*x-2*x+1
void main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
/* Inputs */
up:
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
/* Calculating Functional Value */
f0 = f(x0);
f1 = f(x1);
/* Checking whether given guesses brackets the root or
not. */
if( f0 * f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
/* Implementing Bisection Method */
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = (x0 + x1)/2;
f2 = f(x2);
if( f0 * f2 < 0)
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
step = step + 1;
}while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
getch();
}
Output:
Practical 2:
➢ To implement and understand the Regular-False
method(False position method) for finding the root of a
given function.
Theory:
The False-Position method, also known as the Regular Falsi
method, is a numerical root-finding algorithm used to
approximate the roots of a function. It is an iterative
method that relies on linear interpolation between two
initial guesses to improve the approximation of the root.
Code:
#include <stdio.h>
#include <math.h>
float func(float x) {
// Define the function for which you want to find the
root: x*x*x - 2*x + 1
return x * x * x - 2 * x + 1;
}
do {
// Calculate function values at endpoints
fa = func(a);
fb = func(b);
if (fc == 0.0)
break;
if (fa * fc < 0)
b = c;
else
a = c;
return c;
}
int main() {
float a, b, epsilon, root;
printf("\nCalculation process:\n");
root = false_position(a, b, epsilon);
return 0;
}
Output:
Practical 3:
➢ To implement and understand the Newton-Raphson method
for finding the root of a given function.
Theory:
The Newton-Raphson method, also known as the Newton's
method, is an iterative numerical root-finding algorithm
used to approximate the roots of a function. It is a
powerful and efficient method for finding roots but requires
an initial guess close to the actual root for convergence.
𝑓(𝑋)
Xn=X-
𝑓′(𝑋)
• Set X=Xn.
• If |𝑓(𝑋𝑛)| < 𝐸 then print the root Xn else goto step 3.
• Stop
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#define error 0.00005
#define N 1000
float fx(float x)
{
return x*x*x-2*x+1;
}
float gx(float x)
{
return 3*x*x-2;
}
int main()
{
float x0,x1,fx0,gx0,fx1;
printf("Enter the initial guess:say x0\n");
scanf("%f",&x0);
int iteration=0;
do
{
fx0=fx(x0);
gx0=gx(x0);
if(gx0==0)
{
printf("Mathematical Error\n");
exit(1);
}
x1=x0-(fx0/gx0);
fx1=fx(x1);
printf("iteration %d:x1=%f\t
fx1=%f\n",++iteration,x1,fx1);
x0=x1;
if(iteration>N) break;
}while((fabs(fx1))>error);
printf("root is %f",x1);
return 0;
}
Output:
Practical 4:
➢ To implement and understand the Secant method for
finding the root of a given function.
Theory:
The Secant method is a numerical root-finding algorithm that
approximates the roots of a function. It is similar to the
Newton-Raphson method but does not require the evaluation of
the derivative of the function. Instead, it approximates the
derivative using finite differences.
𝑋1𝑓(𝑋2)−𝑋2𝑓(𝑥1)
Xn=
𝑓(𝑋2)−𝑓(𝑋1)
printf("****************************************************
*************************\n");
} while (fabs(x2-x1)>error);
printf("Hence the root is %f\n",x3);
return 0;
}
Output: