牛顿迭代公式为:
答案:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float sout(float a, float b, float c, float d);
float a, b, c, d;
cout<<"please enter a,b,c,d:";
cin>>a>>b>>c>>d;
cout<<"x="<<sout(a, b, c, d)<<endl;
return 0;
}
float sout(float a, float b, float c, float d)
{
float x = 1, x0, f, f1;
do {
x0 = x;
f = ((a * x0 + b) * x0 + c)*x0 + d;
f1 = (3 * a * x0 + 2 * b) * x0 + c;
x = x0 - f / f1;
} while (fabs(x - x0) >= 1e-3);
return x;
}