% yta, area och ytintegral
close all
set(0,'defaulttextinterpreter','latex')
a=3;b=1;N=31;M=21;
U=linspace(0,2*pi,N);V=linspace(0,2*pi,M);
[u,v]=meshgrid(U,V);
f1=@(u,v) (a+b*cos(u)).*cos(v);
 f2=@(u,v) (a+b*cos(u)).*sin(v);
 f3=@(u,v) b*sin(u);
q=@(x,y,z) y-4*z;
Q = ones([size(u) 3]);
cm=colormap;
n=37;
F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
yta=surf(f1(u,v),f2(u,v),f3(u,v),F,'FaceAlpha',0.7);
% shading interp
% view(-43,-20)
% axis([-11,21,-1,9,0,7])
hold on
%i=1;
%plot3(f1(u(:,i),v(:,i)),f2(u(:,i),v(:,i)),f3(u(:,i),v(:,i)),'k')
%i=N;
%plot3(f1(u(:,i),v(:,i)),f2(u(:,i),v(:,i)),f3(u(:,i),v(:,i)),'k')
%j=1;
%plot3(f1(u(j,:),v(j,:)),f2(u(j,:),v(j,:)),f3(u(j,:),v(j,:)),'k')
%j=M;
%plot3(f1(u(j,:),v(j,:)),f2(u(j,:),v(j,:)),f3(u(j,:),v(j,:)),'k')
Xlabel('x')
Ylabel('y')
Zlabel('z')
axis('equal')
text(2,4,1,'${\mathbf r}= x(u,v){\mathbf i}+y(u,v){\mathbf j}+z(u,v){\mathbf k} $')
text(2,4,3,'$x(u,v) = (a+b*cos(u)).*cos(v)$')
text(2,4,2.5,'$y(u,v) = (a+b*cos(u)).*sin(v)$')
text(2,4,2,'$z(u,v) = b*sin(u)$')
pause
delete(yta)
surf(f1(u,v),f2(u,v),f3(u,v),q(f1(u,v),f2(u,v),f3(u,v)),'FaceAlpha',0.7);
shading interp
text(-2,4,2,'$f(x,y,z) = y-4z$')
% colorbar
hold off
pause

figure
subplot(1,2,2)
surf(f1(u,v),f2(u,v),f3(u,v),q(f1(u,v),f2(u,v),f3(u,v)),'FaceAlpha',0.5)
shading interp
axis 'equal'

%axis([-11,21,-1,9,0,7])
hold on
% i=1;
% plot3(f1(u(:,i),v(:,i)),f2(u(:,i),v(:,i)),f3(u(:,i),v(:,i)),'k')
% i=N;
% plot3(f1(u(:,i),v(:,i)),f2(u(:,i),v(:,i)),f3(u(:,i),v(:,i)),'k')
% j=1;
% plot3(f1(u(j,:),v(j,:)),f2(u(j,:),v(j,:)),f3(u(j,:),v(j,:)),'k')
% j=M;
% plot3(f1(u(j,:),v(j,:)),f2(u(j,:),v(j,:)),f3(u(j,:),v(j,:)),'k')


subplot(1,2,1)
i=1;
plot(u(:,i),v(:,i),'r')
axis([-1,8,-1,7])
hold on
i=N;
plot(u(:,i),v(:,i),'r')
j=1;
plot(u(j,:),v(j,:),'b')
j=M;
plot(u(j,:),v(j,:),'b')

 pause
 
%rutnät

for i = 1:5:N;
subplot(1,2,1) 
plot(u(:,i),v(:,i),'r')
subplot(1,2,2)
plot3(f1(u(:,i),v(:,i)),f2(u(:,i),v(:,i)),f3(u(:,i),v(:,i)),'r')

 pause(.5)

end
for j = 1:4:M;
subplot(1,2,1) 
plot(u(j,:),v(j,:),'b')
subplot(1,2,2)
plot3(f1(u(j,:),v(j,:)),f2(u(j,:),v(j,:)),f3(u(j,:),v(j,:)),'b')

pause(.5)

end
pause

subplot(1,2,1)
u1=u(5:9,6:11); v1= v(5:9,6:11);
Q = ones([size(u1) 3]);
n=20;
F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
surf(u1,v1,zeros(size(u1)),F)
shading interp

subplot(1,2,2)

for i=1:10
su=surf(f1(u1,v1),f2(u1,v1),f3(u1,v1),F,'FaceAlpha',0.8);
shading interp
pause(.5)
delete(su)
pause(.5)
end

surf(f1(u1,v1),f2(u1,v1),f3(u1,v1),F,'FaceAlpha',0.8);
%surf(f1(u1,v1),f2(u1,v1),f3(u1,v1),q(f1(u1,v1),f2(u1,v1),f3(u1,v1)),'FaceAlpha',0.7)
%surf(f1(u,v),f2(u,v),f3(u,v),q(f1(u,v),f2(u,v),f3(u,v)),'FaceAlpha',0.7);
shading interp



subplot(1,2,1)
surf(u1,v1,zeros(size(u1)),F)
shading interp
pause

figure

subplot(1,2,2)
U1=linspace(u(5,6),u(6,7),20); V1= linspace(v(5,6),v(6,7),20);
[u1,v1]=meshgrid(U1,V1);
Q = ones([size(u1) 3]);
n=20;
F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
surf(f1(u1,v1),f2(u1,v1),f3(u1,v1),q(f1(u1(200),v1(200)),f2(u1(200),v1(200)),f3(u1(200),v1(200)))*ones(size(u1)),'FaceAlpha',0.7)
%surf(f1(u1,v1),f2(u1,v1),f3(u1,v1),q(f1(u1,v1),f2(u1,v1),f3(u1,v1)),'FaceAlpha',0.7)
hold on
plot3(f1(U1(1),V1(1)),f2(U1(1),V1(1)),f3(U1(1),V1(1)),'k.')
plot3(f1(U1(20),V1(1)),f2(U1(20),V1(1)),f3(U1(20),V1(1)),'k.')
plot3(f1(U1(20),V1(20)),f2(U1(20),V1(20)),f3(U1(20),V1(20)),'k.')
plot3(f1(U1(1),V1(20)),f2(U1(1),V1(20)),f3(U1(1),V1(20)),'k.')
text(1 ,3.4,0.86,'${\mathbf r}(A)$')
text(0.1 ,3.4,0.86 ,'${\mathbf r}(B)$')
text(.1 ,3.6,0.94,'${\mathbf r}(C)$')
text(1 ,3.5,0.94,'${\mathbf r}(D)$')
shading interp

subplot(1,2,1)
plot(u1(1),v1(1),'k.',u1(400),v1(1),'k.',u1(400),v1(400),'k.',u1(1),v1(400),'k.')
 axis([.9, 1.4, 1.15,1.65])
hold on
surf(u1,v1,zeros(size(u1)),F)
shading interp
text(.95,1.22,'$A=(u,v)$')
text(1.25,1.22,'$B=(u+du,v)$')
text(.95,1.6,'$C=(u,v+dv)$')
text(1.25,1.6,'$D=(u+du,v+dv)$')


