MATLAB Examples

# Fit Exponential Models Using the fit Function

This example shows how to fit an exponential model to data using the fit function.

The exponential library model is an input argument to the fit and fittype functions. Specify the model type 'exp1' or 'exp2' .

## Fit a Single-Term Exponential Model

Generate data with an exponential trend and then fit the data using a single-term exponential. Plot the fit and data.

```x = (0:0.2:5)'; y = 2*exp(-0.2*x) + 0.1*randn(size(x)); f = fit(x,y,'exp1') plot(f,x,y) ```
```f = General model Exp1: f(x) = a*exp(b*x) Coefficients (with 95% confidence bounds): a = 2.021 (1.89, 2.151) b = -0.1812 (-0.2104, -0.152) ```

## Fit a Two-Term Exponential Model

```f2 = fit(x,y,'exp2') plot(f2,x,y) ```
```f2 = General model Exp2: f2(x) = a*exp(b*x) + c*exp(d*x) Coefficients (with 95% confidence bounds): a = 537.7 (-1.307e+10, 1.307e+10) b = -0.2573 (-4112, 4112) c = -535.7 (-1.307e+10, 1.307e+10) d = -0.2576 (-4131, 4130) ```

## Set Start Points

The toolbox calculates optimized start points for exponential fits based on the current data set. You can override the start points and specify your own values.

Find the order of the entries for coefficients in the first model ( f ) by using the coeffnames function.

```coeffnames(f) ```
```ans = 2x1 cell array {'a'} {'b'} ```

If you specify start points, choose values appropriate to your data. Set arbitrary start points for coefficients a and b for example purposes.

```f = fit(x,y,'exp1','StartPoint',[1,2]) plot(f,x,y) ```
```f = General model Exp1: f(x) = a*exp(b*x) Coefficients (with 95% confidence bounds): a = 2.021 (1.89, 2.151) b = -0.1812 (-0.2104, -0.152) ```

## Examine Exponential Fit Options

Examine the fit options if you want to modify fit options such as coefficient starting values and constraint bounds appropriate for your data, or change algorithm settings. For details on these options, see the table of properties for NonlinearLeastSquares on the fitoptions reference page.

```fitoptions('exp1') ```
```ans = Normalize: 'off' Exclude: [] Weights: [] Method: 'NonlinearLeastSquares' Robust: 'Off' StartPoint: [1x0 double] Lower: [1x0 double] Upper: [1x0 double] Algorithm: 'Trust-Region' DiffMinChange: 1.0000e-08 DiffMaxChange: 0.1000 Display: 'Notify' MaxFunEvals: 600 MaxIter: 400 TolFun: 1.0000e-06 TolX: 1.0000e-06 ```