I want to calculate the second derivative of an anonymous function in Matlab. I'm already aware of some formulas for this (Numerical Differentiation) but they seem not to work.
I'm able to calculate the first derivative with :
f = @(x) (x^3);
h = 1e-10;
df = @(x) (f(x+h) - f(x))/h;
But when I try to calculate the second derivative with the following, I don't get the expected result :
f = @(x) (x^3);
h = 1e-10;
d2f = @(x) (f(x+h) - 2*f(x) + f(x-h))/(h^2);
For d2f I'm supposed to get a function similar to d2f = 6x, but if a plot d2f I get this : plot d2f
What I'm doing wrong?