MATLAB Answers

Is there a way to directly take the second derivative of data?

77 views (last 30 days)
Benjamin
Benjamin on 15 Nov 2018
Commented: Benjamin on 19 Nov 2018
In MATLAB, can I take the second derivative of data directly? Or do I need to take the derivative first using gradient, and then take the derivative of that. I am worried I am losing some precision each time, and wondering if I could go straight from y to y''.

  0 Comments

Sign in to comment.

Answers (2)

John D'Errico
John D'Errico on 15 Nov 2018
Edited: John D'Errico on 16 Nov 2018
Well, even gradient or diff does not actually differentiate data. They can give you an approximation to the derivative at each data point.
And, yes, you are correct to fear multiple sequential calls to gradient, as it is anapproximation at each step. The problem with numerical differentiation is it is a noise amplifiying process, sometimes called an ill-posed problem.
You don't tell us enough about your problem that I can be sure about the best approach. Is your data equally spaced in x? Is there noise in the process? This makes things more difficult.
There are several ideas you could use. I'd suggest that perhaps a Savitsky-Golay filter might be a good idea, if there is noise, and if the data is uniform in x. Or perhaps if there is no noise, then you might pass an interpolating spline through the points, then differentiate the spline twice, evaluating it at the points. Since you are looking for a second derivative, I'd probably suggest a quintic spline as a good idea. Or if there is noise in the data, then asmoothing or least squares spline canbe of value. Again, you would differentiate it at the end, and you would not incur the cost of recursive numerical differentiation.
If you truly want better advice, I would need to know more about the data, and your problem, as well as have you attach a copy of the data.

  1 Comment

Benjamin
Benjamin on 19 Nov 2018
John,
Thank you for your detailed answer. Can you explain what you mean when you say that gradient does not actually differentiate the data? How is gradient calculating it then? And how is it different than diff?
For my problem speciically, the data is evenly spaced in x, and there is not a lot of noise. I was thinking maybe I should calculate my derivatives differently. How would I use a quintic spline?

Sign in to comment.


Jim Riggs
Jim Riggs on 16 Nov 2018
Edited: Jim Riggs on 16 Nov 2018
Here are some formulas that I sometimes use:
Everything that John says about data smoothing and noise still applies.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by