NLFitSession::SetParamValues
SetParamValues
Description
Set all the paramter values. Before set parameter values need to set function and data firstly.
Syntax
int SetParamValues(const vector& vParams)
Parameters
- vParams
- [input] contains all the parameters needed for the fit function.
Return
Return 0 if set successfully, -2 if fit function not ready, 1 if too few parameters, -1 if too many parameters.
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_SetParamValues_ex1" in Command window.
#include <..\originlab\NLFitSession.h>
bool NLFitSession_SetParamValues_ex1()
{
Worksheet wks = Project.ActiveLayer();
if( !wks )
return false;
XYRange dr;
dr.Add(wks, 0, "X");
dr.Add(wks, 1, "Y");
dr.Add();
dr.Add(wks, 0, "X");
dr.Add(wks, 2, "Y");
int nNumDataset = dr.GetNumData(DRR_GET_DEPENDENT);
vector vX1, vY1, vX2, vY2;
dr.GetData(vY1, vX1, NULL, 0);
dr.GetData(vY2, vX2, NULL, 1);
NLFitSession FitSession;
// Set function
if( !FitSession.SetFunction("Gauss")) // set function as Gauss, category name can be ignore
{
out_str("invalid fit function");
return false;
}
// Set first data
if( !FitSession.SetData(vY1, vX1, NULL, 0, nNumDataset))
{
out_str("fail to set data");
return false;
}
// Set second data
if( !FitSession.SetData(vY2, vX2, NULL, 1, nNumDataset, DATA_MODE_GLOBAL))
{
out_str("fail to set data");
return false;
}
// Set parameters
vector vParams(8);
// the parameters of the first dataset
vParams[0] = 5.58; // y0
vParams[1] = 26; // xc
vParams[2] = 8.66; // w
vParams[3] = 976; // A
// the parameters of the second dataset
vParams[4] = 2.29; // y0
vParams[5] = 26; // xc
vParams[6] = 10.32; // w
vParams[7] = 102.98; // A
if( 0 != FitSession.SetParamValues( vParams ) ) // return 0 if success
{
out_str("fail to set parameters");
return false;
}
FitSession.GetChiSqr(); // call GetChiSqr order to set parameter settings on internal fit object
out_str("The parameter values before fitting");
show_params(FitSession, nNumDataset);
// Fit
int nOutcome;
if( !FitSession.Fit(&nOutcome) )
{
out_str("Fail to do fitting");
return false;
}
out_str("The parameter values after fitting");
show_params(FitSession, nNumDataset);
// Calculate fitting Y
vector vFitY1(vX1.GetSize());
if( 0 == FitSession.GetYFromX(vX1, vFitY1, vFitY1.GetSize(), 0) ) // get fitting y for first dataset
{
out_str("Fail to get Y values");
return false;
}
vector vFitY2(vX2.GetSize());
if( 0 == FitSession.GetYFromX(vX2, vFitY2, vFitY2.GetSize(), 1) ) // get fitting y for second dataset
{
out_str("Fail to get Y values");
return false;
}
// Add two columns to put fit data
XYRange drFit;
drFit.Add( wks, 0, "X" );
drFit.Add( wks, wks.AddCol(), "Y");
drFit.Add();
drFit.Add( wks, 0, "X" );
drFit.Add( wks, wks.AddCol(), "Y");
drFit.SetData(&vFitY1, &vX1, 0);
drFit.SetData(&vFitY2, &vX2, 1);
return true;
}
void show_params(NLFitSession& FitSession, int nNumDataset)
{
// get parameter values
vector vParamValues, vErrors;
FitSession.GetFitResultsParams(vParamValues, vErrors);
// get parameter names
vector<string> vsParamNames;
FitSession.GetParamNamesInFunction(vsParamNames);
int nNumParamsInFunction = vsParamNames.GetSize();
// output parameter values with names
for(int nParam = 0; nParam < vParamValues.GetSize(); nParam++)
{
int nDataset = nParam / nNumParamsInFunction;
printf("Dataset %d\t%s = %f\n", nDataset + 1, vsParamNames[nParam % nNumParamsInFunction], vParamValues[nParam]);
}
}
Remark
See Also
ParamsInitValues
header to Include
Originlab\NLFitSession.h
|