how to find best order for polyfit in matlab

To find the best order for polyfit in MATLAB, you can use the following steps:

  1. Define a vector of the orders you want to test, for example:
orders = 1:10;
15 chars
2 lines
  1. Apply polyfit to your data for each order:
for i = 1:length(orders)
    coeffs = polyfit(x,y,orders(i));
    % compute the goodness of fit metric (e.g., R-squared)
    % and store it in a vector
    rsquared(i) = computeRsquared(y,polyval(coeffs,x));
end
212 chars
7 lines

In this code, x and y are your data vectors, and computeRsquared is a function that computes a goodness of fit metric, such as R-squared.

  1. Plot the goodness of fit as a function of the order:
plot(orders,rsquared,'o-')
xlabel('Order')
ylabel('R-squared')
63 chars
4 lines

This plot can help you identify the order that gives the best balance between goodness of fit and model complexity.

Note that this approach assumes that a polynomial model is appropriate for your data. If you are not sure, you may want to consider other types of models as well.

gistlibby LogSnag