Minimizes single variable function.
the number of iterations required to reach the solution(minimum) within given tolerance.
Minimizes the function of one variable. This routine uses slightly modified version of the algol 60 procedure localmin, introduced by Richard Brent.
Example 1
Problem: Find the minimum of the function of single variable by using the Brent method.
Solution: The function is defined by the following equation:
Uses MtxVec, Math387, Optimization;
function Fun(Pars: TVec: Const Consts: Array of TSample;
Const ObjConsts: Array of TObject): TSample;
begin
Result := Sin(Pars[0])+Sqr(Pars[0]+2);
// note that Pars holds only one variable !
end;
procedure Example;
var Res,x : TSample;
begin
// initial estimates for x1 and x2
Res := MinBrent(-10,10,Fun,[],[],x,500,1e-8);
// stop if Iters >500 or Tolerance < 1e-8
// Returns Res = -1.8582461797
end;
#include "MtxVecCpp.h" //MtxVecCPP.cpp must be included in the project
#include "Math387.hpp"
#include "Optimization.hpp"
double __fastcall Fun(TVec * x, double const * c, const int c_Size, System::TObject* const * o, const int o_Size)
{
double* X = x->PValues(0);
return Sin(X[0])+IntPower(X[0]+2,2);
// note that Pars holds only one variable !
}
void __fastcall Example();
{
// initial estimates for x1 and x2
double x;
int iter = MinBrent(-10,10,Fun,NULL,-1,NULL,-1,x,500,1.0E-8,NULL);
// stop if Iters >500 or Tolerance < 1e-8
// returns res = -1.8582461797
}
private double Fun(TVec pars, double[] c, object[] o)
{
return Math.Sin(Pars[0])+Math387.IntPower(Pars[0]+2,2);
// note that Pars holds only one variable !
}
private void Example()
{
// initial estimates for x1 and x2
double x;
double res = Optimization.MinBrent(-10,10,Fun,null,null,out x, 500,1e-8,null);
// stop if Iters >500 or Tolerance < 1e-8
// Returns res = -1.8582461797
}