作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我要编译并运行此程序以使用 Newton-Raphson 算法求解方程,但 do-while
循环中的迭代次数始终为 1
,事实并非如此。
代码
#include<math.h>
#include<stdio.h>
float fonc(float x){
float result;
result=2*(pow(x,3))-x-2;
return result;
}
float foncprime(float x){
float result;
result= 6*(pow(x,2))-1;
return result;
}
int main(void)
{
long double x1,x0;
float eps;
eps=0.5*pow(10,-4);
x0=1;
x1=1;
int i=0;
do
{
x0=x1;
x1=x0-(fonc(x0)/foncprime(x0));
i++;
}
while(x1-x0>eps);
printf("%d",i);
}
最佳答案
您可能应该检查 while (fabs(x1-x0) > eps)
。
关于c - 牛顿-拉夫逊算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22492430/
我是一名优秀的程序员,十分优秀!