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

 Respuesta aceptada

Torsten
Torsten el 19 de Mzo. de 2015

0 votos

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

5 comentarios

Jason
Jason el 19 de Mzo. de 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 el 20 de Mzo. de 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 el 20 de Mzo. de 2015
At least an approximation of the maximum value ...
Best wishes
Torsten.
Jason
Jason el 20 de Mzo. de 2015
Yes, they represent a level on a 12bit DAC, so I only need unit granularity. Jason
gwoo
gwoo el 19 de Ag. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

John D'Errico
John D'Errico el 19 de Mzo. de 2015
Editada: John D'Errico el 19 de Mzo. de 2015

1 voto

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 comentario

John D'Errico
John D'Errico el 19 de Mzo. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Descriptive Statistics and Insights en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Mzo. de 2015

Comentada:

el 19 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by