%% Chapter 9. Multidimensional Calculus
% This is the script for the examples and illustrations in the vector calculus chapter.
%% Line Integral Examples

syms x y z t real
F = [cos(y), cos(z) - x*sin(y), -y*sin(z)];
gam1 = [0, 1-t, t]; gam2 = [0, cos(t), sin(t)];
int1 = int(subs(F, [x,y,z], gam1)*transpose(diff(gam1, t)), t, 0, 1)
int2 = int(subs(F, [x,y,z], gam2)*transpose(diff(gam2, t)), t, 0, pi/2)

%

curl(F, [x,y,z])
F - jacobian(x*cos(y) + y*cos(z), [x,y,z])

%
%% Figure 9.1

fill([0,3,3,0,0], [0,0,1,1,0], [1 0.4 0.6]), hold on
plot([0,3],[0,0], 'k', 'LineWidth', 2)
plot([0,3],[1,1], 'k', 'LineWidth', 2)
plot([0,0],[0,1], 'k--', 'LineWidth', 2)
plot([3,3],[0,1], 'k--', 'LineWidth', 2); 
plot([1.4,1.6,1.4],[-0.05,0,0.05], 'k', 'LineWidth', 2)
plot([1.6,1.4,1.6],[0.95,1.0,1.05], 'k', 'LineWidth', 2)
xticks([0,3]); yticks([0,1])
axis equal
xticklabels({'x_0','x_1'})
yticklabels({'y_0','y_1'})
text(1.4,0.45,'R','FontSize',14,'FontAngle','italic'), hold off

%
%% Stokes' Theorem

syms u v
fmesh((4+cos(u))*cos(v), (4+cos(u))*sin(v), sin(u), [0, 2*pi, 0, 3*pi/2])
axis equal, hold on
fplot3(4+cos(u), sym(0), sin(u), [0, 2*pi], 'LineWidth', 2, 'Color', 'k')
fplot3(sym(0), -(4+cos(u)), sin(u), [0, 2*pi], 'LineWidth', 2, 'Color', 'k')
quiver3(4, -4, 0, -4, -1, 0, 'Color', 'k')
quiver3(4, -4, 0, 1, 4, 0, 'Color', 'k') 
quiver3(1, 4, 1, 0, 0, 1, 'LineWidth', 2, 'MaxHeadSize', .5, 'Color', 'k')
text(4, -4.2, 0, '\it C') 
text(-5, 0, 1, '\Sigma') 
text(1, 4, 2, '\bf n') 
hold off
grid off
axis off

%
%% Vector potentials

syms x y z A1(x,y,z) A2(x,y,z) A3(x,y,z)
F = curl([A1(x,y,z), A2(x,y,z), A3(x,y,z)], [x,y,z]);
divergence(F, [x,y,z])

%

F = [-sin(y)-cos(x)*cos(z)*sin(y), cos(y)*cos(z), ...
    sin(y)*sin(z)-sin(x)*sin(y)*sin(z)];
simplify(divergence(F, [x,y,z]))
A = transpose(vectorPotential(F, [x,y,z]))

%
% Check:

simplify(transpose(curl(A, [x,y,z])) - F)