4.24

发布时间 2023-04-24 19:34:01作者: 孙锺鸣

#include<stdio.h>
#include<math.h>
main(){
float solution(float a,float b,float c,float d);
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;
}