x = [-1 0 2 3 4 7]';
y = [9 8 5 5 3 1]';
A = [x x.^0];
z = A\y;
xx = linspace(min(x), max(x), 500)';
yy = [xx xx.^0]*z;
plot(xx, yy, 'b', x, y, 'r*')
Explanation: think of
\(cx+d\) as
\(cx + dx^0\) because this naturally leads to the matrix of this linear system: its columns are
x and
x.^0. The plot of given data points created by
plot(x, y, 'r*') consists of red asterisks, not joined by a line. But to plot the line of best fit (or a curve of best fit, in general) we use more points on the x-axis. These points are in the vector
xx: they are evenly distributed over the same interval where the given data points lie. The way in which y-coordinates of this line are computed may look strange. The matrix product
[xx xx.^0]*z is logically the same as
z(1)*xx + z(2): multiply x-values by the coefficient
\(c\) and add the constant term
\(d\text{.}\) Writing it in matrix form allows for easier generalization later. Note that the line
yy = [xx xx.^0]*z, which uses the solution of a linear system, is consistent with
A = [x x.^0] which created that system.