Filename: 351u2002_matlab_least_squares.html
In this web page I indicate how to obtain and plot least squares polynomials in Matlab. I have tried to keep this example as straight-forward as possible. Thus there are many refinements that I have not indicated (and probably many that I don't know). Explore Matlab's help system!
Note: I will not include the output from the commands here. In some cases I have included a semicolon to suppress Matlab's output in the first place.
We need some "random" data to illustrate the commands.
>> Xdata = linspace(0,10,21);
>> Ydata = Xdata + sin(Xdata);
Let's see what x + sin(x) looks like (though we don't need to do so) before getting into the least squares stuff:
>> plot(Xdata,Ydata)
Here's how to find the coefficients for the least squares fit line (note the 1 indicates a line).
>> p = polyfit(Xdata,Ydata,1)
In order to plot the least squares line we need to decide at what points to evaluate it (unlike Maple were we can plot a functional relation directly).
>> Xev = linspace(0,10,41)
Now evaluate the polynomial at each point in Xev
>> Yev = polyval(p,Xev)
Let's plot the line and the original data
>> plot(Xdata,Ydata,'+w',Xev,Yev,'y')
Let's try a degree 4 polynomial fit instead.
>> p4=polyfit(Xdata,Ydata,4);
>> Yev4=polyval(p4,Xev);
>> plot(Xdata,Ydata,'+w',Xev,Yev4,'y')
The degree 4 fit certainly looks better, but you should be careful not to draw any
unwarranted conclusions. Extrapolation is definitely out. For example the degree 4
polynomial here decreases after x = 10 whereas x + sin(x) continues to increase! If you
were dealing with experimental data the good fit might mislead you! You really need to
have an underlying model which implies a certain functional relation before a least
squares fit with that function allows you to reasonably extrapolate or draw any
conclusions other than statistical ones concerning properties of the actual data.