%% 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)

%
%% Fit a Two-Term Exponential Model

f2 = fit(x,y,'exp2')
plot(f2,x,y)

%
%% 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)

%
% 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)

%
%% 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')

% Copyright 2015 The MathWorks, Inc.