3.9.2.2.1 Fit on one data with one explicit function

Version Info

Minimum Origin Version Required: Origin 8 SR0

Examples

////////////////////////////////////////////////////////////////////////////////////////
// Explicit function fitting
// before running this function, import \Samples\Curve Fitting\Gaussian.dat to worksheet.
////////////////////////////////////////////////////////////////////////////////////////

#include <Origin.h>
#include <FDFTree.h>
#include <ONLSF.h>

void	NLFit_example1()
{
	string			strFDF = okutil_get_origin_path(ORIGIN_PATH_SYSTEM, "FitFunc") + "Gauss.FDF";
	Tree			trFF;
	if( !nlsf_FDF_to_tree( strFDF, &trFF ))
	{
		out_str("Fail to load FDF function file");
		return;
	}
	
	//////////////////////////////////////////////////////////////////////////////
	// 1. Setting the fitting function  //
	
	// The fit object:
	NLFit			fit;
	int				nn = fit.SetFunction(trFF);
	if (nn <= 0)
	{
		out_str("Failed setting fitting function!");
		return;
	}
	
	
	///////////////////////////////////////////////////////////////////////////
	// 2. Setting data for fitting ////////////////////////////////////////////
	Worksheet 	wks = Project.ActiveLayer();
	if( !wks )
	{
		out_str("No active worksheet to get input data");
		return;
	}
	
	DataRange 	dr;
	dr.Add(wks, 0, "X");
	dr.Add(wks, 1, "Y");
	
	DWORD 		dwRules = DRR_GET_DEPENDENT | DRR_NO_FACTORS;
	int 		nNumRanges = dr.GetNumData(dwRules);
	ASSERT( 1 == nNumRanges );
	if( nNumRanges <= 0 || nNumRanges > 1)
	{		
		out_str("Not proper input data to do fit");
		return;
	}
	
	vector		vX, vY;
	DWORD		dwPlotID;
	dr.GetData( dwRules, 0, &dwPlotID, NULL, &vY, &vX);
	
	NLSFONEDATA		stDep, stIndep;
	stIndep.pdData = vX;
	stIndep.nSize = vX.GetSize();
	stDep.pdData = vY;
	stDep.nSize = vY.GetSize();
	nn = fit.SetData(1, &stDep, &stIndep);
	if (nn != 0)
	{
		out_str("SetData() failed!");
		return;
	}	
	
	// 3. Setting paramaters //////////////////////////////////////////////////
	int				nNumParams = 4;	
	vector			vParams(nNumParams);	// this vector should be initialized to the total number of paramaters
	// This vector will be used both to set initial paramater values, and to receive the parameter values after fitting:

	NumericFunction fn;
	fn.SetTree(trFF);
	
	int nErr;
	if( !fn.ParamInit( vParams, vX, vY, nErr ) )// Run parameter init code to get parameter init values
	{
		vParams[0] = 5.58;					// y0
		vParams[1] = 26;					// xc
		vParams[2] = 8;						// w
		vParams[3] = 976;					// A
	}
	
	nn = fit.SetParams(nNumParams, vParams);
	if ( 0 != nn )
	{
		out_str("Invalid paramater setting!");
		return;
	}
	

	///////////////////////////////////////////////////////////////////////////
	// 4. Fitting /////////////////////////////////////////////////////////////
	int				nMaxNumIterations = 100;
	nn = fit.Fit(nMaxNumIterations);
	printf("Fit(%ld) returned: %ld\n", nMaxNumIterations, nn);
	if( 0 == nn )
		printf("Fit Done!\n");	
	
	///////////////////////////////////////////////////////////////////////////
	// 5. Dump all the results and compare with what was expected /////////////
	for (int iparam = 0; iparam < nNumParams; iparam++)
	{
		printf("param index = %d   value obtained = %lf\n", iparam + 1, vParams[iparam]);
	}
	
	return;	
}