# 2.13.3.2 Non-linear Fitting

Non-linear fitting in LabTalk is X-function based and proceeds in three steps, each calling (at least) one X-function:

1. nlbegin: Begin the fitting process. Define input data, type of fitting function, and input parameters.
2. nlfit: Perform the fit calculations
3. nlend Choose which parameters to output and in what format

Besides nlbegin, you can also start a fitting process according to your fitting model or data by the following X-Functions:

• nlbeginr: Fitting multiple dependnet/independent variables' model
• nlbeginm: Fitting a matrix
• nlbeginz: Fitting XYZ worksheet data

## Script Example

Here is a script example of the steps outlined above:

// Begin non-linear fitting, taking input data from Column 1 (X) and
// Column 2 (Y) of the active worksheet,
// specifying the fitting function as Gaussian,
// and creating the input parameter tree named ParamTree:
nlbegin iy:=(1,2) func:=gauss nltree:=ParamTree;
// Optional: let the peak center be fixed at X = 5
ParamTree.xc = 5;     // Assign the peak center an X-value of 5.
ParamTree.f_xc = 1;   // Fix the peak center (f_xc = 0 is unfixed).
// Perform the fit calculations:
nlfit;
// Optional: report results to the Script Window.
type Baseline y0 is $(ParamTree.y0),; type Peak Center is$(ParamTree.xc), and;
type Peak width (FWHM) is \$(ParamTree.w);
// end the fitting session without a Report Sheet
nlend;

## Notes on the Parameter Tree

The data tree that stores the fit parameters has many options besides the few mentioned in the example above. The following script command allows you to see all of the tree nodes (names and values) at one time, displaying them in the Script Window.

// To see the entire tree structure with values:
ParamTree.=;

Note: since the non-linear fitting procedure is iterative, parameter values for the fit that are not fixed (by setting the fix option to zero in the parameter tree) can and will change from their initial values. Initial parameters can be set manually, as illustrated in the example above by accessing individual nodes of the parameter tree, or can be set automatically by Origin (see the nlfn X-function in the table below).

## Table of X-functions Supporting Non-Linear Fitting

In addition to the three given above, there are a few other X-functions that facilitate non-linear fitting. The following table summarizes the X-functions used to control non-linear fitting:

Name Brief Description
nlbegin

Start a LabTalk nlfit session on XY data from worksheet or graph.
Note:
This X-Function fits one independent/dependent model only. For multiple dependent/independent functions, use nlbeginr instead.

nlbeginr Start a LabTalk nlfit session on worksheet data. It is used for fitting multiple dependent/independent variables functions.
nlbeginm

Start a LabTalk nlfit session on matrix data from matrix object or graph

nlbeginz

Start a LabTalk nlfit session on XYZ data from worksheet or graph

nlfn

Set Automatic Parameter Initialization option

nlpara

Open the Parameter dialog for GUI editing of parameter values and bounds

nlfit

Perform iterations to fit the data

nlend

End the fitting session and optionally create a report

For a full description of each of these X-functions and its inputs and outputs, please see the X-function Reference.

## Qualitative Differences from Linear Fitting

Unlike linear fitting, a non-linear fit involves solving equations to which there is no analytical solution, thus requiring an iterative approach. But the idea---calling X-functions to perform the analysis---is the same. Whereas a linear fit can be performed in just one line of script with just one X-function call (see the Linear Fitting section), a non-linear fit requires calling at least three X-functions.