牛顿法求方程的根

2019-12-04  本文已影响0人  gtxe
image.png
#include <stdio.h>
#include <math.h>
#include <string.h>

float a,b,c,d;

int main()
{
    float liudun();
    float fx(float x);
    float fx1(float x);

    float answer;
    printf("input a b c and d:");
    scanf("%f%f%f%f",&a,&b,&c,&d);
    answer=liudun(a,b,c,d);
    printf("the answer is %5.2f\n",answer);

    return 0;
}

float fx(float x)
{
    return a*x*x*x+b*x*x+c*x+d;
}

float fx1(float x)
{
    return 3*a*x*x+2*b*x+c;
}

float liudun()
{
    float x0,x=1;
    do
        {
            x0=x;
            x=x0-fx(x0)/fx1(x0);

        }while(fabs(x-x0)>=1e-4);
    return x;

}
image.png
上一篇 下一篇

猜你喜欢

热点阅读