1 // (C) Copyright Gennadiy Rozental 2001-2015. 2 // Distributed under the Boost Software License, Version 1.0. 3 // (See accompanying file LICENSE_1_0.txt or copy at 4 // http://www.boost.org/LICENSE_1_0.txt) 5 // 6 // See http://www.boost.org/libs/test for the library home page. 7 // 8 9 10 //[snippet8 find_root(double (* f)(double),double low_guess,double high_guess,std::vector<double> & steps,double tolerance)11double find_root( double (*f)(double), 12 double low_guess, 13 double high_guess, 14 std::vector<double>& steps, 15 double tolerance ) 16 { 17 double solution; 18 bool converged = false; 19 20 while(not converged) 21 { 22 double temp = (low_guess + high_guess) / 2.0; 23 steps.push_back( temp ); 24 25 double f_temp = f(temp); 26 double f_low = f(low_guess); 27 28 if(abs(f_temp) < tolerance) 29 { 30 solution = temp; 31 converged = true; 32 } 33 else if(f_temp / abs(f_temp) == f_low / abs(f_low)) 34 { 35 low_guess = temp; 36 converged = false; 37 } 38 else 39 { 40 high_guess = temp; 41 converged = false; 42 } 43 } 44 45 return solution; 46 } 47 //] 48