The polyfit
function in MATLAB can be used to perform least squares regression. It can fit a polynomial model of degree n
to the given data using the n+1
data points.
Here's the MATLAB code for performing a least squares regression for a linear model of the form y = mx + b
:
main.m258 chars16 lines
In the above code, x
and y
are the input data points. The polyfit
function is used to fit a linear polynomial to the data (1
indicates that we want a linear model). The coefficients of the fitted line are then extracted from the result of polyfit
. Finally, the data and fitted line are plotted using the scatter
and plot
functions, respectively.
gistlibby LogSnag