% 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);
q1=@(x,y,z) -2*y-z.^2-x;
q2=@(x,y,z) -y+2*z;
q3=@(x,y,z) 0*x-1;
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),'FaceAlpha',0.7);
 shading interp
%view(-43,-20)
axis('equal')
axis([-5,5,-5,5,-5,5])
Xlabel('x')
Ylabel('y')
Zlabel('z')
set(gca,'XTickLabel',{''})
set(gca,'YTickLabel',{''})
set(gca,'ZTickLabel',{''})

text(6,6,2.5,'${\mathbf r}= x(u,v){\mathbf i}+y(u,v){\mathbf j}+z(u,v){\mathbf k} $')
hold on
rotate3d on
 
pause % första stoppet, ytan ritad

[x,y,z]=meshgrid(linspace(-4,4,5));
qfield=quiver3(x,y,z,q1(x,y,z),q2(x,y,z),q3(x,y,z),1);
text(-5,-6,-3,'${\mathbf F}= f(x,y,z){\mathbf i}+g(x,y,z){\mathbf j}+h(x,y,z){\mathbf k} $')

 pause %andra stoppet fältvektorer ritade


h = streamline(x,y,z,q1(x,y,z),q2(x,y,z),q3(x,y,z),x,y,z);


set(h,'Color','red') 
view(3)

 pause %tredje stoppet strömlinjer ritade

delete(qfield)
delete(h)
delete(yta)
Q = ones([size(u) 3]);
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.5);
shading interp
U1=linspace(0,2*pi,7);V1=linspace(0,2*pi,7);
[u1,v1]=meshgrid(U1,V1);
sx=f1(u1,v1);sy=f2(u1,v1);sz=f3(u1,v1);
h = streamline(x,y,z,q1(x,y,z),q2(x,y,z),q3(x,y,z),sx,sy,sz);

set(h,'Color','r') 
view(3)

pause %fjärde stoppet strömlinjer med start i ytan, ritade

qxyta=q1(f1(u,v),f2(u,v),f3(u,v)); % fältet i varje gridpunkt på ytan
qyyta=q1(f1(u,v),f2(u,v),f3(u,v));
qzyta=q1(f1(u,v),f2(u,v),f3(u,v));
[Nxyta,Nyyta,Nzyta]=surfnorm(f1(u,v)',f2(u,v)',f3(u,v)');
qnyta=(qxyta.*Nxyta'+qyyta.*Nyyta'+qzyta.*Nzyta'); %normalkomposanten i varje gridpunkt(skalär)

delete(h)
ytfalt=quiver3(f1(u,v),f2(u,v),f3(u,v),qxyta,qyyta,qzyta,1.5)

pause %femte stoppet, fältet med start i ytan ritat

n1=16;m1=1;
u1=u(n1:n1+1,m1:m1+1); v1= v(n1:n1+1,m1:m1+1);
Q = ones([size(u1) 3]);
% for n=1:64;
% F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
% ruta=surf(f1(u1,v1),f2(u1,v1),f3(u1,v1),F);
% 
% % pause(.1)
% 
% delete(ruta)
% end
n=10;
F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
ruta=surf(f1(u1,v1),f2(u1,v1),f3(u1,v1),F);
%shading interp
pause %sjätte stoppet, ruta vald och markerad

figure

N3=10;
U1=linspace(u(n1,m1),u(n1+1,m1+1),N3); V1= linspace(v(n1,m1),v(n1+1,m1+1),N3);
[u1,v1]=meshgrid(U1,V1);
Q = ones([size(u1) 3]);
n=30;
F=Q;for i=1:3,F(:,:,i)=cm(n,i)*Q(:,:,i);end,
surf(f1(u1,v1),f2(u1,v1),f3(u1,v1),F,'FaceAlpha',0.7)
view(-14,22)
axis('equal')
set(gca,'XTickLabel',{''})
set(gca,'YTickLabel',{''})
set(gca,'ZTickLabel',{''})
hold on
rotate3d on
plot3(f1(U1(1),V1(1)),f2(U1(1),V1(1)),f3(U1(1),V1(1)),'k.')
plot3(f1(U1(N3),V1(1)),f2(U1(N3),V1(1)),f3(U1(N3),V1(1)),'k.')
plot3(f1(U1(N3),V1(N3)),f2(U1(N3),V1(N3)),f3(U1(N3),V1(N3)),'k.')
plot3(f1(U1(1),V1(N3)),f2(U1(1),V1(N3)),f3(U1(1),V1(N3)),'k.')
text(1.26 ,-3.8,0,'${\mathbf r}(A)$')
text(-.1 ,-3.95,.05,'${\mathbf r}(B)$')
text(0 ,-3.8,0.15,'${\mathbf r}(C)$')
text(1.25,-3.75,0.21,'${\mathbf r}(D)$')
qx=q1(f1(u1,v1),f2(u1,v1),f3(u1,v1));
qy=q2(f1(u1,v1),f2(u1,v1),f3(u1,v1));
qz=q3(f1(u1,v1),f2(u1,v1),f3(u1,v1));
qflux=quiver3(f1(u1,v1),f2(u1,v1),f3(u1,v1),qx,qy,qz);
view(-42,32)

pause  % rutan och fältet ritade

[Nx,Ny,Nz]=surfnorm(f1(u1,v1)',f2(u1,v1)',f3(u1,v1)');

Nflux=quiver3(f1(u1,v1),f2(u1,v1),f3(u1,v1),Nx,Ny,Nz,1.5);

pause %normalvektorer ritade

qn=(qx.*Nx+qy.*Ny+qz.*Nz);
qnflux=quiver3(f1(u1,v1),f2(u1,v1),f3(u1,v1),qn.*Nx,qn.*Ny,qn.*Nz);
text(-0.3,-4,0.7,'Normalkomposant $= {\mathbf q}{\scriptscriptstyle \stackrel{\bullet}{{}}}\hat{{\mathbf N}}$','FontSize',16)
view(-42,32)

pause %normalkomposanter (vektorer) ritade 

text(0.6,-4,0.15,'$dS $','FontSize',16)
text(0.2,-4,-0.1,'$dS=\left\|{\mathbf r}_u \times {\mathbf r}_v\right\| $','FontSize',16)

text(0.2,-4,-0.2,' Fl\"ode $= {\mathbf q}{\scriptscriptstyle \stackrel{\bullet}{{}}}\hat{{\mathbf N}} dS$','FontSize',16)
view(-42,32)

delete(qflux)
delete(Nflux)

pause % ruta med normalkomposanter

figure(1)
delete(ytfalt,ruta,yta)
yta=surf(f1(u,v),f2(u,v),f3(u,v),qnyta,'FaceAlpha',0.5);
shading interp



hold off


