clf
[x,y]=meshgrid(-2:0.1:2);
f=@(x,y) 3-sin(x).*cos(y);
z=f(x,y);
surf(x,y,z)
alpha(0.6)
shading interp
hold on
contour(x,y,z,20)
rotate3d on
a=-pi/4;b=-pi/4;
az =-31;
el =17;

view([az,el])
%dx=-cos(a).*cos(b);dy=sin(a).*sin(b);
%r=sqrt(dx^2+dy^2);
%quiver(a,b,dx/r,dy/r,'r','LineWidth',1.5)
%quiver3(a,b,f(a,b),dx/r,dy/r,r,'r','LineWidth',1.5)
plot3([a a],[b b],[0 f(a,b)],':','LineWidth',1.5)
t=3*pi/4:0.05:3*pi/4+2*pi;
axis equal
%title('The function $\int f(\theta) d\theta$','FontSize',20, );
plot3([a a],[b b],[0 f(a,b)],':','LineWidth',1.5)


 for n=1:length(t)
     if n==1
        cy1=quiver(a,b,cos(t(n)),sin(t(n)),0,'Color','b','LineWidth',1.5);
      % cy0=quiver3(a,b,f(a,b),cos(t(n)),sin(t(n)),-cos(t(n))/2+sin(t(n))/2,'Color','black','LineWidth',2.5);
        cy0=plot3([a a+cos(t(n))],[b b+sin(t(n))],[f(a,b) f(a,b)-cos(t(n))/2+sin(t(n))/2],'LineWidth',1.5 );
        cy2=plot3([a a+cos(t(n))],[b b+sin(t(n))],[f(a,b) f(a,b)],'b','LineWidth',1 );
        cy3=plot3([a+cos(t(n)) a+cos(t(n))],[b+sin(t(n)) b+sin(t(n))],[f(a,b) f(a,b)-cos(t(n))/2+sin(t(n))/2],'b','LineWidth',1 );
        te=text(0,0,4.5,['$ D_vf(a,b)= $ ' num2str(-cos(t(n))/2+sin(t(n))/2)],'FontSize',14 ,'Interpreter','latex');
        v=text(a+1.1*cos(t(n)),b+1.1*sin(t(n)),0,'v','FontSize',14,'Interpreter','latex'); 
        an=quiver3(0,0,4.2,a+cos(t(n)),b+sin(t(n)),(f(a,b)+f(a,b)-cos(t(n))/2+sin(t(n))/2)/2-4.2, 0,'Color','black');
        pause
        delete(te)
        delete(v)
        delete(an)
        delete(cy2)
        delete(cy3)

     elseif(abs(n-length(t)/4) <=1)||(abs(n-length(t)/2) <=1)||(abs(n-3*length(t)/4) <=1) 
          cy1=quiver(a,b,cos(t(n)),sin(t(n)),0,'Color','b','LineWidth',1.5);
      % cy0=quiver3(a,b,f(a,b),cos(t(n)),sin(t(n)),-cos(t(n))/2+sin(t(n))/2,'b','LineWidth',1.5);
        cy0=plot3([a a+cos(t(n))],[b b+sin(t(n))],[f(a,b) f(a,b)-cos(t(n))/2+sin(t(n))/2],'LineWidth',1.5 );
        cy2=plot3([a a+cos(t(n))],[b b+sin(t(n))],[f(a,b) f(a,b)],'b','LineWidth',1 );
        cy3=plot3([a+cos(t(n)) a+cos(t(n))],[b+sin(t(n)) b+sin(t(n))],[f(a,b) f(a,b)-cos(t(n))/2+sin(t(n))/2],'b','LineWidth',1 );
        te=text(0,0,4.5,['$ D_vf(a,b)= $ ' num2str(-cos(t(n))/2+sin(t(n))/2)],'FontSize',14 ,'Interpreter','latex');
        v=text(a+1.1*cos(t(n)),b+1.1*sin(t(n)),0,'v','FontSize',12,'Interpreter','latex');
        an=quiver3(0,0,4.2,a+cos(t(n)),b+sin(t(n)),(f(a,b)+f(a,b)-cos(t(n))/2+sin(t(n))/2)/2-4.2, 0,'Color','black');
        pause
        delete(te)
        delete(v)
        delete(an)
      % set(cy0,'Color','b', 'LineWidth',0.1);
      % set(cy1,'Color','b', 'LineWidth',0.1);
        delete(cy0)
        delete(cy1)
        delete(cy2)
        delete(cy3)
       
    else
        cy1=quiver(a,b,cos(t(n)),sin(t(n)),0,'Color','b','LineWidth',1.5);
      % cy0=quiver3(a,b,f(a,b),cos(t(n)),sin(t(n)),-cos(t(n))/2+sin(t(n))/2,'b','LineWidth',1.5);
        cy0=plot3([a a+cos(t(n))],[b b+sin(t(n))],[f(a,b) f(a,b)-cos(t(n))/2+sin(t(n))/2],'LineWidth',1.5 );
        cy2=plot3([a a+cos(t(n))],[b b+sin(t(n))],[f(a,b) f(a,b)],'b','LineWidth',1 );
        cy3=plot3([a+cos(t(n)) a+cos(t(n))],[b+sin(t(n)) b+sin(t(n))],[f(a,b) f(a,b)-cos(t(n))/2+sin(t(n))/2],'b','LineWidth',1 );
        te=text(0,0,4.5,['$ D_vf(a,b)= $ ' num2str(-cos(t(n))/2+sin(t(n))/2)],'FontSize',14 ,'Interpreter','latex');
        v=text(a+1.1*cos(t(n)),b+1.1*sin(t(n)),0,'v','FontSize',12,'Interpreter','latex');
        an=quiver3(0,0,4.2,a+cos(t(n)),b+sin(t(n)),(f(a,b)+f(a,b)-cos(t(n))/2+sin(t(n))/2)/2-4.2, 0,'Color','black');
        pause(0.1)
        delete(te)
        delete(v)
        delete(an)
      % set(cy0,'Color','b', 'LineWidth',0.1);
      % set(cy1,'Color','b', 'LineWidth',0.1);
        delete(cy0)
        delete(cy1)
        delete(cy2)
        delete(cy3)
        
    end
   % set(cy0, 'LineStyle', 'none' );
   % set(cy1, 'LineStyle', 'none' );
