0% found this document useful (0 votes)
56 views

NM First Practical

The document describes implementing the bisection method and secant method for finding the root of a function numerically. The bisection method works by repeatedly bisecting an interval and narrowing down the search space until the root is found. The secant method uses linear interpolation between two points to approximate the root, with an iterative formula. Source code in C is provided to implement both methods to find the root of sample functions within a given tolerance over a maximum number of iterations.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

NM First Practical

The document describes implementing the bisection method and secant method for finding the root of a function numerically. The bisection method works by repeatedly bisecting an interval and narrowing down the search space until the root is found. The secant method uses linear interpolation between two points to approximate the root, with an iterative formula. Source code in C is provided to implement both methods to find the root of sample functions within a given tolerance over a maximum number of iterations.

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 PDF, TXT or read online on Scribd
You are on page 1/ 9

Practical :1

Objective: To implement and understand the bisection method for


finding the root of a given function.
Theory:
The bisection method is a numerical technique used to find the root of
a continuous function within a given interval. It works by repeatedly
bisecting the interval and narrowing down the search space until a root
is found or until the desired accuracy is achieved.
Algorithm:
Steps:
Check if f(0) * f(1) >= 0. If true, the bisection method cannot be applied
to the given interval [a, b]. Exit the algorithm.
Set the iteration counter step= 1.
While step <= maximum number of iterations:
a. Set x2 = (xo+ x1) / 2 as the midpoint of the interval.
b. Evaluate f(2).
c. If |f(2)| < desired tolerance or f(2) = 0, then x2 is the root. Exit the
algorithm.
d. If f(0) * f(2) < 0, set x1= x2 (the root is in the interval [x0, x2]).
e. Otherwise, set x0= x2 (the root is in the interval [x2, x1]).
f. Increment step by 1.
The bisection method was unsuccessful within the specified number of
iterations. Exit the algorithm.
Source code:
#include <stdio.h>
#include <math.h>
#define f(x) x*x*x-2*x+1

int main()
{
float x0,x1,x2,f0,f1,f2,e;
int step=1;
up:
printf("Enter two intial guesses:\n");
scanf("%f %f",&x0,&x1);
printf("Enter tolerable error.\n");
scanf("%f",&e);
f0= f(x0);
f1= f(x1);
if(f0*f1>0.0){
printf("Incorrect initial guesses.\n");
goto up;

}
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);
return 0;

}
Output:
Practical :2
Objective:
To implement and understand the Secant Method for solving nonlinear
equations using the Numerical Methods approach.

Theory:
The Secant Method is an iterative numerical method used to
approximate the root of a nonlinear equation. It is an improvement
over the Bisection Method and the Newton-Raphson Method. The
Secant Method requires two initial guesses, and it uses a linear
interpolation between these two points to approximate the root.

Given an equation f(x) = 0, the Secant Method can be applied using the
following iterative formula:

x_(n+1) = x_n - (f(x_n) * (x_n - x_(n-1))) / (f(x_n) - f(x_(n-1)))

where x_n and x_(n-1) are the current and previous approximations of
the root, respectively, and x_(n+1) is the next approximation.

Algorithm:
1 Input initial guesses x0 and x1, the tolerance error e, and the
maximum number of steps n.
2 Compute f0 = f(x0) and f1 = f(x1), where f(x) is the function for which
we want to find the root.
3 Set step = 2.
4 Repeat steps 5-9 while step ≤ n.
5 Calculate x2 using the secant formula:
x2 = x1 - ((f1 * (x1 - x0)) / (f1 - f0))
6 Compute f2 = f(x2).
7 Check if |f2| ≤ e. If true, x2 is the root. Terminate the loop.
8 Update x0 = x1, x1 = x2, f0 = f1, and f1 = f2.
9 Increment step by 1.
10 If the loop ends without finding a suitable root, print "Method failed
after n steps."
11 Print the root x2 as the solution.
Source code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

/* Defining equation to be solved.


Change this equation to solve another problem. */
#define f(x) x*x*x - 2*x +1

int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
printf("\nEnter initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Enter maximum iteration:\n");
scanf("%d", &N);

/* Implementing Secant Method */


printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
printf("Mathematical Error.");
exit(0);
}

x2 = x1 - (x1 - x0) * f1/(f1-f0);


f2 = f(x2);

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

x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;

step = step + 1;

if(step > N)
{
printf("Not Convergent.");
exit(0);
}
}while(fabs(f2)>e);

printf("\nRoot is: %f", x2);


return 0;
}
Output:
Another caser when no. of iteration is given less, the output will be:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy