牛顿迭代法解方程

作者在 2012-10-28 18:28:51 发布以下内容
//:牛顿迭代法
#include <stdio.h>
#include <math.h>

int main(){
    float a, b, c, d;
    float x1, x2, f1, f2;

    printf("输入4个数:");
    scanf("%f%f%f%f", &a, &b, &c, &d);
    x1 = 0;
    x2 = 1;
    while(fabs(x1-x2)>1e-6){
        x1 = x2;
        f1 = a * x1* x1 * x1 + b * x1 * x1 + c * x1 + d;
        f2 = 3 * a * x1 * x1 + 2 * b * x1 + c;
        x2 = x1 - f1/f2;
    }
    printf("%fn", x2);
    return 0;
}
 
基础编程 | 阅读 1194 次
文章评论,共0条
游客请输入验证码
浏览18294次
文章归档