If I had a nickel for every time someone asked a similar question, I won't say I'd be rich, but at least I'd get tired of rolling up those nickels and carrying them to the bank.
A problem is that it is easy to see something in your mind. You say, thats what I want to see. But doing the computation can be sometimes less easy. Computing the derivative(s) of a noisy function, especially when the data is not equally spaced is what is called an ill-posed problem. It amplifies any noise in the data. And that amplification can be significant. Finding the location of a second derivative max for noisy data can be nasty as hell to do.
So what would I suggest?
Don't use polyfit!!!!!!! Using a high order polynomial here is absolutely insane. Raising the order of the polynomial is insanity raised to a power. Run as fast as you can, away from polyfit!
I don't have your data, so I can't give you much of an example. In general, a far better choice will be a spline model. If you attach your data in a .mat file to a comment, I can give you a decent example of how I would approach the problem.
x = linspace(-5,5,100);
y = exp(-x.^2) + sin(x/2);
plot(x,y,'o'),grid on
This curve has NO noise in it at all. So I can use a simple interpolating spline to fit it.
[fppmax,fppmaxloc] = slmpar(pp,'maxfpp')
fppmax =
1.0405
fppmaxloc =
-1.2626
[fppmin,fppminloc] = slmpar(pp,'minfpp')
fppmin =
-2.0011
fppminloc =
0.050505
In fact, I'd probably not have picked that spot for the maximum of the second derivative by eye, but that location does make sense.
Dp you want to see the second derivative function plotted? This will give you a hint as to whether it was successful.
If my data was noisy, but equally spaced in x (as it is here, but with noise added on) then I would use either a smoothing spline of some sort (SLM will do this nicely) or I would use a Savitsky-Golay filter.
If the data is unequally spaced AND noisy, then a smoothing or least squares spline of some sort is your only viable choice.