%%
%% 1.3  Metric of a Mexican hat (Computational example)

clear all
syms r s phi real

%
%% 1 Parameterization of the hat and the path over the hat
% We parameterize a Mexican hat by
% \[\vec r = \left( {\begin{array}{*{20}{c}}
%   {r\cos \varphi } \\
%   {r\sin \varphi } \\
%   {{r^4} - {r^4}}
% \end{array}} \right)\] mit r\in[0,0.9], \ \varphi\in[0, 2\pi]
% A path over the hat is parametrized by  \varphi \to 0,\  r\to s\in[-0.9,0.9]
% \[\vec {r_w} = \left( {\begin{array}{*{20}{c}}
%   s \\
%   0 \\
%   {{s^4} - {s^4}}
% \end{array}} \right)\]


r_v=[r*cos(phi);r*sin(phi);r^4-r^2]
r_w=sube(r_v,[phi==0,r==s])

%
%% 2 Graphic representation (includes path over the hat)

hold off
fsurf(r_v(1),r_v(2),r_v(3),[0 2*pi 0 0.9])
hold on
fplot3(r_w(1),r_w(2),r_w(3),0.9*[-1 1],'r','LineWidth',3)

%
%% 3 Jacobian J und metric G
% J=\left({
% \partial x_i \over \partial q_j
% }\right), G=J'J

J=jacobian(r_v,[r,phi])
G=simplify(J'*J)

%
%% 4  Surface of the Mexican hat
% Invariant volume element V_{inv}=\sqrt{(det(G))}
% Surface O=
% \int \limits_0^{2\pi}
% \int \limits_0^{0.9}V_{inv}
% drd\varphi

V_inv=sqrt(det(G))
O=vpaintegral(int(V_inv,phi,0,2*pi),r,0,0.9)

%
%% 5 Path length across the hat
% J_s=\left({
% \partial r_{wi}\over\partial s
% }\right )
% , S=\int \limits_{-0.9}^{0.9}\sqrt{det(G_s)}ds
% =\int \limits_{-0.9}^{0.9}\sqrt{{J_s}'J_s}ds

J=jacobian(r_w)
S=vpaintegral(sqrt(J'*J),-0.9,0.9)