# 4.2.2.28 Parameter Initialization for Rational Functions

## Summary

In this tutorial, we will show you how to calculate initial parameters for rational fitting functions using the multiple linear regression method, and perform a fit of the data using calculated initial parameters.

Minimum Origin Version Required: Origin 9.0 SR0

## What you will learn

This tutorial will show you how to:

• Calculate initial parameters for rational fitting functions.
• Perform multiple linear regression using Origin C code.

## Example and Steps

### Algorithm

In this tutorial, we will use the following rational function as an example:

$y=\frac{a+bx+cx^2}{1+dx+ex^2}$

where x is the independent variable, y is the dependent variable, and a, b, c, d, e are fitting parameters.

Multiplying both sides by the denominator on the right side yields:

$y+dxy+ex^2y\;=\;a+bx+cx^2$

and the equation can be expressed as:

$y\;=\;a+bx+cx^2-dxy-ex^2y$

Substituting fitting data $(x_i, y_i) \;\; i=1 \ldots N$ into the equation gives:

$\begin{cases} a+bx_1+cx_1^2-dx_1y_1-ex_1^2y_1=y_1 \\ a+bx_2+cx_2^2-dx_2y_2-ex_2^2y_2=y_2 \\ \vdots \\ a+bx_N+cx_N^2-dx_Ny_N-ex_N^2y_N=y_N \\ \end{cases}$

Hence estimating initial parameters for a rational polynomial fitting function becomes a multiple linear regression problem with linear coefficients a, b, c, d, e.

$\begin{bmatrix} 1 & x_1 & x_1^2 & -x_1y_1 & -x_1^2y_1 \\ 1 & x_2 & x_2^2 & -x_2y_2 & -x_2^2y_2 \\ \vdots \\ 1 & x_N & x_N^2 & -x_Ny_N & -x_N^2y_N \\ \end{bmatrix} \begin{bmatrix} a \\ b \\ c \\ d \\ e \\ \end{bmatrix}= \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_N \\ \end{bmatrix}$

Origin provides a function ocmath_multiple_linear_regression in Origin C for multiple linear regression, which can be called in initialization code.

### Import Data

1. Open a new workbook.
2. Copy data in Sample Data to the workbook.
3. Highlight column B, and select Plot: Symbol: Scatter from Origin menu. The graph should look like:

### Define Fitting Function and Initialize Parameters

The fitting function can be defined using the Fitting Function Builder tool.

1. Select Tools: Fitting Function Builder from Origin menu.
2. In the Fitting Function Builder dialog's Goal page, click the Next button.
3. In the Name and Type page, select User Defined from the Select or create a Category drop-down list, type rationalfunc in the Function Name field, and select Expression in the Function Type group. Click the Next button.
4. In the Variables and Parameters page, type a, b, c, d, e in the Parameters field. Click the Next button.
5. In the Expression Function page, type the following script in the Function Body box.
(a+b*x+c*x^2)/(1+d*x+e*x^2)

Click the Evaluate button, and it shows y=1 at x=1, hence this implies the expression is correct. Click the Next button.

6. In the Parameter Initialization Code page, select Use Custom Code radio butto, click the Open Code Builder button to the right of the Initialization Code box, and initialize fitting parameters as follows, in terms of the algorithm.
  UINT nOSizeN = x_data.GetSize(); //Number of points
UINT nVSizeM = 5; //Number of parameters

matrix mX(nOSizeN, 5);

//Construct matrix for data points of independent variables
vector vCa(nOSizeN), vCb, vCc, vCd, vCe;
vCa = 1;
mX.SetColumn( vCa, 0 );
vCb = x_data;
mX.SetColumn( vCb, 1 );
vCc = x_data^2;
mX.SetColumn( vCc, 2 );
vCd = -x_data*y_data;
mX.SetColumn( vCd, 3 );
vCe = -x_data^2*y_data;
mX.SetColumn( vCe, 4 );

//Options for multiple linear regression
LROptions stLROptions;
stLROptions.UseReducedChiSq = 1;
stLROptions.FixIntercept = 1; //Fix the intercept at 0.

FitParameter stFitParameters[ 6 ]; // should be nVSizeM+1
UINT nFitSize = nVSizeM + 1;

int nRet = ocmath_multiple_linear_regression(mX, nOSizeN, nVSizeM, y_data,
NULL, 0, &stLROptions, stFitParameters, nFitSize );

if( nRet == STATS_NO_ERROR )
{
a = stFitParameters[1].Value;
b = stFitParameters[2].Value;
c = stFitParameters[3].Value;
d = stFitParameters[4].Value;
e = stFitParameters[5].Value;
}

Click the Compile button to compile the code. And click the Return to Dialog button. Click Finish to close the Fitting Function Builder dialog.

### Fit the Curve

1. Select Analysis: Fitting: Nonlinear Curve Fit from Origin menu. In the NLFit dialog, select Settings: Function Selection, in the page select User Defined from the Category drop-down list and rationalfunc function from the Function drop-down list.
2. Click the Parameters tab. Initial parameters calculated from initialization code are listed in the dialog, and the fitting function curve for initial parameters is shown as follows. It seems that initial parameters from initialization code are very good.
3. Click the Fit button to fit the curve.

### Fitting Results

The fitted curve should look like:

Fitted Parameters are shown as follows:

Parameter Value Standard Error
a 3.17139 0.30284
b -1.65602 1.76748
c 0.26407 1.81764
d 3.6884 0.26362
e 5.31812 0.55265

### Sample Data

x y
-1.5 1.13173
-1.39474 0.8262
-1.28947 1.06999
-1.18421 1.37155
-1.07895 0.79569
-0.97368 2.11346
-0.86842 2.32006
-0.76316 3.9205
-0.65789 5.81904
-0.55263 7.38037
-0.44737 8.31272
-0.34211 11.39718
-0.23684 8.39808
-0.13158 4.7305
-0.02632 4.11105
0.07895 2.39105
0.18421 1.65394
0.28947 0.42953
0.39474 0.83337
0.5 1.18758
 Note: You can also use this method to initialize parameters for other rational polynomial fitting functions.