You are here: Symbol Reference > Optimization Namespace > Functions > Optimization.Marquardt Function
MtxVec VCL
ContentsIndex
Example

Problem: Find the minimum of the "Banana" function by using the Marquardt method. 

Solution:The Banana function is defined by the following equation: 

 

Also, Marquardt method requires the gradient and Hessian matrix of the function. The gradient of the Banana function is: 

 

and the Hessian matrix is : 

 

 

Uses MtxVec, Math387, Optimization; function Banana(const Pars: TVec; const Consts: TVec; const PConsts: array of TObject): double; begin Banana := 100*Sqr(Pars[1]-Sqr(Pars[0]))+Sqr(1-Pars[0]); end; procedure GradHessBanana(Fun: TRealFunction; const Pars: TVec; const Consts: TVec; const PConsts: array of TObject; const Grad: TVec; const Hess: TMtx); begin Grad[0] := -400*(Pars[1] - Sqr(Pars[0]))*Pars[0] - 2*(1 - Pars[0]); Grad[1] := 200*(Pars[1] - Sqr(Pars[0])); Hess[0,0] := -400*Pars[1] + 1200*Sqr(Pars[0])+2; Hess[0,1] := -400*Pars[0]; Hess[1,0] := Hess.Values[0,1]; // symmetric ! Hess[1,1] := 200; end; procedure Example; var Iters : integer; Pars : Array [0..1] of double; StopReason : TOptStopReason; begin // initial estimates for x1 and x2 Pars[0] := 0; Pars[1] := 0; Iters := Marquardt(Banana,GradHessBanana,Pars,[],[],FMin,StopReason,mvDouble,IHess); //stop if Iters > 500 or Tolerance < 1e-8 // Returns Pars = [1,1] and FMin = 0, meaning x1=1, x2=1 and minimum value is 0 end;
#include "MtxExpr.hpp" #include "Math387.hpp" #include "Optimization.hpp" // Objective function double __fastcall Banana(TVec* const Parameters, TVec* const Constants, System::TObject* const * ObjConst, const int ObjConst_Size) { double* Pars = Parameters->PValues1D(0); return 100.0*IntPower(Pars[1]-IntPower(Pars[0],2),2)+IntPower(1.0-Pars[0],2); } // Analytical gradient and Hessian matrix of the objective function void __fastcall BananaGradHess(TRealFunction Fun, TVec* const Parameters, TVec* const Consts, System::TObject* const * ObjConst, const int ObjConst_Size, TVec* const Grad, TMtx* const Hess) { double* Pars = Parameters->PValues1D(0); Grad->Values[0] = -400*(Pars[1]-IntPower(Pars[0],2))*Pars[0] - 2*(1-Pars[0]); Grad->Values[1] = 200*(Pars[1]-IntPower(Pars[0],2)); Hess->Values1D[0] = -400*Pars[1]+1200*IntPower(Pars[0],2)+2; Hess->Values1D[1] = -400*Pars[0]; Hess->Values1D[2] = -400*Pars[0]; Hess->Values1D[3] = 200; } void __fastcall Example(); { double Pars[2]; double fmin; TOptStopReason StopReason; // initial estimates for x1 and x2 Pars[0] = 0; Pars[1] = 0; int iters = Marquardt(Banana,GradHessBanana,Pars,1,NULL,-1,NULL,-1,fmin, StopReason, mvDouble, true,false,1000,1.0e-8,1.0e-8,NULL); // stop if Iters >1000 or Tolerance < 1e-8 }
Copyright (c) 1999-2025 by Dew Research. All rights reserved.