# lsqcurvefit

Find the equation parameters that produce the least squares best fit to a data set.

## Syntax

x = lsqcurvefit(@func,x0,xdata,ydata)

x = lsqcurvefit(@func,x0,xdata,ydata,lb,ub)

x = lsqcurvefit(@func,x0,xdata,ydata,lb,ub,options)

[x,resnorm,residual,exitflag,output] = lsqcurvefit(...)

## Inputs

`func`- The function of system residuals. See the optimset option Jacobian for details.
`x0`- An estimate of the best fit parameters.
`xdata`- The domain values for which the best fit is performed.
`ydata`- The range values for which the best fit is performed.
`lb`- The fitting parameter lower bounds.
`ub`- The fitting parameter upper bounds.
`options`- A struct containing option settings.

## Outputs

- x
- The best fit parameters.
- resnorm
- The squared length of the residuals vector.
- residual
- The residuals vector.
- info
- The convergence status flag.
- info = 4
- Relative step size converged to within tolX.
- info = 3
- Relative function value converged to within tolFun.
- info = 2
- Step size converged to within tolX.
- info = 1
- Function value converged to within tolFun.
- info = 0
- Reached maximum number of iterations or function calls, or the algorithm aborted because it was not converging.
- info = -3
- Trust region became too small to continue.

- output
- A struct containing iteration details. The members are as follows:
- iterations
- The number of iterations.
- nfev
- The number of function evaluations.
- xiter
- The candidate solution at each iteration.
- resnormiter
- The objective function value at each iteration.

## Examples

Fit an exponential curve to the data
provided.

```
function y = FittingFunc(p, x)
y = p(1) * exp(-p(2)*x);
end
x = [1; 2; 3; 4];
y = [8.025, 3.975, 2.025, 0.975];
p0 = [15; 1];
[p,res] = lsqcurvefit(@FittingFunc,p0,x,y)
```

```
p = [Matrix] 2 x 1
16.09848
0.69669
res = 0.00190995098
```

Modify the previous example to pass an extra parameter to the function using a function
handle.

```
function y = FittingFunc(p, x, offset)
y = p(1) * exp(-p(2)*x) + offset;
end
handle = @(x, p) FittingFunc(x, p, 2);
[p,res] = lsqcurvefit(handle,p0,x,y+2);
```

## Comments

lsqcurvefit uses a modified Gauss-Netwon algorithm with a trust region method.

Options for convergence tolerance controls and analytical derivatives are specified with optimset.

To pass additional parameters to a function argument, use an anonymous function.

The optimset options and defaults are as follows:

- MaxIter: 400
- MaxFunEvals: 1,000,000
- TolFun: 1.0e-7
- TolX: 1.0e-7
- Jacobian: 'off'
- Display: 'off'