# Boost - brent_find_minima problem

This topic is 3121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This works (although with predefined values)
void CtestDlg::OnBnClickedButton1()
{
doSomething();
}

double sinc(double x) {
static const double PI = 3.14159265358979323846264338327950288419716939937510;
return (x == 0.0) ? 1.0 : sin( PI * x ) / ( PI * x );
}

double x(double t) {
static double vals[6] = { 5.0, 14.0, 60.0, 111.0, 100.0, 37.0 };
double result = 0;
for (int i = 0; i < 6; i++) result += vals * sinc(t-i);
return -result;
}

typedef std::pair<double, double> Range;

void CtestDlg::doSomething()
{
std::pair<double, double> mm = boost::math::tools::brent_find_minima(x, 0.0, 6.0, 64);
}


But if I make those functions class members and try to pass values like this
void CtestDlg::OnBnClickedButton1()
{
vals[0]=5.0;
vals[1]=14.0;
vals[2]=60.0;
vals[3]=111.0;
vals[4]=100.0;
vals[5]=37.0;
doSomething();
}

typedef std::pair<double, double> Range;

void CtestDlg::doSomething()
{
std::pair<double, double> mm = boost::math::tools::brent_find_minima(x, 0.0, 6.0, 64);
}

double CtestDlg::sinc(double x) {
static const double PI = 3.14159265358979323846264338327950288419716939937510;
return (x == 0.0) ? 1.0 : sin( PI * x ) / ( PI * x );
}

double CtestDlg::x(double t) {
//static double vals[6] = { 5.0, 14.0, 60.0, 111.0, 100.0, 37.0 }; -THIS CANCLED OUT
double result = 0;
for (int i = 0; i < 6; i++) result += vals * sinc(t-i);
return -result;
}


I get errors: Error 1 error C3867: 'CtestDlg::x': function call missing argument list; use '&CtestDlg::x' to create a pointer to member Error 2 error C2780: 'std::pair<T,T> boost::math::tools::brent_find_minima(F,T,T,int,boost::uintmax_t &)' : expects 5 arguments - 4 provided and if I do type there &CtestDlg::x instead of just x I get error in boost itself Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:\program files\boost\boost_1_39\boost\math\tools\minima.hpp How can I fix it? (The program uses this values to define Y of the curve and mm is the x value where the curve has maximum)

##### Share on other sites

But I noticed you define the constant pi in your code.

You can use boost::math::constants::pi

http://www.boost.org/doc/libs/1_42_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals1/constants.html

It gives you a better accuracy and as you already use boost then you might as well use their pi, too.

##### Share on other sites
Thanks!
I solved the main problem by making vals[] global variable (unelegant but it works)

##### Share on other sites
The problem is that a member function must be converted to a functor before being able to be passed as a variable. The keyword here is binding (std::bind1st() etc.).

I found a forum topic elsewhere that has an answer to exactly your question: http://www.velocityreviews.com/forums/t690259-function-that-requires-a-function-as-an-argument-brent_find_minima.html

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633410
• Total Posts
3011726
• ### Who's Online (See full list)

There are no registered users currently online

×