Max value of a fit

17 views (last 30 days)
Jason
Jason on 19 Mar 2015
Commented: gwoo on 19 Aug 2021
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

Accepted Answer

Torsten
Torsten on 19 Mar 2015
f=fit(x,y,'poly5');
c = coeffvalues(f);
cd = polyder(c);
roots(cd);
Best wishes
Torsten.
  5 Comments
Jason
Jason on 20 Mar 2015
Yes, they represent a level on a 12bit DAC, so I only need unit granularity. Jason
gwoo
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.

Sign in to comment.

More Answers (1)

John D'Errico
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
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.

Sign in to comment.

Categories

Find more on Polynomials in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!