6牛顿迭代法

#include <stdio.h>
#include <math.h>

float solution(float a, float b, float c, float d)
{
float x0, f, fd, h;
float x = 1.5;

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;
}

int main()
{
float a, b, c, d;

float x;

printf("请输入方程的系数:");

scanf("%f %f %f %f", &a, &b, &c, &d);

x = solution(a, b, c, d);

printf("\n");

printf("所求方程的根为:x=%f\n", x);

return 0;
}v

posted @ 2023-04-23 20:48  几条小鱼  阅读(17)  评论(0)    收藏  举报