Lab NM
Lab NM
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.
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);
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.
X 1 f ( X 2 )−X 2 f ( X 1)
Xo= f ( X 2 )−f (X 1)
Calculate f(Xo).
If f(Xo) x f(X1) < 0 then;
X2=Xo else
X1=Xo
X 2−X 1
If | X1
∨¿ E , then print the root Xo else go to step 3.
Stop
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("Enter the interval [a, b] where you want to find the root:\n");
printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("\nCalculation process:\n");
root = false_position(a, b, epsilon);
printf("\nRoot: %.6f\n", root);
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.
The method is based on the idea of approximating the function by its
tangent line at a given point. The tangent line is then extended to intersect
the x-axis, providing an improved estimate of the root. This process is
repeated iteratively, generating a sequence of approximations that
converge to the actual root of the function.
f (X)
Xn=X- f '( X )
Set X=Xn.
If |f ( Xn )|< E 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.
Stop.
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define error 0.000005
float function(float x)
{
return x*x*x-2*x+1;
}
int main()
{
printf("Our Question is :X^3-2x+1\n");
float x1,x2,x3,fx1,fx2,fx3;
printf("Enter the first Guess:Say x1\n");
scanf("%f",&x1);
printf("Enter the Second Guess:say x2\n");
scanf("%f",&x2);
int iteration=0;
do
{
fx1=function(x1);
fx2=function(x2);
x3=(x1*fx2-x2*fx1)/(fx2-fx1);
fx3=function(x3);
printf("Iteration=%d:",++iteration);
printf("x1=%f\t x2=%f\t x3=%f\t fx3=%f\n",x1,x2,x3,fx3);
x1=x2;
x2=x3;
printf("*****************************************************************************\
n");
} while (fabs(x2-x1)>error);
printf("Hence the root is %f\n",x3);
return 0;
}
Output: