How do I find the max value of a fit to data. I am using:
f=fit(x,y,'poly5')
I have tried
p=polyder(f)
roots(P)
But I get an error

 Réponse acceptée

Torsten
Torsten le 19 Mar 2015

0 votes

f=fit(x,y,'poly5');
c = coeffvalues(f);
cd = polyder(c);
roots(cd);
Best wishes
Torsten.

5 commentaires

Jason
Jason le 19 Mar 2015
Thankyou, John and Torsten.
So I typically have more than one root of the derivative. Is there a better way to determine the max as shown by the graph below.
Jason
Jason le 20 Mar 2015
OK, I've worked out how:
for j=1:3000
A(j,:)=f(j); %Create a column array
end
B=max(A(:));
idx=find(A==B);
Torsten
Torsten le 20 Mar 2015
At least an approximation of the maximum value ...
Best wishes
Torsten.
Jason
Jason le 20 Mar 2015
Yes, they represent a level on a 12bit DAC, so I only need unit granularity. Jason
gwoo
gwoo le 19 Août 2021
Just to simplify, that little bit of code can be:
A = f([1:3000]'); % Create a column array
[maxValue, maxIndex] = max(A); % Second output of max() is the index of the max value
This simplification works if A is a 1-column vector.

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 19 Mar 2015
Modifié(e) : John D'Errico le 19 Mar 2015

1 vote

So what error did you get? It helps if you are complete in your comments about an error so that others can help you.
I'll bet that since polyder is not designed to work with polynomials in the form that polyfit generates, that it did not understand how to work with the polynomial object returned from fit.
This should work though:
roots(polyder(coeffvalues(f)))

1 commentaire

John D'Errico
John D'Errico le 19 Mar 2015
To determine the maximum value, you evaluate the function at each (real) candidate. Thus at each root of the derivative, evaluate the original function, and take the maximum value over all candidate roots.
If a root was outside of the interval (the support of your data) then this would be extrapolation, so generally a bad idea to use extrapolated values for this, especially if the extrapolation was done from a polynomial.
In some circumstances, you might also compare the value of the function at the first and last data point, assuming you choose not to allow extrapolation. For the curve as shown in your figure, this is of course not necessary.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Descriptive Statistics and Insights dans Centre d'aide et File Exchange

Tags

Question posée :

le 19 Mar 2015

Commenté :

le 19 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by