求方程x^3-2x^2+x-2=0在[1,3]之间的根。
答案应该是2,但我的运行结果是1.15366e+029
#include <iostream>
#include <math.h>
using namespace std;
float f(float x)
{
float y;
y=x*x*x-2*x*x+x-2;
return y;
}
float pointx(float x1,float x2)
{
float x0;
x0=x1*f(x2)-x2*f(x1)/f(x2)-f(x1);
return x0;
}
float root(float x1,float x2)
{
float x0;
do
{
x0=pointx(x1,x2);
if(f(x0)*f(x1)>0)
{x1=x0;
}
else x2=x0;
}
while((fabs(f(x0)>=1e-6))||(fabs(f(x0)<=-1e-6)));
return x0;
}
main()
{
float x0;
x0=root(1,3);
cout<<x0<<endl;
}