184.108.40.206 Parameters Initialization using LabTalk in NLFit
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:
, 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:
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.
Write Parameter Initialization Script for a User-Defined Fitting Function
- 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.
- 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.
- 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.
- 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)
- 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.
//Get the data worksheet
//Get the data worksheet index
//Make sure the data workbook is active
win -a %(ry.getpage()$);
//Make sure the data worksheet is active
//Active column 2 in the data worksheet
//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.
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
- Create a new Origin project by clicking the button on the Standard toolbar. Click the button (or Data: Import from File: Import Wizard or press Ctrl+3) to open the Import Wizard.
- 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.
- 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.
- 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.
- An attention box pops up and shows the experimental temperature and the calculated initial value of time constant.
- Go to the Parameters tab. The value of parameter T is 5106, which indicates the parameter initialization script is successfully called.
- Go to the Code tab and select the Parameter Init section, there you could double check the parameter initialization script by clicking the button.
- Click OK to close the attention box, and click Fit to carry out the fit.
Create an Analysis Template and Do Batch Processing
- 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.
- Keep the workbook active and select File:Save Workbook as Analysis Template to save this workbook as MyUptakeFit.ogw.
- Create a new Origin project and select File:Batch Processing to open the batchProcess dialog.
- 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:
- 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: