3.9.2.1.4 Fit User Defined Function

Version Info

Minimum Origin Version Required: Origin 8.1 SR0

Need to do before Running Examples

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 following command or just add this file to your workspace.

run.LoadOC(Originlab\nlsf_utils.c);

Examples

The following code shows how to use NLFitSesssion to fit on user define fitting function.

If there is a big loop in your fit function body, please close Code Builder to improve fitting speed.

  1. Add a user defined function according to Add User Defined Integral Fitting Function
  2. New a worksheet and import \Samples\Curve Fitting\Replicate Response Data.dat. Set Col(A) = log(Col(A)) in the Set Column Values dialog.
  3. Copy and compile the following codes. Close Code Builder and then run "NLFitSession_UserDefinedFuncFit" in Command Window.
#include <..\originlab\NLFitSession.h>

BOOL NLFitSession_UserDefinedFuncFit(int nMaxIter = 30, string strFunc = "nag_integration_fitting", int nXCol=0, int nYCol=1)
{
    Worksheet wks = Project.ActiveLayer();
    if(!wks)
        return false;
    
    NLFitSession    FitSession;
    
    // 1. Set fucntion
    if(!FitSession.SetFunction(strFunc, NULL)) // set function, category name can be ignore
        return error_report("invalid fit function");
    
    vector<string>  vsParamNames;
    int             nNumParamsInFunction = FitSession.GetParamNamesInFunction(vsParamNames);
    
    DataRange   	drInputData;
    drInputData.Add(wks, nXCol, "X");
    drInputData.Add(wks, nYCol, "Y");
    
    DWORD           dwRules = DRR_GET_DEPENDENT | DRR_NO_FACTORS;
    int         	nNumData = drInputData.GetNumData(dwRules);
    ASSERT( 1==nNumData );
        
    // 2. Set the dataset
	int             nDataIndex = 0; // only one set in our case
    vector  		vX, vY;
    drInputData.GetData( dwRules, nDataIndex, NULL, NULL, &vY, &vX );    
    
	if( !FitSession.SetData( vY, vX, NULL, nDataIndex, nNumData ) )  
        return error_report("err setting data");  
        
    // 3. Set parameter init values
    vector vParams(nNumParamsInFunction);
    vParams[0] = 1;	// y0
    vParams[1] = 1; // A
    vParams[2] = 1; // xc
    vParams[3] = 1; // w
    int nErr = FitSession.SetParamValues(vParams);
    if(nErr != 0)
    	return error_report("Fail to set init parameters: err=" + nErr);

    // 4. Iterate with default settings
    FitSession.SetMaxNumIter(nMaxIter);   
    
    int             nFitOutcome;
    if(!FitSession.Fit(&nFitOutcome))
    {
        string strOutcome = FitSession.GetFitOutCome(nFitOutcome);
        printf("fit failed:%d->%s\n", nFitOutcome, strOutcome);
        return false;
    }
    
    // 5. Success, get results
    RegStats        fitStats;
    NLSFFitInfo     fitInfo;
 	vector 			vParamValues, vErrors;
    FitSession.GetFitResultsStats(&fitStats, &fitInfo, false, nDataIndex);
    FitSession.GetFitResultsParams(vParamValues, vErrors);
	printf("# Iterations=%d, Reduced Chisqr=%g\n", fitInfo.Iterations, fitStats.ReducedChiSq);
    
	for( int nParam = 0; nParam < vParamValues.GetSize(); nParam++)
    {
        printf("%s = %f\n", vsParamNames[nParam], vParamValues[nParam]);
    }   
    return true;
}