连续函数的根

#include 
#include 

double f(double const & x)
{
    return 4 * x + 1;
}

double root(double (*f)(double const &), double a, double b, double const & e)
{
    double middle = (a + b) / 2;
    while(f(middle) != 0 && fabs(b - 1) > e)
    {
        if(f(a) * f(middle) < 0)
        {
            b = middle;
        }
        else
        {
            a = middle;
        }
        middle = (a + b) / 2;
    }
    return middle;
}

int main()
{
    std::cout << root(f, -20, 20, 0.001);
    getchar();
    return 0;
}
answer_of_continuous_func : main.cpp
	g++ -g main.cpp -o answer_of_continuous_func
clean:
	rm answer_of_continuous_func   

连续函数的根_第1张图片

 

连续函数的根_第2张图片

 

连续函数的根_第3张图片

 

转载于:https://www.cnblogs.com/sunyongjie1984/p/4264827.html

你可能感兴趣的:(连续函数的根)