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 << "Iteration no." <<setw(3) << *itr<< "X = " << setw(7) << setprecision(5)<< *x << endl;
}
int main()
{
int itr = 0, maxitr;
float x, a, b, aerr, x1;
cout << "Enter the value of a : ";
cin >> a ;
cout << "Enter the value of b : ";
cin >> b ;
cout << "Enter allowed error : ";
cin >> aerr;
cout<< "Enter the number of iterations : ";
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 << "After" << itr << "iterations, root"<< "=" << setw(6) << setprecision(4)<< x1 << endl;
return 0;
}
x = x1;
} while (itr < maxitr);
cout << "Solution does not converge, iterations not sufficient" << endl;
return 1;
}
Comments
Leave a comment