gpt4 book ai didi

c - 牛顿-拉夫逊算法

转载 作者:太空狗 更新时间:2023-10-29 16:07:32 25 4
gpt4 key购买 nike

我要编译并运行此程序以使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com