MATLAB: Calculating mixed partial derivatives of a function A(x,y)

MATLABmixed numerical derivativesnumerical derivativespartial derivatives

I have a script in MATLAB to calculate the value of a function A. I have also programmed the analytic derivatives of A with respect to x and y and also d2A/dxdy. Now, I want to check that my analytic derivatives are correct with a numerical validation. The form of the function is very complicated, so it's not so easy as just re-checking my math. How would you program the numerical derivative of a mixed partial derivative in MATLAB?

Best Answer

syms x y
f(x,y) = x*sin(y);
d2fdxdy = diff(diff(f,x),y); % = cos(y)
Another way is:
syms x y
diff(x*sin(x*y),x,y); % = 2*x*cos(x*y)-x^2*y*sin(x*y)