# Use Initial Formula to Better Guess Fitting Parameter Values from Data

Good parameter initialization results in fast and reliable model/data convergence. Before Origin 2017, you can write lengthy LabTalk functions or Origin C functions to intialize parameter values based on your data but it is still not easy for new users.

Origin 2017 added the following ways to initialize parameter values without the need to learn coding.

• Use Data Information, such as XMin, XMax, YMin, YMax, XatY50, Peak, Center, etc.
• Use metadata, such as column long name, comments, etc. and values extracted during import

You can also write simple math expressions with the above values.

This blog shows a simple example of extracting metadata during import and then using it to initialize fitting parameters. Then an analysis template is setup to batch fit multiple such data files.

Step 1. Extract Temperature Value during Import

1. Open Origin and start a new worksheet.  Click Import wizard button (or File:Import:Import Wizard or press Ctrl+3) to open the Import Wizard. In the Data Source, select the file \sample\Curve Fitting\UptakeCurve_343.txt.  In Import Mode, select Replace Existing Data. Click Next.
2. In the Header Lines page,  set Long Names, Units, Comments as 3, 4, and 1 to 1, respectively. Click Next.
3. In the Variable Extraction page, Check Specify location of variable names and values using character positions and Add variable to page info. Click Next.
4. In the Variable Extraction by Position page, Under Variable, select H001 right to Line and set Start and End to 13 and 15 to extract the temperature value from the header line.  Click the radio button Extract variable name from file, select H001 besides line, set Start and End to 1 and 12 to create the user parameter with name Temperature.  Click Add to create it. Then click Next.
5. Keep Clicking Next until the last page. Check Save filter checkbox. Select In the Window. Click Finish to import the data.
6. In the worksheet, A user parameter line with name Temperature is added. In the next step, we are going to  generate a fitting function with parameters initialized by the Temperature value.

Step 2. Define a Fitting Function with Parameter Initialized with Temperature Value

1. Select Tools:Fitting Function Builder (or press F8) to open the Fitting Function Builder, in the Goal page, select Create a New Function and click Next.
2. In the Name and Type page, make sure the function is created under the User Defined category, name the function UptakeCurveFit, keep the Function Model as Explicit, and select Origin C for Function Type. Click the Next button.
3. In the Variables and Parameters page, keep the Independent Variables and Dependent Variables as the default x and y, respectively. Set the Parameters to T and click Next.
4. In the Origin C Fitting Function page, type in the following equation in the Function Body edit box, and then click Next.
`y = 1 - 6/(pi^2)*exp(-(pi^2/T)*x)`
5. In the Parameter Initialization Code page, select Use Variable or Metadata. In the table, click the triangular button right to Param T and then go to Metadata from Columns:User-defined Parameters:X D1. Thus the Temperature value in the worksheet user parameter is assigned to XD1, Edit the initial Formula to 25000-58*XD1.  Click Finish.

Step 3. Fit the Data with the User Defined Function

1. Highlight column B and select Analysis:Fitting:Nonlinear Curve Fit (or press Ctrl+Y) to open the NLFit dialog, select User Defined for Category and UptakeCurveFit(User) as Function.
2. Go to the Parameters tab, see that the initial parameter T is set to 5106. The Initial Formula check box is turned on and is set to 25000-58*XD1. This indicates the parameter initialization is successfully applied. Click Fit to carry out the fit. Note: in this dialog, you can also update the initial formula and click P button again to reinitialize parameter.

Step 4. Create an Analysis Template and Do Batch Processing

1. Go to the report sheet FitNL1 which is generated from previous steps, click the triangle button right to the Summary table and choose Create Copy as New Sheet. A new sheet will be created, delete column A in the new sheet and rename it as Result.

2. Keep the workbook active and select File:Save Workbook as Analysis Template to save this workbook as MyUptakeFit.ogw.
3. Create a new Origin project and select File:Batch Processing to open the batchProcess dialog.
4. Load the analysis template MyUptakeFit.ogw, select the files UptakeCurve_343K.txt, UptakeCurve_373K.txt, and UptakeCurve_403K.txt under the <Origin Folder>\Samples\Curve Fitting\, choose File Name for Dataset Identifier and make sure the other settings are the same according to the image below:
5. Click OK to run the batch processing. For each run, the temperature value in the header line is first extracted during importing and then used to initialize the parameter T for the curve fitting. The final analysis result is shown below:

To see another example of initialize parameter values with min, max, mean, etc of data itself, you can unzip this file (cdf_fitting) which contains a CDF (Cumulative distribution function) function of a normal distribution. Along with it is a .ogw file with the data to be fitted. Go to the Parameter tab of the curve fitting dialog. All four fitting parameters are initialized using the data info as shown below:

View all posts by Yiming Chen →