You are on page 1of 2

Experiment no.

1
Bisection Method

1.1​ Objective 1.2​ Theory 1.3 ​Procedure/Code 1.4​ Result

1.1 Objective: ​To find the root of the non-linear equation by Bisection Method using C/C++

1.2 Theory: ​The simplest root-finding algorithm is the bisection method. The algorithm
applies to any continuous function f(x) on an interval [a,b] where the value of the function f(x)
changes sign from a to b. The idea is simple: divide the interval in two, a solution must exist
within one subinterval, select the subinterval where the sign of f(x) changes and repeat.

if f(c) = 0 means that we found the root of the function, which is c


if f(c) ≠ 0 we check the sign of f(c):
1. if f(c) has the same sign as f(a) we replace a with c and we keep the same value for
b.
2. if f(c) has the same sign as f(b), we replace b with c and we keep the same value for
a.

1.3 Procedure/Code:

#include<iostream>
using namespace std;
#define EP 0.01

double func(double x)
{
return x*x*x - 2*x - 5;
}

void bisection(double a, double b)


{
int i = 1;
if (func(a) * func(b) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}

double c = a;
while ((b-a) >= EP)
{
// Find middle point
c = (a+b)/2;

// Check if middle point is root


if (func(c) == 0.0)
break;

// Decide the side to repeat the steps


else if (func(c) * func(a) < 0)
b = c;
else
a = c;

cout << "The value of root after "<< i++ <<" is : " << c << endl;
}
}

int main()
{
double a = 0;
double b = 0;

cout << "Enter the value of a :";


cin >> a;
cout << "\nEnter the value of b :";
cin >> b;

bisection(a, b);
cout << endl;
cout << "\nAditya Sarawat \nECE-A \n31\n";
return 0;
}

1.4 Result:

You might also like