I am trying to calculate the location of maximum curvature on many curves, and I notice that many times the curvature calculation is not maximum at the location I expect, whereas the rest of the time it seems to work.
For an example case in which this does not work as expected, I have included my curvature estimate calculation below (using diff to estimate first derivative and second derivative from given values describing a curve), plus the plots of each of the steps to determine max curvature. As you can see, the max curvature seems to occur way low on the curve, in contrast to expectation. Can someone please explain why this is and how to fix it?
first_deriv=(np.gradient(y))
second_deriv=(np.gradient(np.gradient(y)))
curvature=second_deriv/((1+first_deriv**2)**1.5)
fig, axs = plt.subplots(4,1,figsize=(6,10))
axs[0].plot(x,y)
axs[0].axvline(1000,linestyle='--',color='red')
axs[0].set_ylabel('Fit at \n Current')
axs[1].plot(x,first_deriv)
axs[1].axvline(1000,linestyle='--',color='red')
axs[1].set_ylabel('First \n Derivative')
axs[2].plot(x,second_deriv)
axs[2].axvline(1000,linestyle='--',color='red')
axs[2].set_ylabel('Second \n Derivative')
axs[3].plot(x,curvature)
axs[3].axvline(1000,linestyle='--',color='red')
axs[3].set_ylabel('Curvature')
Original curve vs calculations leading to curvature, with red vertical line placed where I would have identified maximum curvature by eye: