# 3.142 FAQ-858 How to build up a summation fitting function or a double integral fitting funtion?

Last Update: 10/12/2017

You can create your user-defined summation or double integral fitting function by LabTalk scripts or Origin C codes, in Tools: Fitting Function Organizer.

## summation fitting function

You can define the fitting function with an embedded For loop in the function definition box. Take the following function as an example. $y=\sum_{i=1}^{10}a \cdot x^2$

Suppose you define the fitting function in the Function box as follow:

y = sum(x, a, n); //n is a constant, and n=10

Then define the user-defined LabTalk functions sum in the LabTalk Functions Definition and Initializations box as follow:

function double sum(double x, double a, int n)
{
double bb = 0;
for(ii =1; ii<=n; ii++)
{
bb = a * x * x + bb;
}
return bb;
}

## double integral fitting function

You can define an Origin C fitting function in which NAG functions are called to perform the integration. Take the following function as an example. $y=\int_{amp}^x \int_{amp}^x (z_{1}+z_{2})dz_{1}dz_{2}$

1. Click the button next to the Function edit box to open the Code Builder.
2. Scroll up to go to line
//add the header file for the NAG functions here.

and add the header file for the NAG functions below this line:

#include <OC_nag.h>
static double NAG_CALL f(int n, double z[], Nag_User *comm)
{
int *use_comm = (int *)comm->p;
double dUserSuppliedFunction;
dUserSuppliedFunction = z + z[n-1];
return dUserSuppliedFunction;
}
void _nlsfnag_double_integral_fitting(
// Fit Parameter(s):
double amp,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
// Beginning of editable part
static int use_comm;
int ndim = 2;  // the integral dimension
int maxpts = 1000*2;  // maximum number of function evaluation
double a, b;

static NagError fail;
double finval;
int minpts;
double acc, eps;
Nag_User comm;
comm.p = (Pointer)&use_comm; // For communication with user-supplied functions

for (int k=0; k < ndim; ++k)  // integration interval
{
a[k] = amp;
b[k] = x;
}
eps = 0.0001; // set the precision
minpts = 0;

d01wcc(ndim, f, a, b, &minpts, maxpts, eps, &finval, &acc, &comm, &fail);

y = finval;
// End of editable part
}
5. Click Compile button to compile.

We provide several tutorials of user-defined integral functions. Refer to the corresponding topic for detailed steps if needed:

Keywords: integrate, integral, fitting, Origin C, LabTalk, NAG, summation, double integral