how to determine the best order of polynomial in matlab

There is no fixed rule to determine the order of polynomial. However, we can use the following methods to determine the best order of polynomial in MATLAB:

  1. Visual Inspection: Plot the data and observe the trend. If the data points seem to follow a linear trend, then a first-order polynomial is sufficient. If the data points seem to follow a curve, then a second or higher-order polynomial may be needed.

  2. Residual Analysis: Calculate the residual for each polynomial degree and choose the degree with the lowest residual values.

Here is an example code that demonstrates how to apply the above methods:

main.m
% Generate example data
x = (0:0.1:2)';
y = 1 + 2*x + 3*x.^2 + randn(size(x))*0.5;

% Visual inspection
figure
plot(x,y,'o')
xlabel('x')
ylabel('y')
legend('data')

% Residual analysis
for k = 1:10
    p = polyfit(x,y,k);
    yfit = polyval(p,x);
    residuals(:,k) = y - yfit;
    SSE(k) = sum(residuals(:,k).^2);
end
figure
plot(SSE,'o-')
xlabel('Polynomial degree')
ylabel('SSE')
383 chars
23 lines

The first figure shows the scatter of the example data, while the second figure shows the SSE values for various polynomial degrees. Based on both visual inspection and residual analysis, we can see that a second-order polynomial is the best fit for this particular data.

related categories

gistlibby LogSnag