牛顿迭代法求方程根

问题描述:编写用牛顿迭代法求方程根的函数。方程为ax三次方+bx平方+cx+d=0,系数a,b,c,d由主函数输入,求x在1附近的一个实根,由主函数输出。

完整程序:

#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)>=le-5);

return x;

}

 

posted @ 2023-04-22 18:59  张佳木  阅读(184)  评论(0)    收藏  举报