To obtain the least-squares polynomial of degree n in Matlab, you can use the polyfit
function. Here's an example:
main.m272 chars12 lines
In this example, we define the x
and y
vectors that represent our data points. We then use the polyfit
function to fit a polynomial of degree 2 to the data using least squares. The resulting coefficients of the polynomial are stored in the p
variable. Finally, we plot both the original data points and the fitted polynomial using the plot
and polyval
functions.
gistlibby LogSnag