1.12.3.2 Polynomial Fitting

To perform a polynomial fitting routine in Origin C, you can use the ocmath_polynomial_fit function. With this function, you can do polynomial fitting with weight, and then you can get the fitting results, including parameter values, statistical information, etc.

The following procedure will show how to perform polynomial fitting in Origin C by using this function.

Perform Polynomial Fitting

Before doing polynomial fitting, please import the desired data, here need one independent and one dependent.

The procedure of performing polynomial fitting needs three steps.

1. Get the data from the worksheet for polynomial fit. Both independent and dependent are using vector variables.

Worksheet wks = Project.ActiveLayer();
if(!wks)
	return;  // invalid worksheet

DataRange dr;
dr.Add("X", wks, 0, 0, -1, 0);  // x column
dr.Add("Y", wks, 0, 1, -1, 1);  // y column

vector vX, vY;
dr.GetData(&vX, 0);  // get data of x column to vector
dr.GetData(&vY, 1);  // get data of y column to vector

2. Define the structure variables and other data types as parameters for passing to the function. It also can initialize some fitting settings.

// here just define the structure for output results
int nSize = vX.GetSize();
const int nOrder = 2;  // order

int nSizeFitParams = nOrder+1;
FitParameter psFitParameter[3];  // number of parameter = nOrder+1

RegStats psRegStats;  // regression statistics
RegANOVA psRegANOVA;  // anova statistics

3. Pass the desired arguments and perform polynomial fitting on the data.

// polynomial fitting, using the default options, 2 order
int nRet = ocmath_polynomial_fit(nSize, vX, vY, NULL, nOrder, NULL, psFitParameter, 
									nSizeFitParams, &psRegStats, &psRegANOVA);

// check error
if(nRet!=STATS_NO_ERROR)
{
	out_str("Error");
	return;
}

Output the Results

After finishing the calculation, the results may need to output to somewhere for presentation, such as Script Window, Result Log, Worksheet, etc.

Please refer to the Result to Output Window and Result to Worksheet section in the chapter Analysis and Applications: Curve Fitting: Linear Fitting for more details about how to output the results.