You are on page 1of 2

#include <iostream>

#include <conio.h>
using namespace std;
const double eps = 0.2;
double f(double x)
{
return -26 + 82.3 * x - 88 * pow(x, 2) + 45.4 * pow(x, 3) - 9 * pow(x, 4) + 0.65 *
pow(x, 5);
}
double bisect(double xl, double xu)
{
double iter = 0;
double xr = 0;
double xrOld = 0;
double error = 0;
do
{
xrOld = xr;
xr = (xl + xu) / 2;
error = abs((xr - xrOld) / xr) * 100;
cout << "iteration=" << iter << " | xl=" << xl << " | f(xl)=" << f(xl)
<< " | xu=" << xu << " | f(xu)"
<< f(xu) << " | xr=" << xr << " | f(xr)=" << f(xr) << " Error%="
<< error << endl << endl;
_getch();
if (f(xl) * f(xr) > 0)
{
xl = xr;
}
else
{
xu = xr;
}
iter++;
} while (error > eps);
return xr;
}
int main(){
float xl, xu, root;
cout << "Enter lower value = ";
cin >> xl;
cout << "Enter upper value = ";
cin >> xu;
if (f(xl) * f(xu) < 0)
{
root = bisect(xl, xu);
cout << "root= " << root;
}
else
{
cout << "Not correct xl and xu" << endl;
}
_getch();
return 0;
}

You might also like