set(0,'defaulttextinterpreter','latex')
clf
f=@(x,y) 1+(50-15*(x+0.5).^2-20*(y-0.5).^2)/80; %funktion
f1=@(x,y) -30*(x+0.5)/80;
f2=@(x,y) -40*(y-0.5)/80;
g=@(x,y) f(0,0)+f1(0,0).*x+f2(0,0).*y; % linearisering


%surf(x,y,z,-10*ones(size(x))) 


[x,y]=meshgrid(-1:0.1:1,-1:0.1:1);
z=f(x,y);
X=x;Y=y;Z=z;
Q = ones([size(x) 3]);
n=37;
F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
surf(x,y,z,F) % funktionsyta
alpha(0.2)
 set(gca,'XTickLabel',{'','','a','',''})
  set(gca,'YTickLabel',{'',' ','b ',' ',''}) 
   set(gca,'ZTickLabel',{'','','f(a,b)',' ',''}) 

%surf(x,y,z)
axis equal
axis([-1 1 -1 1 0 2])
v=text(-0.2,-0.2,0,'$(a,b)$','FontSize',12);   % shading interp
title('Partiell derivata, tangentplan, normal och gradient','FontSize',14 )
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 pause
i0=quiver(0,0,1,0,0,'Color','b','LineWidth',1.5);
v=text(1.1,0,0,'$\bf{i}$','FontSize',12); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause
j0=quiver(0,0,0,1,0,'Color','b','LineWidth',1.5);
v=text(0,1.1,0,'$\bf{j}$','FontSize',12);    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause
j1=plot3([0 0],[1 0],[f(0,0) f(0,0)],'black','LineWidth',1 );

i1=plot3([0 1],[0 0],[f(0,0) f(0,0)],'black','LineWidth',1 );

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause
tvi=quiver3(0,0,f(0,0),1,0,f1(0,0),0,'Color','r','LineWidth',1.5 );
% tangentvektor i-riktning

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause
tvj=quiver3(0,0,f(0,0),0,1,f2(0,0),0,'Color','r','LineWidth',1.5 );
% tangentvektor j-riktning

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pause

d1=plot3([1 1],[0 0],[f(0,0) f(0,0)+f1(0,0)],'black','LineWidth',1 );


   
te1=text(1.5,0,2,['\hspace*{-2  cm}$ f_1(a,b)= $ ' num2str(f1(0,0))],'FontSize',14);

an1=quiver3(1.5,0,1.9,1-1.5,0-0,(f(0,0)+f(0,0)+f1(0,0))/2-1.9, 0,'Color','black');






%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause

d2=plot3([0 0],[1 1],[f(0,0) f(0,0)+f2(0,0)],'black','LineWidth',1 );


te2=text(0,1.5,2,['\hspace*{-2  cm}$ f_2(a,b)= $ ' num2str(f2(0,0))],'FontSize',14);
an2=quiver3(0,1.5,1.9,0-0,1-1.5,(f(0,0)+f(0,0)+f2(0,0))/2-1.9, 0,'Color','black');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause
[x,y]=meshgrid([-1 1],[-1 1]);
z=g(x,y);
cm = colormap;
Q = ones([size(x) 3]);
n=20;
F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
tp=surf(x,y,z,F); % tangentplan
alpha(0.1)


rotate3D on
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pause
delete(te2)
delete(an2)
delete(j1)
delete(d2)

delete(i1)
delete(d1)
delete(te1)
delete(an1)

tvjtext=text(0,1.1,f(0,0)+f2(0,0)+0.1,'${\bf j}+ f_2(a,b){\bf k}$','FontSize',12);   
tvitext=text(1.1,0,f(0,0)+f1(0,0),'${\bf i}+f_1(a,b){\bf k}$','FontSize',12);   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause

t=-1:0;
normal=plot3(0+f1(0,0)*t,0+f2(0,0)*t,f(0,0)-t,'Color','r','LineWidth',1.5 );
normaltext=text(0.2,0.2,2.5,'$-f_1(a,b){\bf i} -f_2(a,b){\bf j}+{\bf k}$','FontSize',12);   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause

delete(tp)
delete(tvi)
delete(tvj)
delete(tvitext)
delete(tvjtext)
t=-1:0;
contour(X,Y,Z,[f(0,0) f(0,0)], 'r')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause
%plot(0+f1(0,0)*t,0+f2(0,0)*t,'Color','r','LineWidth',1.5 );
grad=quiver(0,0,f1(0,0),f2(0,0),0,'Color','b','LineWidth',1.5);
vg=text(-1,1,0,'$f_1(a,b){\bf i}+f_2(a,b){\bf j}$','FontSize',10); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 pause

[x,y]=meshgrid([-1 1],[-1 1]);
z=f(0,0)*ones(size(x));
cm = colormap;
Q = ones([size(x) 3]);
n=20;
F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
t0=surf(x,y,z,F); % nivåplan
text(0.8,- 0.5,f(0,0)+.1,'$z=f(a,b)$','FontSize',10); 
[x,y]=meshgrid(-1:0.1:1,-1:0.1:1);
z=f(x,y);
contour3(x,y,z,[f(0,0) f(0,0)],'r')
pause
alpha(0.1)
contour3(x,y,z,[f(0,0) f(0,0)],'r')
nz=quiver3(0,0,f(0,0),f1(0,0),f2(0,0),0,0,'Color','b');
nztext=text(-2,1.3,2.3,'$f_1(a,b){\bf i}+f_2(a,b){\bf j}+0{\bf k}$','FontSize',10);   
pek=plot3([-.9 f1(0,0)/2],[.9 f2(0,0)/2],[2,f(0,0)+f1(0,0)+f2(0,0)],'black','LineWidth',1 );
pause
delete(nz)
delete(nztext)
delete(pek)
quiver3(0,0,f(0,0),f1(0,0),f2(0,0),f1(0,0)^2+f2(0,0)^2,0,'Color','r')
pause

text(-2,1.3,2.3,'$f_1(a,b){\bf i}+f_2(a,b){\bf j}+(f_1^2(a,b)+f_2^2(a,b)){\bf k}$','FontSize',10);   

plot3([-.9 f1(0,0)/2],[.9 f2(0,0)/2],[2,f(0,0)+f1(0,0)+f2(0,0)],'black','LineWidth',1 );
pause

hold off
title('Slut','FontSize',14) 