二分法解方程
#include <cmath>
#include <iostream>
using namespace std;
float f(float x)
{
return x * x * x - 5 * x *x + 16 * x - 80;
}
void main(void)
{
float x1, x2, x0, f0, f1, f2;
do
{
cout<<"Input x1, x2\n";
cin>>x1>>x2;
f1 = f(x1);
f2 = f(x2);
} while (f1 * f2 > 0);
do
{
x0 = ( x1 + x2) / 2;
f0 = f(x0);
if ((f0*f1)>0)
{
x1 = x0;
f1 = f0;
}else
{
x2 = x0;
f2 = f0;
}
} while (fabs(f0)>=0.0001);
cout<<"x = "<< x0 <<endl;
}
追求永不止步