Borrar filtros
Borrar filtros

How can I automate the order of a polynomial using polyfit?

17 visualizaciones (últimos 30 días)
John Harry
John Harry el 10 de Dic. de 2015
Respondida: Walter Roberson el 10 de Dic. de 2015
I am trying to fit a least-squared polynomial to multiple vectors at a polynomial order that best fits each vector, all of which are 101 data points in length. My issue is that I am working with 20+ vectors, each of which requires a different polynomial order. Is there a way to use polyfit while having matlab "select" the best fit polynomial to the curve. Below is a function I built (It is to compute a Spanning Set) in which I call for a specific order for the polynomial. Can I adjust the function to choose the best polynomial fit without having to visually inspect the plots for all 20+ vectors?
Thanks in advance!
Function Inputs: t = time vector (or normalized 0-100% of stride) m = the mean emsemble curve of same size as the time vector s = the standard deviation vector associated with mean ensemble curve o = the order of the polynomials to create the space that defines the vectors of the spanning set
function h = spanningset(t,m,s,o)
% Create above and below the mean standard deviations
above = m+s;
below = m-s;
% Fit a least squares polynomial to the above and below mean standard
% deviations at the order "o" entered when calling the function:
above_fit = polyfit(t,above,o);
below_fit = polyfit(t,below,o);
%[above_fit,s,mu] = polyfit(t,above,o); % centered and scaled if needed
%[below_fit,s,mu] = polyfit(t,below,o);
% Evaluate and plot the polynomial (to see the goodness of the polynomial
% fit):
above_fit_eval = polyval(above_fit,t);
below_fit_eval = polyval(below_fit,t);
figure
plot(t,above,'ob',t,above_fit_eval,'b',t,below,'ok',t,below_fit_eval,'k')
legend('Above Stdev', 'Above Polynomial Fit','Below Stdev','Below Polynomial Fit')
% Determine the Norm Difference Between The Above & Below Vectors:
magnitude = abs(above_fit)-(below_fit);
for i = 1:length(magnitude);
magnitude(i) = magnitude(i)^2; % Norm diff of the upper/lower coeffs
end
% Compute the Final norm Difference
h = sqrt(sum(magnitude));
end

Respuestas (1)

Walter Roberson
Walter Roberson el 10 de Dic. de 2015
The "best" order to use in polyfit of a vector of length N is (N-1) up to about N = 7, after which it becomes roughly (N - 3).
The (N-3) accounts for numeric error. If there were no numeric error then the best order to use for N points would always be N-1 exactly and the result would be the Lagrange Interpolating Polynomial.
If you are using polyfit() with degree 5 or higher you should be asking yourself "Why?" If you are using polyfit with degree above 7 then you should be conscious that you have likely made a mistake of either theory or numeric practice.

Categorías

Más información sobre Polynomials en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by