188.8.131.52 Fitting with NAG Special Function
Origin allows user to define an Origin C fitting function using NAG special functions. You can call NAG routine to evaluate the special function.
Minimum Origin Version Required: Origin 8.0 SR6
What you will learn
This tutorial will show you how to:
- Create fitting function using Fitting Function Organizer
- Create fitting function using NAG special function
Example and Steps
We will fit the following model:
Here , and are the model parameters we want to obtain from the data fitting. and are the first kind of Modified Bessel function of order 0 and order 1, respectively. For current example, we use the sample data in the end of this tutorial. The fitting procedure can be outlined into the following steps:
Press F9 to open the Fitting Function Organizer and then create a new Category named FittingWithNAGSpecialFunc. Define a new fitting function FittingWithBessel in the new category as follow:
| Function Name:
| Function Type:
| Independent Variables:
| Dependent Variables:
| Parameter Names:
| Function Form:
|| Origin C
Click the button (icon) beside the Function box to open the code builder and define and compile and save the fitting function as follows:
// Add your special include files here.
// For example, if you want to fit with functions from the NAG library,
// add the header file for the NAG functions here.
// Add code here for other Origin C functions that you want to define in this file,
// and access in your fitting function.
// You can access C functions defined in other files, if those files are loaded and compiled
// in your workspace, and the functions have been prototyped in a header file that you have
// included above.
// You can access NLSF object methods and properties directly in your function code.
// You should follow C-language syntax in defining your function.
// For instance, if your parameter name is P1, you cannot use p1 in your function code.
// When using fractions, remember that integer division such as 1/2 is equal to 0, and not 0.5
// Use 0.5 or 1/2.0 to get the correct value.
// For more information and examples, please refer to the "User-Defined Fitting Function"
// section of the Origin Help file.
// Fit Parameter(s):
double A, double t0, double td,
// Independent Variable(s):
// Dependent Variable(s):
// Beginning of editable part
//inorm= A* exp(-td/2.0/(t-t0)) * ( s18aec(td/2.0/(t-t0),NAGERR_DEFAULT)+s18afc(td/2.0/(t-t0),NAGERR_DEFAULT) );
static NagError fail1;
static NagError fail2;
double dtemp = td/2.0/(t-t0);
inorm= A* exp(-dtemp) * ( s18aec(dtemp,&fail1)+s18afc(dtemp,&fail2) );
// End of editable part
Simulate the Function
After the function body is defined, you can click the Compile button in Code Builder to check syntax errors. And then click Return to Dialog button to go back Fitting Function Organizer dialog box. Now click the Save button to generate the .FDF file (Function definition file).
Once you have a .FDF file, you can click the Simulate button to simulate a curve, this will be very helpful to evaluate the initial values. In the simcurve dialog, enter some proper parameter values and X range, and see what the curve looks like in the Preview panel.
Set the Initial Values for the Parameters
As it is a user-defined fitting function, you have to supply the initial guess values for the parameters before performing your fitting task for the data. You may do it by set them manually in the Parameter tab in Nonlinear Curve Fit dialog. For the sample data shown below, you can just set the initial values for the parameters A = 1, td = 1, t0 = 1. After the parameters are initialized, you can then do the fitting to obtain the fitting result, as shown to the right of the sample data.
Copy the below sample data and use Import Wizard to import the data from Clipboard, then do the fitting using the given initial values for the parameters: A = 1, td = 1, t0 = 1.