double Seek_roots::bisection(double a, double b, double eps, double (*f)(double)) {
int i=0;
double fa=f(a);
double fb=f(b);

if (fa*fb>0)     {         
    cout << "错误:f(a) 和 f(b) 具有相同的符号。" << endl;         
    return NAN;     
}     

double c=(a+b)/2;     
double fc = f(c);     

while (abs(fc) > eps)     {         
    i++;         
    printf("%d.[%.6f,%.6f]",i,a,b);         
    printf("  %c 精度:%.7f\n", f(c)>0 ? '+':'-', abs(b-a)); // 简化if-else语句         
    if (fa*fc < 0)         {             
        b=c;             
        fb=fc;         
    }         
    else         {             
        a=c;             
        fa=fc;         
    }         
    c=(a+b)/2;         
    fc=f(c);     
}     
return c; 

}

double Seek_rootsbisectiondouble a double b double eps double fdouble int i=0; double fa=fa; double fb=fb; if fafb0 cout 错误:fa 和 fb 具有相同的符号。 endl; return NAN;

原文地址: http://www.cveoy.top/t/topic/bndw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录