# Fitting

Horace allows you to fit your data. Beware, however, that the fitting algorithms are not especially optimised and can therefore take quite a long time to converge if the dataset to be fitted is large.

## fit_sqw

>> [wout, fitdata] = fit_sqw(win, sqwfunc, pin) >> [wout, fitdata] = fit_sqw(win, sqwfunc, pin, pfree) >> [wout, fitdata] = fit_sqw(win, sqwfunc, pin, pfree, keyword, value)

Required inputs are:

`win`

- dnd object or array of dnd objects (all of same dimensionality)

`sqwfunc`

- handle of the function to calculate sqw. The function must have the form

`weight = sqwfunc (qh,qk,ql,en,p,c1,c2,...)`

,where qh, qk, ql, and en are arrays containing the coordinates of a set of points in reciprocal space (note difference in form to that used in `fit`

- see below). p is a vector of parameters needed by dispersion function (e.g. [A,js,gam] as amplitude, exchange, lifetime). c1, c2, ... are additional, non-fittable, parameters required by the function (e.g. lookup tables for magnetic form factor, etc). weight is an array containing calculated intensity.

`pin`

gives the initial function parameter values [pin(1), pin(2)...]

`pfree`

indicates which are the free parameters in the fit (e.g. [1,0,1,0,0] indicates first and third are free). The default (if`pfree`

is not specified) is that all are free.

Optional keywords are :

`'list'`

- a numeric code to control output to Matlab command window to monitor status of fit

=0 for no printing to command window; =1 prints iteration summary to command window; =2 additionally prints parameter values at each iteration

`'fit'`

- array of fit control parameters.

fcp(1) is the relative step length for calculation of partial derivatives; fcp(2) is the maximum number of iterations; fcp(3) is the stopping criterion: relative change in chi-squared (i.e. stops if chisqr_new-chisqr_old < fcp(3)*chisqr_old).

`'keep'`

- ranges of data to retain for fitting. A range is specified by a pair of numbers which define the lower and upper bounds, i.e. [xlo,xhi]. Several ranges can be given by making an (m x 2) array such as [x1_lo, x1_hi; x2_lo, x2_hi; ...].

`'remove'`

- ranges to remove from fitting. Follows the same format as 'keep'.

`'mask'`

- array of ones and zeros, with the same number of elements as the data array, that indicates which of the data points are to be retained for fitting

`'select'`

- calculates the returned function values, yout, only at the pointsthat were selected for fitting by 'keep' and 'remove'; all other points are set to NaN. This is useful for plotting the output, as only those points that contributed to the fit will be plotted.

`'all'`

- requests that the calculated function be returned over the whole of the domain of the input dataset. If not given, then the function will be returned only at those points of the dataset that contain data.

The outputs are:

`wout`

- n-D dataset object containing the evaluation of the function for the fitted parameter values.

`fitdata`

- the result of fit for each dataset. It is a structure array with fields:- fitdata.p - parameter values
- fitdata.sig - estimated errors (=0 for fixed parameters)
- fitdata.corr - correlation matrix for free parameters
- fitdata.chisq - reduced Chi^2 of fit (i.e. divided by (no. of data points) - (no. free parameters))
- fitdata.pnames - parameter names (if the sqw function is an mfit function; otherwise named 'p1','p2',...).

## fit

>> [wout, fitdata] = fit(win, func, pin) >> [wout, fitdata] = fit(win, func, pin, pfree) >> [wout, fitdata] = fit(win, func, pin, pfree, keyword, value)

The input and output arguments are the same as those used for `fit_sqw`

. The only exception is the functional form of the fitting function `func`

. This has the form:

`func`

- handle of the function to calculate the intensity. The function must have the form

`y = func (x1,x2,...,p,c1,c2,...)`

,where x1, x2,... are arrays containing the coordinates of the points in the dataset. This means that for a d2d then only 2 arrays need to be supplied, whereas for a d4d 4 are required, and so on. p is a vector of parameters needed by dispersion function (e.g. [A,js,gam] as amplitude, exchange, lifetime). c1, c2, ... are additional, non-fittable, parameters required by the function (e.g. lookup tables for magnetic form factor, etc). weight is an array containing calculated intensity.