MATLAB: Taking the second derivative

arraysderivativeMATLABsecond derivative

I have two arrays M and V, both are the same dimesions.
I am trying to find and plot this vs. M, this is the code I am working with – but it doesn't work – what should I do?
second_der = diff(V_preamp,2)./diff(M,2);
The probem with the above code is it creates a second derivative code that is shorter than the independent variable I am plotting the graph with (independent variable – M)
How can I fix this isse.

Best Answer

second_der = zeros(size(V_preamp));
second_der(2:end-1) = ((V_preamp(3:end)-V_preamp(2:end-1))./...
(M(3:end)-M(2:end-1))-...
(V_preamp(2:end-1)-V_preamp(1:end-2))./...
(M(2:end-1)-M(1:end-2)))./...
(0.5*(M(3:end)-M(1:end-2)));
plot(M(2:end-1),second_der(2:end-1))
Related Question