Advertisement

每日一题--20200428牛顿迭代法求根C语言程序设计

阅读量:

牛顿迭代法求解三次方程实根

复制代码
     #include <stdio.h>
     #include <math.h>
    
     float solut(float a, float b, float c, float d)
     {
     float x = 1, x0, f, f1;
    
     do//开始迭代
     {
         x0 = x;
         f = ((a * x0 + b) * x0 + c) * x0 + d;
         f1 = (3 * a * x0 + 2 * b) * x0 + c;
         x = x0 - f / f1;
     }
     while (fabs(x - x0) >= 1e-5);
    
     return x;
     }
    
     int main()
     {
     float a, b, c, d;
    
     printf("\n输入方程的系数a、b、c、d:\n");
     scanf("%f,%f,%f,%f", &a, &b, &c, &d);
     printf("\n方程是:%5.2fx^3+%5.2fx^2+%5.2fx+%5.2f=0", a, b, c, d);

全部评论 (0)

还没有任何评论哟~