4.2.2.5 Parameters Initialization using LabTalk in NLFit


Summary

In previous Origin versions, one can only initialize fitting parameters with Origin C code. Since Origin 9.0 SR1, it is also supported to use LabTalk script to initialize parameters. This implement is especially useful when one wants to use worksheet values for the initial parameters.

In this tutorial, three adsorption uptake curves at three different temperatures were measured, and the results are exported in three .txt files. The experimental conditions are stored in the same .txt file as header info. We are going to fit the data to the isothermal-spherical model, according to the following equation:

y = 1 - \frac{6}{pi^2}*exp(-\frac{pi^2}{T}*x)

, where y is the normalized mass uptake (mg/g), x is the time span (s), T is the time constant (1/s), and the fitting parameter.

There is an empirical equation describing the relationship between the temperature and time constant, which is:

T = 25000 - 58 * Temp

For the uptake curve at each temperature, we will calculate T from this equation and use it as the initial value for the curve fitting.

Minimum Origin Version Required: Origin 9.0 SR1

What you will learn

This tutorial will show you how to:

  • Create a user-defined fitting function and use Labtalk script to do parameter initialization.
  • Use values in worksheet for initialized parameters.

Steps

Write Parameter Initialization Script for a User-Defined Fitting Function

  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)

Tutorial LT Para Init 01.png

  1. In the Parameter Initialization Code page, select Use Custom Code radio butto, and then select the Use Labtalk radio button to enable the use of Labtalk script. In the Initialization Code edit box, type in the following script:
    //Code to be executed to initialize parameters
    //Get the current worksheet page.
    range rpage=ry.getpage()$;  
    //Get the data worksheet
    range rlayer=ry.getlayer()$; 
    //Get the data worksheet index
    int inext=rlayer.index;
    //Make sure the data workbook is active
    win -a %(ry.getpage()$);
    //Make sure the data worksheet is active
    page.active=inext;
    //Active column 2 in the data worksheet
    wks.col=2;
    //Get the temperature as a string from column comment
    string str1$ = wks.col.comment$;
    //Get the string of the temperature number
    string str2$ = str1.Left(3)$;
    //Substitute the string variable to a double number
    double Temp = %(str2$); 
    //Use the empirical equation to calculate the initial value of T
    T=25000 - 58 * Temp; 
    //Check whether parameter initialization script runs successfully
    type -b "Experimental temperature is $(Temp) K, so the initial value of T is $(T)."
    Notes: The ry in the script above is an auto-defined range variable for the input data range of parameter y, the syntax for such range variable is r+parameter name. For example, if the parameter name is Temp, the range variable should be rTemp.
  2. Click Finish to create the user defined fitting function UptakeCurveFit, you could find its .fdf file in the User Files Folder.

Fit Data with the User Defined Fitting Function

  1. Create a new Origin project by clicking the Button New Project.png button on the Standard toolbar. Click the Button Import Wizard.png button (or Data: Import from File: Import Wizard or press Ctrl+3) to open the Import Wizard.
  2. In the Data Source, select the UptakeCurve_343K.txt as the File, which is located under path <Origin Folder>\Samples\Curve Fitting\. Click Next button to navigate to the Header Lines page. Set the Long Names, Units, and Comments as 3, 4, and 1 to 1, respectively.
    Tutorial LT Para Init 02.png
  3. Click Finish to import the data file. Note that the experimental temperature is stored as the comment of column 2, which will be retrieved by the Labtalk script for parameter initialization in the UptakeCurveFit fitting function.
    Tutorial LT Para Init 03.png
  4. 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.
  5. An attention box pops up and shows the experimental temperature and the calculated initial value of time constant.
    Tutorial LT Para Int 05.png
  6. Go to the Parameters tab, see that the initial parameter for T is 5106, which is calculated from the experimental temperature. This indicates the parameter initialization script is successfully called.
  7. Go to the Code tab and select the Parameter Init section, there you could double check the parameter initialization script by clicking the Button Initialize Parameters.png button.
  8. Click OK to close the attention box, and click Fit to carry out the fit.

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.
    Tutorial LT Para Init 06.png
  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\ path, choose File Name for Dataset Identifier and make sure the other settings are the same according to the image below:
    Tutorial LT Para Init 07.png
  5. Click OK to carry out the batch processing, for every file being executed, the attention box pops up and indicates the initial value being used, click OK every time to close the attention box and continue the batch processing. Finally, a summary report will be created:
    Tutorial LT Para Init 08.png