2.13.3.2 Nonlinear FittingNonlinearFitting
Fit NonLinearRegressionNonlinear FittingScript, FittingNonlinear fitting in LabTalk is Xfunction based and proceeds in three steps, each calling (at least) one Xfunction:
 nlbegin: Begin the fitting process. Define input data, type of fitting function, and input parameters.
 nlfit: Perform the fit calculations
 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 XFunctions:
 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 nonlinear 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 Xvalue 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 nonlinear 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 Xfunction in the table below).
Table of Xfunctions Supporting NonLinear Fitting
In addition to the three given above, there are a few other Xfunctions that facilitate nonlinear fitting. The following table summarizes the Xfunctions used to control nonlinear fitting:
Name 
Brief Description

nlbegin

Start a LabTalk nlfit session on XY data from worksheet or graph.
Note: This XFunction 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 Xfunctions and its inputs and outputs, please see the Xfunction Reference.
Qualitative Differences from Linear Fitting
Unlike linear fitting, a nonlinear fit involves solving equations to which there is no analytical solution, thus requiring an iterative approach. But the ideacalling Xfunctions to perform the analysisis the same. Whereas a linear fit can be performed in just one line of script with just one Xfunction call (see the Linear Fitting section), a nonlinear fit requires calling at least three Xfunctions.
