牛顿迭代法求方程的根,可是运行怎么不对呀,求高手,
#include<iostream>#include<cmath>
#include<iomanip>
using namespace std;
double f(double );
double qiuxielv(double);
double root(double,double);
void main()
{
double x0,x1,x;
cout<<"please input x0:"<<endl;
cin>>x0;
x=root(x0,x1);
cout<<setiosflags(ios::fixed)<<setprecision(7);
cout<<"the root of the equation is "<<x<<endl;
}
double f(double x)
{
double y;
y=((x+2)*x+3)*x+4;
return y;
}
double qiuxielv(double x)
{
double y;
y=(3*x+4)*x+3;
return y;
}
double root(double x0,double x1)
{
double f0,f2;
do
{
f0=f(x0);
f2=qiuxielv(x0);
x1=x0-f0/f2;
x0=x1;
}while(fabs(x1-x0)>=1e-6);
return x0;
}