函数作为函数的参数,使用二分法C++

Function as parameter of a function, using bisection method C++

 // Function as parameter of a function

#include <iostream>
#include <cmath>
#include <cassert>

using namespace std;

const double PI = 4 * atan(1.0); // tan^(-1)(1) == pi/4 then 4*(pi/4)== pi

typedef double(*FD2D)(double);

double root(FD2D, double, double); //abscissae of 2-points, 
//For the algorithm to work, the function must assume values of opposite sign in these two points, check
// at point 8

double polyn(double x) { return 3 - x*(1 + x*(27 - x * 9)); }

int main() {
double r;

cout.precision(15);

r = root(sin, 3, 4);
cout << "sin:       " << r << endl
    << "exactly:  " << PI << endl << endl;

r = root(cos, -2, -1.5);
cout << "cos:       " << r << endl
    << "exactly:  " << -PI/2 << endl << endl;

r = root(polyn, 0, 1);
cout << "polyn:       " << r << endl
    << "exactly:  " << 1./3 << endl << endl;

/* 
we look for the root of the function equivalent to function polyn
but this time defined as a lambda function 
*/
r = root([](double x) -> double { 
            return 3 - x*(1 + x*(27 - x * 9)); 
        }, 0, 1);
cout << "lambda:       " << r << endl
    << "exactly:  " << 1. / 3 << endl << endl;

return 0;
}
// Finding root of function using bisection.
// fun(a) and fun(b)  must be of opposite sign

double root(FD2D fun, double a, double b) {

static const double EPS = 1e-15; // 1×10^(-15)
double f, s, h = b - a, f1 = fun(a), f2 = fun(b);

if (f1 == 0) return a;
if (f2 == 0) return b;
assert(f1*f2<0); // 8.- macro assert from header cassert.

do {
    if ((f = fun((s = (a + b) / 2))) == 0) break;
    if (f1*f2 < 0) { 
        f2 = f; 
        b = s;
    }
    else {
        f1 = f; 
        a = s; 
    }

} while ((h /= 2) > EPS);

    return (a + b) / 2;

}

谁能解释一下双根函数中的循环是如何工作的?好像不是100%理解,网上查了这个二分法,纸上试了下,这个例子怎么也想不通。 提前致谢!

如果将"clever"行拆分成多行,就更容易理解了。这里有一些修改,加上评论:

double root(FD2D fun, double a, double b) {
    static const double EPS = 1e-15; // 1×10^(-15)
    double fa = fun(a), fb = fun(b);

    // if either f(a) or f(b) are the root, return that
    // nothing else to do
    if (fa == 0) return a;
    if (fb == 0) return b;

    // this method only works if the signs of f(a) and f(b)
    // are different. so just assert that
    assert(fa * fb < 0); // 8.- macro assert from header cassert.


    do {
        // calculate fun at the midpoint of a,b
        // if that's the root, we're done

        // this line is awful, never write code like this...
        //if ((f = fun((s = (a + b) / 2))) == 0) break;

        // prefer:
        double midpt = (a + b) / 2;
        double fmid = fun(midpt);

        if (fmid == 0) return midpt;

        // adjust our bounds to either [a,midpt] or [midpt,b]
        // based on where fmid ends up being. I'm pretty
        // sure the code in the question is wrong, so I fixed it
        if (fa * fmid < 0) { // fmid, not f1
            fb = fmid; 
            b = midpt;
        }
        else {
            fa = fmid; 
            a = midpt; 
        }
    } while (b-a > EPS); // only loop while
                         // a and b are sufficiently far
                         // apart

    return (a + b) / 2;  // approximation
}