NLFitSession::Fit
Fit
Description
Perform the fit. Iterations will be carried out until the tolerance is reached, or the number of iterations has reached the maximum specified by SetMaxNumIter.
Syntax
bool Fit(int* pnOutcome = NULL, bool bCheckLastOutcome = false, bool bAllowCancel = false)
Parameters
- pnOutcome
- [output] the int pointer, if not NULL, receive the error code.
- bCheckLastOutcome
- [input] if ture, will not iteration again if already converged.
- bAllowCancel
- [input] if true to allow type Esc to interrupt iteration.
Return
return ture if fit converged, else return false.
Examples
EX1
- 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_Fit_ex1" in Command window.
#include <..\originlab\NLFitSession.h>
void NLFitSession_Fit_ex1()
{
Worksheet wks = Project.ActiveLayer();
if( !wks )
return;
wks.SetSize(-1, 2); // remove other columns
wks.SetSize(-1, 4); // add two new columns
wks.SetColDesignations("XYXY");
XYRange dr;
// add input data range
dr.Add(wks, 0, "X");
dr.Add(wks, 1, "Y");
dr.Add();
// add output fit data range
dr.Add(wks, 2, "X");
dr.Add(wks, 3, "Y");
int nRangeIndex = 0;
vector vX, vY;
dr.GetData(vY, vX, NULL, nRangeIndex);
NLFitSession FitSession;
// 1. set function
if( !FitSession.SetFunction("Gauss")) // set function as Gauss, category name can be ignore
{
out_str("invalid fit function");
return;
}
// 2. set data
if( !FitSession.SetData(vY, vX))
{
out_str("fail to set data");
return;
}
// 3. init parameter
if( !FitSession.ParamsInitValues())
{
out_str("fail to init parameter");
return;
}
out_str("The parameter values before fitting");
FitSession.GetChiSqr(); // call GetChiSqr order to set parameter settings on internal fit object
show_params(FitSession);
// 4. fit
int nOutcome;
if( !FitSession.Fit(&nOutcome) )
{
out_str("Fail to do fitting");
return;
}
out_str("The parameter values after fitting");
show_params(FitSession);
// 5. get fitting xy data and put to source worksheet
double xmin, xmax, xinc;
vX.GetMinMax(xmin, xmax);
int count = 100;
RoundLimits(&xmin, &xmax, &xinc, count);
vector vFitX, vFitY;
vFitX.Data(xmin, xmax, xinc);
vFitY.SetSize(vFitX.GetSize());
if( 0 == FitSession.GetYFromX(vFitX, vFitY, vFitX.GetSize()) )
{
out_str("Fail to get Y values");
}
nRangeIndex = 1;
dr.SetData(&vFitY, &vFitX, nRangeIndex);
// plot source data and fitting data
GraphPage gp;
gp.Create();
GraphLayer gl = gp.Layers();
if( gl.AddPlot(wks) >= 0 )
legend_update(gl);
}
void show_params(NLFitSession& FitSession)
{
// get parameter values after initialization
vector vParamValues;
vector<int> vnParamsOffsets; // the begin index of one group of parameters, normally one group for one dataset
FitSession.GetParamValuesAndOffsets(vParamValues, vnParamsOffsets);
// output parameter values with names
vector<string> vsParamNames;
FitSession.GetParamNamesInFunction(vsParamNames);
int nDataset = 0;
for( int nParam = vnParamsOffsets[nDataset], ii = 0; nParam < vnParamsOffsets[nDataset+1]; nParam++, ii++ )
{
printf("%s = %f\n", vsParamNames[ii], vParamValues[nParam]);
}
}
Remark
See Also
GetFitOutCome
header to Include
Originlab\NLFitSession.h
|