% tangent plane demo
% author: Fernando Guevara Vasquez
x=linspace(-2,2,20); y=linspace(-2,2,20);
[xx,yy]=meshgrid(x,y);

% draw the surface z = f(x,y)
zz = exp(-xx.^2-yy.^2);
figure(1); clf;
h=mesh(xx,yy,zz); set(h,'edgecolor','k','facecolor','green','facealpha',0.5);

% draw the plane tange to the surface at (1,0) (using slightly different vars)
p0=[1,0];
x1=linspace(p0(1)-1/2,p0(1)+1/2,20); y1=linspace(p0(2)-1/2,p0(2)+1/2,20);
[xx1,yy1]=meshgrid(x1,y1);
hold on;
tp = exp(-1) - 2*exp(-1)*(xx1-1);
h=mesh(xx1,yy1,tp); set(h,'edgecolor','none','facecolor','red','facealpha',0.5);
plot3(1,0,exp(-1),'*')

xlabel('x'); ylabel('y'); zlabel('z');
