boost bisection example

Can somebody give me a hand with the bisection method implemented in boost. I found this http://www.boost.org/doc/libs/1_47_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals1/roots2.html but there is no example how to use it.
Lets say I have a function
1
2
3
4
double f(double x)
{
return (x*x-1);
}
what do I have to write in main() to get the result?
Last edited on
int main()
{
std::cout<<f(2)<<endl;
return 0;
}
I should have been more specific. This is what I have in mind
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <utility>
#include <iostream>
#include <boost/math/tools/roots.hpp>

class root
{
public:
	double f(double x)
	{
		return (x*x-1);
	}
	void findRoot()
	{
		typedef double (root::*MemFn)(double x);
		std::pair<double, double> found = boost::math::tools::bisect<MemFn, double, double> (&root::f,0.0,10.0,1.e-6);
		std::cout << "==> x = [" << found.first << ',' << found.second << "]\n";
	}
};

int main()
{
	root solve;
	solve.findRoot();
	return (0);
}
But than I get
Invoking: GCC C++ Compiler
g++ -I/home/ppisarski/lib/bazille/boost_1_47_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
/home/ppisarski/lib/bazille/boost_1_47_0/boost/math/tools/roots.hpp: In function ‘std::pair<T, T> boost::math::tools::bisect(F, T, T, Tol, uintmax_t&, const Policy&) [with F = double (root::*)(double), T = double, Tol = double, Policy = boost::math::policies::policy<boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy>]’:
/home/ppisarski/lib/bazille/boost_1_47_0/boost/math/tools/roots.hpp:180: instantiated from ‘std::pair<T, T> boost::math::tools::bisect(F, T, T, Tol) [with F = double (root::*)(double), T = double, Tol = double]’
../main.cpp:22: instantiated from here
/home/ppisarski/lib/bazille/boost_1_47_0/boost/math/tools/roots.hpp:99: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘f (...)’
/home/ppisarski/lib/bazille/boost_1_47_0/boost/math/tools/roots.hpp:100: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘f (...)’
/home/ppisarski/lib/bazille/boost_1_47_0/boost/math/tools/roots.hpp:180: instantiated from ‘std::pair<T, T> boost::math::tools::bisect(F, T, T, Tol) [with F = double (root::*)(double), T = double, Tol = double]’
../main.cpp:22: instantiated from here
/home/ppisarski/lib/bazille/boost_1_47_0/boost/math/tools/roots.hpp:130: error: ‘tol’ cannot be used as a function
/home/ppisarski/lib/bazille/boost_1_47_0/boost/math/tools/roots.hpp:133: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘f (...)’
make: *** [main.o] Error 1

And, Yes it is important for me to have the f(x) function as a member of the class that looks for its root.
This is where I am right now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cmath>
#include <utility>
#include <iostream>
#include <boost/math/tools/roots.hpp>

class tolerance {
public:
	tolerance(double eps) :
		_eps(eps) {
	}
	bool operator()(double a, double b) {
		return (fabs(b - a) <= _eps);
	}
private:
	double _eps;
};

double f(double x) {
	return (x * x - 1);
}

int main() {
	double a = 0.0;
	double b = 10.0;
	tolerance tol = 0.00001;

	std::pair<double, double> found = boost::math::tools::bisect(f, a, b, tol);
	std::cout << "==> x = [" << found.first << ',' << found.second << "]\n";
	return (0);
}
This code works.

But if I want to use that with member functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cmath>
#include <utility>
#include <iostream>
#include <boost/math/tools/roots.hpp>

class tolerance {
public:
	tolerance(double eps) :
		_eps(eps) {
	}
	bool operator()(double a, double b) {
		return (fabs(b - a) <= _eps);
	}
private:
	double _eps;
};

class root {
public:
	double f(double x) {
		return (x * x - 1);
	}
	void findRoot() {
		double a = 0.0;
		double b = 10.0;
		tolerance tol = 0.00001;

		typedef double (root::*MemFn)(double x);
		std::pair<double, double> found = boost::math::tools::bisect(&root::f,
				a, b, tol);
		std::cout << "==> x = [" << found.first << ',' << found.second << "]\n";
	}
};

int main() {
	root solve;
	solve.findRoot();
	return (0);
}
I get an error massage as above. Is there a workaround?
The only solution I found for this problem is not to use member functions.
How does bisect know which instance of root to call f on?

There is STL functor for this purpose that I don't know the name of, because I use boost::bind.

1
2
std::pair<double, double> found = boost::math::tools::bisect(
 boost::bind(&root::f, this, _1), a, b, tol);


Topic archived. No new replies allowed.