#include <bits/stdc++.h>
using namespace std;
float solution(float a,float b,float c,float d);
int main()
{
float a,b,c,d,x;
printf("请输入方程的系数: ");
scanf("%f %f %f %f",&a,&b,&c,&d);
x=solution(a,b,c,d);
printf("所求方程的根为x=%f",x);
}
float solution(float a,float b,float c,float d){
float x0,x=1.5,f,fd,h;
do{
x0=x;
f=a*x0*x0*x0+b*x0*x0+c*x0+d;
fd=3*a*x0*x0+2*b*x0+c;
h=f/fd;
x=x0-h;
}while(fabs(x-x0)>=1e-5);
return x;
}
牛顿迭代法求方程根
最新推荐文章于 2024-05-28 21:13:10 发布