clear

% Enter Data
x=[.1 .2 .3 .4 .5];
y=[1.089 2.042 2.6710 3.5150 4.4060];
figure(1)

% Polynomial Fit
n = 1;                       % n = deg polynomial fit
[m,b]=polyfit(x,y,n);        % m = [slope intercept]

% Plot (xfit, yfit) for model
xfit = linspace(0,.6,20);
yfit = polyval(m,xfit);
plot(x,y,'ok',xfit,yfit);

% Residuals
ymodel = polyval(m,x);      % predicted values
resid  = y - ymodel;        % residuals
 
% R^2 Value (Rsq)
dev = y - mean(y);          % deviations - measure of spread
SST = sum(dev.^2);          % total variation accounted for
SSE = sum(resid.^2);        % variation NOT accounted for
Rsq = 1 - SSE/SST;          % percent of error explained

% F-Statistic
SSR = SST - SSE;            % variation of residual
dfe = length(x) - 1 - n;    % degrees of freedom
MSE = SSE/dfe;              % mean-square error of residuals
MSR = SSR/n;                % mean-square error for regression
f = MSR/MSE;                % f-statistic

% t-Statistics for coeffs from b in polyfit
R = b.R;                
covm = (R'*R)\eye(n+1);     % The covariance matrix
d = diag(covm)';            % ROW vector of the diagonal elements
MSE = (b.normr^2)/b.df;     % variance of the residuals
se = sqrt(MSE*d);           % the standard errors
t = m./se;                  % observed T-values 