2.2.1.2.9 NLFitSession::IterateIterate
Description
Perform iteration until the tolerance is reached, or the number of iterations has reached the maximum specified by nMaxNumIterations variable.
Syntax
int Iterate(int nMaxNumIterations, int nMethod = FITMETH_LEVENBERG_MARQUARDT, bool bAllowCancel = false, int* lpnActualInterateCount = NULL, int* lpnIterateOutCome = NULL, DWORD dwOption = 0)
Parameters
- nMaxNumIterations
- [input] The maximum number of times to iterate.
- nMethod
- [input] FITMETH_LEVENBERG_MARQUARDT or FITMETH_SIMPLEX.
- FITMETH_LEVENBERG_MARQUARDT, perform the fit using Levenberg Marquardt algorithm.
- FITMETH_SIMPLEX, perform a Simplex fit to estimate the parameter values.
- bAllowCancel
- [input] If true, allow user press Esc key to interrupt iteration.
- lpnActualInterateCount
- [output] If not NULL, it receives the number of iteration actually performed.
- lpnIterateOutCome
- [output] If not NULL it receives the code describing the outcome of the iterative procedure. Possible values are from the FITITER enumeration in ONLSF.h.
- dwOption
- [input] If set as NLSFFIT_GENERATE_OUTPUTS_WITHOUT_ITERATION, will always generate outputs without iteration that can be retrieved by GetFitResultsParams.
Return
Return 0 if set successfully, -1 for failure.
Examples
EX1
This example shows to do iteration until the tolerance is reached.
- Prior to running the following example, the nlsf_utils.c file need to be loaded and compiled. This can be done from script with the command run.LoadOC(Originlab\nlsf_utils.c) or just add this file to your workspace.
- New a worksheet and import \Samples\Curve Fitting\Gaussian.dat.
- Copy and compile the following codes, and run "NLFitSession_Iterate_ex1" in Command window.
#include <..\originlab\NLFitSession.h>
bool NLFitSession_Iterate_ex1()
{
Worksheet wks = Project.ActiveLayer();
if ( !wks || wks.GetNumCols() < 2 )
{
out_str("Error. Please make sure activate one worksheet contains at least two columns with data.");
return false;
}
//prepare input data
XYRange drInput;
drInput.Add(wks, 0, "X");
drInput.Add(wks, 1, "Y");
vector vX, vY;
drInput.GetData(vY, vX);
NLFitSession nlfSession;
// Set fit function
if ( !nlfSession.SetFunction("Gauss") )
{
out_str("Fail to set function!");
return false;
}
// Set fit data
if ( !nlfSession.SetData(vY, vX) )
{
out_str("Fail to set data!");
return false;
}
// Run parameter initialization codes to initialize parameter value
if( !nlfSession.ParamsInitValues())
{
out_str("fail to initialize parameters");
return false;
}
// Iterations will be carried out until the tolerance is reached
int nCount = 0;
const int nNumMaxIteration = 100;
bool bReached = false;
int nIterateOutCome;
double dTol = 1e-12;
nlfSession.SetTolerance(dTol);
while(nCount < nNumMaxIteration)
{
// perform a single iteration in each loop
int nNum = 1;
int nActualInterateCount;
if( nlfSession.Iterate(nNum, FITMETH_LEVENBERG_MARQUARDT, true, &nActualInterateCount, &nIterateOutCome) != 0 )
{
out_str("Fail to iterate!");
return false;
}
nCount = nCount + nActualInterateCount;
if( FITITER_REACHED_TOLERANCE == nIterateOutCome )
{
bReached = true;
break;
}
}
string strOutcome = nlfSession.GetFitOutCome(nIterateOutCome);
printf("The count of iterations is %d.\n%s\n", nCount, strOutcome);
// Prepare fit x data
double dMin, dMax;
vX.GetMinMax(dMin, dMax);
int nPoints = 100;
vector vFitX;
vFitX.Data(dMin, dMax, (dMax - dMin)/nPoints);
// Calculate fit y data
vector vFitY( vFitX.GetSize() );
nlfSession.GetYFromX(vFitX, vFitY, vFitY.GetSize());
// Put fit data to input worksheet new columns
int nFitXCol = wks.AddCol("FitX");
int nFitYCol = wks.AddCol("FitY");
wks.Columns(nFitXCol).SetType(OKDATAOBJ_DESIGNATION_X);
XYRange drFit;
drFit.Add(wks, nFitXCol, "X");
drFit.Add(wks, nFitYCol, "Y");
drFit.SetData(&vFitY, &vFitX);
return true;
}
Remark
See Also
Fit
Header to Include
Originlab\NLFitSession.h
|