%% Annotated Matlab transcript -- 5/1 %% CS 323 - Doug DeCarlo % least squares fitting % data from the book x = 1:0.2:5; y = [-1.945 -1.253 -1.140 -1.087 -0.760 -0.682 -0.424 -0.012 -0.190 0.452 0.337 0.764 0.532 1.073 1.286 1.502 1.582 1.993 2.473 2.503 2.322]; plot(x,y,'.'); % here is the data with the line that results from a least squares fit plot(x,y,'.',x,1.06338*x-2.74605); % solving the system... A = [size(x,2) sum(x); sum(x), sum(x.^2)] f = [sum(y); sum(x.*y)] z = A \ f plot(x,y,'.',x,1.06338*x-2.74605); % the fit locations yf = 1.06338*x-2.74605; % measuring the error between these locations and the data sqrt(1/size(x,2)*sum((y - yf).^2)) % we notice this error increases when we tweak the line equation % (as the above solution minimizes this error) yf = 1.07338*x-2.74605; sqrt(1/size(x,2)*sum((y - yf).^2)) yf = 2.07338*x-5.74605; sqrt(1/size(x,2)*sum((y - yf).^2)) % and here we see a plot of one of these tweaked lines... plot(x,y,'.',x,2.07338*x-5.74605);