% Linear Algebra: Theory and Applications % Ward Cheney & David Kincaid % JBPub.com (c) 2008 % File: exp726.m % Example 7.2.6 % Fitting a linear least square polynomial to % the given data. This Matlab program evaluates % a and b for the Linear Least Squares. % 'polyfit' fits a polynomial of degree n, here % n = 1. % evaluating a and b for Linear Least Squares: xdata = [1.0 2.0 2.5 3.0] ydata = [3.7 4.1 4.3 5.0] ls = polyfit(xdata, ydata, 1) pause % evaluating the value of the function phi: d =ls(1)*xdata + ls(2) - ydata phi = sum(d.^2) pause % plotting the linear least square on a finer data range: x = 0 :0.1:4 y = polyval(ls, x) plot(xdata, ydata, 'ok', x, y, '-r') title('Linear least squares fit') xlabel('x axis'), ylabel('y axis')