3.9.2.3 Fitting with Operation

Simplest Case of Fitting with Operation

This example shows how to do nonlinear curve fit on one XY data with Gauss function. Without dialog open and with default settings to do fit and genereate worksheet report including parameter, statistics report tables and fitted curves report graphs.

#include <operation.h>
void NLFit_op_ex()
{
    bool bInit;
    Operation& op = (Operation&)op_create("FitNL", bInit);  // the operation name of NLFit is FitNL
    if( !bInit )
    {
        out_str("Error in op create");
        return;
    }
    
    Tree trOp;
    op.GetTree(trOp);   
 
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
        return; 
    DataRange dr;
    dr.Add(wks, 0, "X");
    dr.Add(wks, 1, "Y");
    
    // init trOp.GUI.InputData treenodes from dr and do SetData, SetFunction actions
    string 	strFunction = "Gauss";
    string	strCategory = "Peak Functions";
    if (OP_NOERROR != op.OnInitDataFromOCLT(trOp, trOp.GUI.InputData, dr, strFunction, strCategory) )
    {
        out_str("Fail to init data from data range");
        return;
    }
    op.SetTree(trOp);// set the changes of trOp back to op object
    
    int nAutoUpdate = AU_AUTO;
    op.OnNoEdit(0, nAutoUpdate);
    
    op.Execute(); // do fit and genereate report
    
}

Copy the following commands in Command window and press Enter to run:

newbook;
string ascfile$ = System.path.program$ + "samples\Curve Fitting\Gaussian.dat";
ImpASC fname:= ascfile$;
NLFit_op_ex;

Fit on Multipe Dataset with Parameter Shared

The following example shows how to fit on two XY datasets with Gauss function, parameter xc is shared and Y0 of the first dataset is fixed as 5.

#include <operation.h>
void NLFit_op_with_Multi_Datasets()
{
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
    {
        out_str("Please activate a worksheet with Gaussian.dat data");
        return; 
    }
 
    DataRange dr;
    dr.Add(wks, 0, "X");
    dr.Add(wks, 1, "Y");
    dr.Add();
    dr.Add(wks, 0, "X");
    dr.Add(wks, 2, "Y");
 
    bool bInit;
    Operation& op = (Operation&)op_create("FitNL", bInit);  // the operation name of NLFit is FitNL
    if( !bInit )
    {
        out_str("Error in op create");
        return;
    }
 
    Tree trOp;
    op.GetTree(trOp); 
 
    // init trOp.GUI.InputData treenodes from dr and do SetData, SetFunction actions
    string  strFunction = "Gauss";
    string  strCategory = "Peak Functions";
    if (OP_NOERROR != op.OnInitDataFromOCLT(trOp, trOp.GUI.InputData, dr, strFunction, strCategory) )
    {
        out_str("Fail to init data from data range");
        return;
    }    
 
    // set share
    trOp.GUI.InputData.Use = DATA_MODE_GLOBAL; // if want to share parameters, data mode should be Global Fit
    trOp.GUI.Parameters.Share.Value1.nVal = 0; // not share y0
    trOp.GUI.Parameters.Share.Value2.nVal = 1; // share xc
 
    // set fix
    trOp.GUI.Parameters.Fixed.Value1.nVal = 1; // fix y0
    trOp.GUI.Parameters.FixedValues.Value1.dVal = 5; // fixed value
 
    // set the changes of trOp back to op object
    op.SetTree(trOp);    
 
    int nAutoUpdate = AU_AUTO;
    op.OnNoEdit(0, nAutoUpdate);  
 
    // do fit and genereate report
    op.Execute(); 
    
    // to get the newest operation tree including fit result.
    op.GetTree(trOp);
 
    // output parameter values
    int nNumData = 2;
    vector<string> vsParamNames = {"y0", "xc", "w", "A"};    
    TreeNode trParams = trOp.Calculation.Parameters;
    
    for(int nData = 0; nData < nNumData; nData++)
    {
    	printf("%d:\t", nData+1);
    	
	    for(int nn = 0; nn < vsParamNames.GetSize(); nn++)
	    {
	    	string strParamName = vsParamNames[nn];
	    	TreeNodeCollection trNs(trParams, strParamName+"_");
	    	if( trNs.Count() > nData )
	    	{
		    	TreeNode trParam;
		    	trParam = trNs.Item(nData);
		    	
		    	if( trParam && trParam.Value )
			    	printf("%s=%g, ", strParamName, trParam.Value.dVal );
	    	}
	    	
	    }
	    
	    printf("\n");
    }
}

