% MATLAB script file for fitting the linear model, y = m*x + b to 
% experimental (x,y_hat) data.
%
% See Supplemental Unit S3 of "A First Course on Kinetics and Reaction
% Engineering" and/or the MATLAB documentation for additional information
% or if questions arise.
%
% Carl R. F. Lund, 4/22/13
%   last rev. 6/4/13
%
% Required input (must be present in the MATLAB workspace): 
%   x (column vector) - x(i) is the value of the set variable x for data
%       point i
%   y_hat (column vector) - y_hat(i) is the measured value of the response
%       variable for data point i

% Perform the regression analysis
[f,gof]=fit(x,y_hat,'poly1');
ci = confint(f);

% Report the results
r_squared = gof.rsquare
m = f.p1
m_u = (ci(2,1)-ci(1,1))/2.0
b = f.p2
b_u = (ci(2,2)-ci(1,2))/2.0

% Generate the model plot
plot(f,x,y_hat,'o')

% End of FitLinmbSR.m