3.9.2.1.8 Fit with multiple independent variables function

Version Info

Minimum Origin Version Required: Origin 8.5 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:

run.LoadOC(Originlab\nlsf_utils.c);

Examples

  1. New a worksheet and import \Samples\Matrix Conversion and Gridding\XYZ Random Gaussian.dat. Add a new column and fill it with uniform random numbers as weight data.
  2. Copy the following example codes to a c file and compile.
  3. Run test_fit_with_multi_indep_func in Command Window. The fitting results will be output to Command Window.
#include <..\OriginLab\NLFitSession.h>

void test_fit_with_multi_indep_func()
{
	Worksheet wks = Project.ActiveLayer();
	if(!wks)
	{
		out_str("Not a valid worksheet!");
		return; // need to activate a worksheet with data
	}
 
	NLFitSession FitSession;
	if(!FitSession.SetFunction("Gauss2D"))
	{
		out_str("Set fitting function error!");
		return;
	}	
	vector<string> vsParamNames;
	int nNumParamsInFunction = FitSession.GetParamNamesInFunction(vsParamNames);
	
	// get data range
	DataRange drInputData;
	drInputData.Add(wks, 0, "X");  // x column
	drInputData.Add(wks, 1, "X");  // y column
	drInputData.Add(wks, 2, "Y");  // z column
	drInputData.Add(wks, 3, "W");  // optional, weight column
	
	// 2. set the dataset with tree	
	Tree trInputData;
	TreeNode trRange = trInputData.AddNode("Range1");
	drInputData.GetTree(trRange);
	
	bool bRet = FitSession.SetData(trInputData);

	// 3. Set parameter init values	
	 // Way 1. to init parameter by running parameter initial code.
    if(!FitSession.ParamsInitValues())
	{
		out_str("Init values error!");
		return;
	}	
	
	// Way 2. to init parameter values one by one	
	/*
    vector vParams(nNumParamsInFunction);
    vParams[0] = 2; // z0
    vParams[1] = 9.45; // A
    vParams[2] = 15; // xc
    vParams[3] = 3.4; // w1
    vParams[4] = 14.7; // yc
    vParams[5] = 3.31; // w2
    int nErr = FitSession.SetParamValues(vParams);
    
    if(nErr != 0)
    {
    	printf("Fail to set init parameters: err= %d.", nErr);
    	return;
    }   
    */

    // set weight method
	if(!FitSession.SetWeightData(WEIGHT_INSTRUMENTAL, 0.0, 0.0, 0.0))
	{
		out_str("Set weight method error!");
		return;
	}

	// fit
	int nFitOutcome;
	if(!FitSession.Fit(&nFitOutcome))
	{
		string strOutcome = FitSession.GetFitOutCome(nFitOutcome);
		out_str("Fit error! "+strOutcome);
		return;
	}
	
	// get fit results
	RegStats fitStats;
	NLSFFitInfo fitInfo;
	vector vParamValues, vErrors;
	FitSession.GetFitResultsStats(&fitStats, &fitInfo, false, 0);
	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, %s_error = %f\n", vsParamNames[nParam], vParamValues[nParam], vsParamNames[nParam], vErrors[nParam]);
	}
}