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 = 2443 (-1.229e+12, 1.229e+12) b = -0.2574 (-1.87e+04, 1.87e+04) c = -2441 (-1.229e+12, 1.229e+12) d = -0.2575 (-1.872e+04, 1.872e+04) ```

## 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 ```
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://cn.mathworks.com/examples/curvefitting/mw/curvefit-ex72685292-fit-exponential-models-using-the-fit-function", "fetch_time": 1516382999000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2018-05/segments/1516084888077.41/warc/CC-MAIN-20180119164824-20180119184824-00645.warc.gz", "warc_record_offset": 651978851, "warc_record_length": 12926, "token_count": 751, "char_count": 2379, "score": 3.65625, "int_score": 4, "crawl": "CC-MAIN-2018-05", "snapshot_type": "latest", "language": "en", "language_score": 0.6695150136947632, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00037-of-00064.parquet"}