MATLAB: Second Derivative using diff

diff()second derivative

I have a quick question concerning second derivatives using the diff function.
So, after I put in my x and y arrays I did
y1=diff(y)./diff(x)
I know that I have to change the length of y1 to take the second derivative. But I'm not sure in which order it should go. Should it be:
y1=[0 y1]
or
y1=[y1 0]
Then I would proceed to do
y2=diff(y1)./diff(x)
Any help is greatly appreciated! Thank you!

Best Answer

I wouldn’t use diff to calculate numerical derivatives. It has its uses, but I always use the gradient function for derivative calculations, since gradient produces a result the same size as its argument.
Related Question