C語言練習-二分法解方程

二分法,滿足一個精度

#include<stdio.h>
#include<math.h>
double fx(double x);

int main()
{
    float a,b,c;
    do
    {
        scanf("%f %f",&a,&b);
    }
    while(fx(a)*fx(b)>=0);    // 滿足小於零才結束輸入

    printf("there is a root between[%f,%f]\n",a,b);

    do{
        c=(a+b)/2;
        if(fx(a)*fx(c)<0)
            b=c;
        else
            a=c;
    }while((fx(c)>0.00001)||(fx(c)<-0.00001));
    printf("the root is %.5f\n",c);
    return 0;
}

double fx(double x)
{
    double ret;
    ret = 2*x*x*x - 5*x*x + 3*x - 6;
    return ret;
}

猜你喜欢

转载自blog.csdn.net/weixin_38486169/article/details/86509630