end 
te=text(0,0,4.5,['    $ D_vf(a,b)= $ ' num2str(-cos(t(length(t)))/2+sin(t(length(t)))/2)],'FontSize',14,'Interpreter','latex');
cy2=plot3([a a+cos(t(n))],[b b+sin(t(n))],[f(a,b) f(a,b)],'b','LineWidth',1 );
cy3=plot3([a+cos(t(n)) a+cos(t(n))],[b+sin(t(n)) b+sin(t(n))],[f(a,b) f(a,b)-cos(t(n))/2+sin(t(n))/2],'b','LineWidth',1 );
an=quiver3(0,0,4.2,a+cos(t(length(t))),b+sin(t(length(t))),(f(a,b)+f(a,b)-cos(t(length(t)))/2+sin(t(length(t)))/2)/2-4.2, 0,'Color','black');
v=text(a+cos(t(length(t))),b+sin(t(length(t))),0,'v','FontSize',12,'Interpreter','latex');        
%dx=-1/sqrt(5);dy=2/sqrt(5);
%quiver(a,b,dx,dy,'g','LineWidth',1.5)
%quiver3(a,b,f(a,b),dx,dy,3/(2*sqrt(5)),'g','LineWidth',1.5)
%dx=0;dy=1;
%quiver(a,b,dx,dy,'b','LineWidth',1.5)
%quiver3(a,b,f(a,b),dx,dy,1/2,'b','LineWidth',1.5)
%axis equal
