To find the best order for polynomial fitting in Matlab, we can use the polyfit
function along with cross-validation to determine the best fit. The basic idea is to fit the polynomials of different orders to the data and select the order that provides the best fit based on some metric.
Here's an example implementation:
main.m975 chars41 lines
In this example, we first load the data from a file 'mydata.mat' and split it into 80% training set and 20% test set. We then loop over different orders (up to a maximum order of 10) and fit the polynomial using polyfit
. For each order, we compute the mean squared error for both the training and test sets. Finally, we plot the error curves as a function of order and select the order that minimizes the test error.
Note that this is just one possible approach to order selection for polynomial fitting, and there are many other methods that could be used depending on the specific problem at hand.
gistlibby LogSnag