The bisection method is an approximation method to find the roots of the given equation by repeatedly dividing the interval. This method will divide the interval until the resulting interval is found, which is extremely small. You are required to: i. write a pseudocode algorithm to determine the roots of a polynomial equation using Bisection Method. ii. develop a trace table to test the algorithm. iii.write C++ computer programs to determine the roots of an equation using Bisection Method.
#include<bits/stdc++.h>
using namespace std;
float f(float x)
{
return (x*x*x - 4*x - 9);
}
void bisect(float *x,float a,float b,int *itr)
{
*x = (a + b)/2;
++(*itr);
cout << "\nIteration no." <<setw(3) << *itr<< "X = " << setw(7) << setprecision(5)<< *x ;
}
int main()
{
int itr = 0, maxitr;
float x, a, b, aerr, x1;
cout << "Enter the value of a ";
cin>>a;
cout<<"\nEnter the value of b ";
cin>>b;
cout<< "\nEnter allowed error ";
cin>>aerr;
cout<<"\nEnter maximum iterations allowed ";
cin >> maxitr;
cout << fixed;
bisect(&x,a,b,&itr);
do
{
if (f(a)*f(x) < 0)
b = x;
else
a = x;
bisect (&x1,a,b,&itr);
if (fabs(x1-x) < aerr)
{
cout << "\nAfter " << itr << " iterations, root = "<< setw(6) << setprecision(4)<< x1 ;
return 0;
}
x = x1;
} while (itr < maxitr);
cout << "\nSolution does not converge, iterations not sufficient" << endl;
return 1;
}
Comments
Leave a comment