% Least squares straight fitting, L. Braile September 2002 % Display error estimates for predicted values of y % % Enter data x = [1.0 3.0 3.9 4.0 4.2 5.0 5.5 5.8 6.2 6.5 6.8 8.0 8.5]; y = [5.5 4.0 3.9 3.9 3.4 3.5 3.0 2.7 2.5 2.3 2.5 2.2 0.4]; % use Matlab function polyfit to determine least squares fit to data [p,s] = polyfit(x,y,1); xt = [-2.0:0.5:12]; % evaluate polynomial at the xt locations [yt,del2] = polyval(p,xt,s); % plot the data, theoretical line, least squares fit and 95% error bound on y plot(x,y,'or',xt,yt,'-k',xt,yt+2*del2,':r',xt,yt-2*del2,':r') xlabel('x-value') ylabel('y-value') title('Least squares regression line and 95% error bounds on the predicted values of y')