1, 代码:
Simplex simp(1.0e-8);
Size maxIterations=100;
Size maxStationaryStateIterations=maxIterations/2;//要小于maxIterations
Real rootEpsilon=EPSILON;
Real functionEpsilon=EPSILON;
Real gradientNormEpsilon=EPSILON;
EndCriteria endCriteria(maxIterations,maxStationaryStateIterations,
rootEpsilon,functionEpsilon,gradientNormEpsilon);
boost::shared_ptr<MyCostFunction> func(new MyCostFunction());
func->setArgument(myFunction_parameters);
Constraint constraint = MyConstraint();
Array initialValue(1, MyInitGuessValue);
Problem p(*func,constraint,initialValue);
simp.minimize(p, endCriteria);
ret = p.currentValue()[0];
2,方程类MyCostFunction
class MyCostFunction : public CostFunction
{
public:
Real value(const Array& params) const {
Array tempRet = values(params);
Real ret = 0.0;
for (Size i = 0; i < params.size(); i++) {
ret += tempRet[i]*tempRet[i];
}
return ret;
};
//! method to overload to compute the cost function values in x
Disposable<Array> values(const Array& x) const {
Real guess = x[0];
Real ret = ..........我的要求解的公式
Array values(1,0);
values[0]=ret;
return values;
};
};