Perform iteration until the tolerance is reached, or the number of iterations has reached the maximum specified by nMaxNumIterations variable.
int Iterate(int nMaxNumIterations, int nMethod = FITMETH_LEVENBERG_MARQUARDT, bool bAllowCancel = false, int* lpnActualInterateCount = NULL, int* lpnIterateOutCome = NULL, DWORD dwOption = 0)
Return 0 if set successfully, -1 for failure.
EX1
This example shows to do iteration until the tolerance is reached.
#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; }
Originlab\NLFitSession.h