Fitting data with functions
The fit function allows to perform multidimensional nonlinear leastsquare fits.
The aim of the fit function is to minimize the sum of squares of the residuals (i.e. the difference between values calculated via the fit parameters and the input values).
Residual calculation
There are two different ways of specifying the residuals that should be minified: You can use a residual callback function or use the Kontrast math parser.
The advantage of the residual callback is that you can reuse existing JavaScript code and you can easily handle complex conditional logic. However, you can only use the function to fit real numbers, not complex ones.
Using the math parser has the advantage of fitting complex equations
(by setting the numberType
property to
'complex'
).
Minimal example using the residual callback
The following snippet shows how to perform a linear fit through three (x, y) data points. The result contains the value and uncertainty of the slope and intercept. Note that you are free to choose the name of any of the quantities involved. The result is logged to the console.
The residualCallback
callback function is invoked by
Kontrast internally. The function is called with a single parameter
(of type object
). The entries in the object are the
quantities specified in the quantities property.
Minimal example using the Kontrast math parser
The example performs a fit of the same data with the same fit function as above but specifies the residuals as a string that is parsed by the Kontrast math parser.
Interactive code examples
Input
The function is called as kontrast.fit(options)
, where
options
is an object with the following properties:

residuals
:string
(optional)Mathematical expression that determines how the residuals are computed. You can use the same variable names that you pick in the quantities object. If you specify theresiduals
property, theresidualCallback
property must be undefined. 
residualCallback
:function
(optional)Callback function that returns the residual for a given set of input quantities. The input quantities are specified as numeric values of entries in an object specified as a single parameter. If you specify theresidualCallback
property, theresiduals
property must be undefined. 
quantities
:object
A description of the quantities used in the function. The property names are used as names for the quantities. 
numberType
:'real'
'complex'
(default value:
)'complex'
The type of numbers used in the calculation. 
convergenceTolerance
:positiveNumber
(default value:
)1e14
If the relative change in all fit parameters is smaller than this value, the fit is assumed to have converged. 
maxIterationCount
:positiveInteger
(default value:
)100
The maximum number of iterations
Quantities
Each quantity must have a type and, depending on the type, various additional properties:

type
:'constant'
'parameter'
The type of the quantity. There are constants that stay constant during the fit (these are, for example, measured values) and there are parameters, that are fitted so that the mean square of the residuals is minimized. 
initial
:number
The initial guess for the value of the parameter. This value is used as a starting point for the fitting procedure. For nonlinear fits, the selection of a good initial value is crucial for the algorithm to succeed. 
stepSize
:number
(default value:
)0.001
The step size used to numerically compute derivatives for this quantity in the algorithm (only applied to parameters, not to constants). 
data
:numericData
An numerical array of data (only applies to the constants). If there is more than one constant, all arrays must be of equal length.
Output

iterationCount
:function
The number of iterations used until the fit converged. 
quantities
:object
An object containing the fit parameters and their error. 
degreesOfFreedom
:number
The number of degrees of freedom (the number of datatuples minus the number of fit parameters). 
standardError
:number
The standard error of the fit.