%% Linear Model with Nonpolynomial Terms
% This example shows how to fit data with a linear model containing nonpolynomial terms.
% When a polynomial function does not produce a satisfactory model of your data, you can try using a linear model with nonpolynomial terms. For example, consider the following function that is linear in the parameters a_{0}, a_{1}, and a_{2}, but nonlinear in the t data:
% y = a_{0} + a_{1}e^{-t} + a_{2}te^{-t}.
% You can compute the unknown coefficients a_{0}, a_{1}, and a_{2} by constructing and solving a set of simultaneous equations and solving for the parameters. The following syntax accomplishes this by forming a design matrix, where each column represents a variable used to predict the response (a term in the model) and each row corresponds to one observation of those variables.
%
% Enter t and y as column vectors.

t = [0 0.3 0.8 1.1 1.6 2.3]';
y = [0.6 0.67 1.01 1.35 1.47 1.25]';

%
% Form the design matrix.

X = [ones(size(t))  exp(-t)  t.*exp(-t)];

%
% Calculate model coefficients.

a = X\y

% Therefore, the model of the data is given by
% y = 1.3983 - 0.8860e^{-t} + 0.3085te^{-t}.
%
% Now evaluate the model at regularly spaced points and plot the model with the original data.

T = (0:0.1:2.5)';
Y = [ones(size(T))  exp(-T)  T.*exp(-T)]*a;
plot(T,Y,'-',t,y,'o'), grid on
title('Plot of Model and Original Data')

% Copyright 2015 The MathWorks, Inc.