Max value of a fit
17 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Torsten
on 19 Mar 2015
f=fit(x,y,'poly5');
c = coeffvalues(f);
cd = polyder(c);
roots(cd);
Best wishes
Torsten.
5 Comments
gwoo
on 19 Aug 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.
More Answers (1)
John D'Errico
on 19 Mar 2015
Edited: John D'Errico
on 19 Mar 2015
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 Comment
John D'Errico
on 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.
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!