Copy the following commands in Command window and press Enter to run:

newbook;
string ascfile$ = System.path.program$ + "samples\Curve Fitting\Gaussian.dat";
ImpASC fname:= ascfile$;
NLFit_op_with_Multi_Datasets;

Fit Multiple Peaks Data

The following example shows how to fit on the multiple peaks data.

#include <operation.h>
void NLFit_op_with_Multi_Peaks()
{
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
    {
    	out_str("Please activate a worksheet with Multiple Peaks.dat data");
    	return; 
    }
        
    DataRange dr;
    dr.Add(wks, 0, "X");
    dr.Add(wks, 1, "Y");
    
    bool bInit;
    Operation& op = (Operation&)op_create("FitNL", bInit);  // the operation name of NLFit is FitNL
    if( !bInit )
    {
        out_str("Error in op create");
        return;
    }
    
    Tree trOp;
    op.GetTree(trOp); 
    
    // set replica number to 2. Default is 0. 
	trOp.GUI.Replica.Number.nVal = 2;  

    // init trOp.GUI.InputData treenodes from dr and do SetData, SetFunction actions
    string 	strFunction = "Gauss";
    string	strCategory = "Peak Functions";
    if (OP_NOERROR != op.OnInitDataFromOCLT(trOp, trOp.GUI.InputData, dr, strFunction, strCategory) )
    {
        out_str("Fail to init data from data range");
        return;
    }        

    // set the changes of trOp back to op object
    op.SetTree(trOp);    
    
    int nAutoUpdate = AU_AUTO;
    op.OnNoEdit(0, nAutoUpdate);  
    
    // do fit and genereate report
	op.Execute(); 
  
}

Copy the following commands in Command window and press Enter to run:

newbook;
string ascfile$ = System.path.program$ + "samples\Curve Fitting\Multiple Peaks.dat";
ImpASC fname:= ascfile$;
NLFit_op_with_Multi_Peaks;

Fit and Output Find Y from X table

The following example shows how to output Find Y from X table.

#include <operation.h>

void NLFit_op_with_YfromX(bool bYfromX = true)
{
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
    {
        out_str("Please activate a worksheet with Gaussian.dat data");
        return; 
    }

    DataRange dr;
    dr.Add(wks, 0, "X");
    dr.Add(wks, 1, "Y");

    bool bInit;
    Operation& op = (Operation&)op_create("FitNL", bInit);  // the operation name of NLFit is FitNL
    if( !bInit )
    {
        out_str("Error in op create");
        return;
    }
 
    Tree trOp;
    op.GetTree(trOp); 
 
    // init trOp.GUI.InputData treenodes from dr and do SetData, SetFunction actions
    string  strFunction = "Gauss";
    string  strCategory = "Peak Functions";
    if (OP_NOERROR != op.OnInitDataFromOCLT(trOp, trOp.GUI.InputData, dr, strFunction, strCategory) )
    {
        out_str("Fail to init data from data range");
        return;
    } 
    
    //enable Find Y from X and Calculate Confidence
    TreeNode trCalibration, trCustomSettings;
	trCalibration = op_get_calibration_table(trOp.GUI);
	if ( trCalibration )
	{
		int nIndexTbl = bYfromX ? 2 : 1;
		string strCustomNode = "Custom" + (nIndexTbl + 1);
		trCustomSettings = trCalibration.GetNode(strCustomNode);
	}
	if ( trCustomSettings )
	{
		trCustomSettings.Conf.nVal = 1;
		trCustomSettings.Conf.Enable = true;
		trCustomSettings.Use = 1;
	}
	else
	{
		out_str("Fail to enable Find Y from X");
        return;
	}
	
	// set the changes of trOp back to op object
    op.SetTree(trOp);    

    int nAutoUpdate = AU_AUTO;
    op.OnNoEdit(0, nAutoUpdate);  
    
    // do fit and genereate report
    op.Execute(); 
}

Copy the following commands in Command window and press Enter to run:

newbook;
string ascfile$ = System.path.program$ + "samples\Curve Fitting\Gaussian.dat";
ImpASC fname:= ascfile$;
NLFit_op_with_YfromX;