如何使用升压平分? [英] How to use boost bisection?
问题描述
昨天,我与另一升压功能的问题,但幸运的是,你们帮我解决这些问题。今天,我需要知道如何正确使用二分法功能。
Yesterday I had problems with another boost functions but luckily you guys helped me to solve them. Today I would need to know how to use bisection function properly.
因此,这里是如何,我认为它应该工作,但从来没有少,似乎我得到这个也是不对的。好了,所以我想用:
So here is how I think it should work but never the less it seems that I'm getting this also wrong. Okay so I would like to use:
template <class F, class T, class Tol>
std::pair<T, T>
bisect(
F f,
T min,
T max,
Tol tol);
从<一个href=\"http://www.boost.org/doc/libs/1_46_1/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals1/roots2.html\"相对=nofollow>这里但我的问题是宽容,因为我不知道如何设置是正确的。我试过
from here but my problem is with tolerance because I don't know how to set it right. I've tried
double value = boost::math::tools::eps_tolerance<double>(0.00001);
和如何我返回时,二分法已经找到了价值?如果结果是对数字作为的std ::对在功能,之后才算分钟+最大/ 2?
and how to I return the value when the bisection has found ? Should the result be pair of numbers as std::pair in the function and after that just calculate min+max/2?
谢谢!
推荐答案
这是一个例子使用的开张
。考虑求解方程 X ^ 2 - 3X + 1 = 0
:
This is an example use of bisect
. Consider solving the equation x^2 - 3x + 1 = 0
:
struct TerminationCondition {
bool operator() (double min, double max) {
return abs(min - max) <= 0.000001;
}
};
struct FunctionToApproximate {
double operator() (double x) {
return x*x - 3*x + 1; // Replace with your function
}
};
// ...
using boost::math::tools::bisect;
double from = 0; // The solution must lie in the interval [from, to], additionally f(from) <= 0 && f(to) >= 0
double to = 1;
std::pair<double, double> result = bisect(FunctionToApproximate(), from, to, TerminationCondition());
double root = (result.first + result.second) / 2; // = 0.381966...
编辑:另外,你这是怎么可以自定义函数使用它:
Alternatively, this is how you can use it with custom functions:
double myF(double x) {
return x*x*x;
}
double otherF(double x) {
return log(abs(x));
}
// ...
std::pair<double, double> result1 = bisect(&myF, from, to, TerminationCondition());
std::pair<double, double> result2 = bisect(&otherF, 0.1, 1.1, TerminationCondition());
这篇关于如何使用升压平分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!