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

Lab NM

The document describes implementing numerical root-finding algorithms in C code to find the root of a given function. It includes 4 practical assignments: 1) Implementing the bisection method to approximate roots. 2) Implementing the false position method using linear interpolation. 3) Implementing Newton's method based on approximating the tangent line. 4) Implementing the secant method which approximates the derivative using finite differences. Pseudocode and C code implementations are provided for each method.

Uploaded by

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

Lab NM

The document describes implementing numerical root-finding algorithms in C code to find the root of a given function. It includes 4 practical assignments: 1) Implementing the bisection method to approximate roots. 2) Implementing the false position method using linear interpolation. 3) Implementing Newton's method based on approximating the tangent line. 4) Implementing the secant method which approximates the derivative using finite differences. Pseudocode and C code implementations are provided for each method.

Uploaded by

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

Submitted by:

Name: Swagat Adhikari


Faculty : BCE(III/I)
Roll number : 92
Group : D

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.
(X 1+ X 2)
 Compute Xo=
2
and f(Xo).
 If f(X1) x f(X0) < 0
Set X2 = Xo
Else
Set X1 = Xo
 If | X 1+X 0X 2|< E , root = X o

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

printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);


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.

Algorithm for False Position Method


 Define a function f(X) and minimum permissible error E.
 Decide initial guess X1 and X2.
 Calculate f(X1) and f(X2).
 If f(X1) x f(X2) > 0 then no root lies between X1 and X2 so go to step 9
else go to step 5.
 Calculate :

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;
}

float false_position(float a, float b, float epsilon) {


float c, fa, fb, fc;

do {
// Calculate function values at endpoints
fa = func(a);
fb = func(b);

// Calculate the next approximation using false position formula


c = a - (fa * (b - a)) / (fb - fa);
fc = func(c);

if (fc == 0.0)
break;
if (fa * fc < 0)
b = c;
else
a = c;

printf("Approximation: %.6f\n", c);


} while (fabs(fc) >= epsilon);

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("Enter the tolerable error (epsilon): ");


scanf("%f", &epsilon);

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.

Algorithm for Newton-Raphson Method


 Define a function f(x) and permissible error E.
 Decide initial guess X= Xo.
 Compute f(X) and f’(X).
 If f’(X)=0 then solution can’t be computed so go to step 8.
 Compute

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.

Algorithm for Secant Method


 Define a function f(X) and minimum permissible error E.
 Decide initial guess X1 & X1.
 Calculate f(X1),f(X2)and
X 1 f ( X 2 )−X 2 f ( x 1)
Xn= f ( X 2 )−f (X 1)

 Set X1 = X2 and X2 = Xn.


 If | X 3−X 3X 2|< E,then print X else go tostep 3.
3

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

You might also like