# 4.2.2.11 Fitting with NAG Special Function

## Summary

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:

$inorm= A* exp(-td/2.0/(t-t0)) * ( I0(td/2.0/(t-t0))+I1(td/2.0/(t-t0)) ) \,$

Here $A$, $td$ and $t0$ are the model parameters we want to obtain from the data fitting. $I0$ and $I1$ 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: FittingWithBessel Function Type: User-Defined Independent Variables: t Dependent Variables: inorm Parameter Names: A,t0,td Function Form: Origin C Function:

Click the button (icon) beside the Function box to open the code builder and define and compile and save the fitting function as follows:

#include <origin.h>

// 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.
#include <OC_nag8.h>

// 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.

//----------------------------------------------------------
//
void _nlsfFittingWithBessel(
// Fit Parameter(s):
double A, double t0, double td,
// Independent Variable(s):
double t,
// Dependent Variable(s):
double& inorm)
{
// 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) );
if(fail1.code !=NE_NOERROR)
printf("%s\n",fail1.message);
if(fail2.code !=NE_NOERROR)
printf("%s\n",fail2.message);

// 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.

## 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.

Sample Data Results
X Y
2 0.7868954118
2.080808081 0.8133022141
2.161616162 0.8178216765
2.242424242 0.8427866729
2.323232323 0.8315815363
2.404040404 0.8484657180
2.565656566 0.8618233553
2.646464646 0.8745962570
2.727272727 0.8921620316
2.808080808 0.8687399759