二元函数的梯度运算-2

[x,y]=meshgrid(-3:.1:3,-2:.1:2);

z=(x.^2-2*x).*exp(-x.^2-y.^2-x.*y);

[fx,fy]=gradient(z);fx=fx/0.1;fy=fy/0.1;

zx=-exp(-x.^2-y.^2-x.*y).*(-2*x+2*x.^3+x.^2.*y-4*x.^2-2*x.*y);

zy=-x.*(x-2).*(2*y+x).*exp(-x.^2-y.^2-x.*y);

surf(x,y,abs(fx-zx));axis([-3 3 -2 2 0,0.02])

figure;surf(x,y,abs(fy-zy));axis([-3 3 -2 2 0,0.06])

注:227代码与226代码区别在于227为减小误差将网格加密了一倍