
# Illustrating the gradient of a second order difference

This example illustrates how to compute a gradient of a second order difference and how to vizualize the result using an export to Asymptote. If we are given three points $x,y,z\in\mathbb S^2$ on the manifold $\M=\mathbb S^2$, the 2-sphere, the second order difference can be interpreted as (twice) the distance of the midpoint $\geo{x}{z}(\tfrac{1}{2})$ to $y$, since in Euclidean space $\lVert \vect{x} - 2\vect{y}+\vect{z}\rVert = 2\lVert \tfrac{1}{2}(\vect{x}+\vect{z})-\vect{y}\rVert$. The first figure illustrates this situation on the sphere for points $x,z$ on the equator and $y$ near the north pole, to be precise

1
2
3
4
5
M = Sn(2);
x = [1;0;0];
z = [0;1;0];
y = M.geopoint([0;0;1], M.geopoint(x,z,0.5),0.05);
c = M.midPoint(x,z);


The gradient $\gradM d_2 \in (T\M)^3$ with respect to $x,y,z$ of this second order absolute difference is derived in [1] and implemented within MVIRT in gradSecondOrderMidpointModel or can be computed using DxGeo and DyGeo of the manifold class as

1
2
3
4
5
% we take the inner differential (for x and z)
inner = -M.log(c,y)./M.dist(c,y);


To illustrate the vectors and the points in Matlab, we can use quiver3 and plotS2 together as

1
2
3
4
5
6
7
8
9
10
11
12
pts = cat(2,x,y,z);
quiver3(pts(1,:),pts(2,:),pts(3,:),vecs(1,:),vecs(2,:),vecs(3,:));
vecs1 = permute(cat(3,pts,vecs),[1,3,2]);
hold on
plotS2(cat(2,x,y,z,c),'o','MarkerSize',3);
pts1 = cat(2,x,y,z,c);
plotS2(M.geodesic(x,z,'t',0:1/100:1),'-');
geo1 = M.geodesic(x,z,'t',0:1/100:1);
plotS2(M.geodesic(y,c,'t',0:1/100:1),'--b');
geo2 = M.geodesic(y,c,'t',0:1/100:1);
hold off


plotting the decent direction tangent vectors $-\gradM d_2$, all 4 points, and two geodesics. The corresponding image in Asymptote is shown in the second Figure

We perform a step towards the negative gradient direction with step size $1$, i.,e.

1
2
3


yields finally the third figure

and a reduction of the second order absolute difference from about $1.4923$ (old dark green segment) to $0.3873$ (light green segment).

Creating the additional arrays of points

1
2
3
pts2 = cat(2,xN,yN,zN,M.midPoint(xN,zN));
geo3 = M.geodesic(xN,zN,'t',0:1/100:1);
geo4 = M.geodesic(yN,M.midPoint(xN,zN),'t',0:1/100:1);


we can use the exportSpherePCT2Asy export to export points, curves (connected points) and tangent vectors to Asymptote with

1
2
3
4
5
6
7
exportSpherePCT2Asy({pts1,pts2},{geo1,geo2,geo3,geo4},{vecs1},
{ [0;0;.66],[.33;0;.66],...
[.6;.6;.6],[0;0.5;0],[0.5;.6;.5],[0;1;0],...
[0;0.5;1]},