Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说用牛顿迭代法求方程的根C语言_牛顿迭代法的计算过程,希望能够帮助你!!!。
题目要求:牛顿迭代法是一种重要的基本的求方程根的方法。现有方程为axˆ3+bxˆ2+cx+d=0,系数a,b,c,d的值一次为1,2,3,4,由主函数输入。求x在1附近的一个实根。
思路:
如图所示:
以下是具体代码:
#include <stdio.h>
#include <math.h>
int main()
{
double newton_method(int a, int b, int c, int d);
int a,b,c,d;
double root;
printf("please enter a,b,c,d: ");
scanf("%d %d %d %d",&a,&b,&c,&d);
root=newton_method(a,b,c,d);
printf("The root is: %f\n",root);
return 0;
}
double newton_method(int a, int b, int c, int d)
{
double x1,x0,fx,f;
x1=1.0;
while(fabs(x1-x0)>=1e-5)
{
x0=x1;
fx=a*x0*x0*x0+b*x0*x0+c*x0+d;
f=3*a*x0*x0+2*b*x0+c;
x1=x0-fx/f;
}
return(x1);
}
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
上一篇
已是最后文章
下一篇
已是最新文章