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

Lab NM

Uploaded by

riwajbasnet9
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)
29 views16 pages

Lab NM

Uploaded by

riwajbasnet9
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

Submitted by:

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

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


1. Start with two initial guesses a and b such that f(a)
and f(b) have opposite signs, i.e., f(a) * f(b) < 0.
2. Calculate the function values at the endpoints: f(a) and
f(b).
3. Calculate the next approximation c using the False-
Position formula: c = a - (f(a) * (b - a)) / (f(b) -
f(a)).
4. Evaluate the function at c: f(c).
5. If f(c) is sufficiently close to zero (i.e., within a
desired tolerance), consider c as the root and terminate
the algorithm.
6. If f(c) and f(a) have opposite signs, update b to c to
narrow down the interval where the root lies. Otherwise,
update a to c.
7. Repeat steps 2 to 6 until the difference between
successive approximations is smaller than the desired
tolerance or until a maximum number of iterations is
reached.
8. The final approximation c is considered the root of the
function within the desired tolerance.

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

𝑓(𝑋)
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.

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

𝑋1𝑓(𝑋2)−𝑋2𝑓(𝑥1)
Xn=
𝑓(𝑋2)−𝑓(𝑋1)

• Set X1 = X2 and X2 = Xn.


𝑋3−𝑋2
• If | | < 𝐸,then print X3 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