MATLAB: How to define a vector along the meridional direction

directionvector

There is a total of 16416 points which can be defined by (x,y,z) in Cartesian coordinates or (theta, phi, r) in spherical coordinates. All of these points are located on the surface of a sphere. For each point, I want to define a vector go through it. The direction of this vector should along the meridional direction of the sphere. Does anyone know how to define 16416 vectors which oriented meridionally? Thanks.

Best Answer

If you have p=[x, y, z] (coordinates for point p), we need a vector v_merid=[vx, vy, vz] that is tangent to the sphere surface, that is, normal to the radius of the sphere, which joins the sphere centre (I assume it's in C=[0, 0, 0]) to p. As such it must be (p-C).v_merid=0, where . denotes the dot product. This translates to
[x, y, z]*[vx, vy, vz].'==0
in MATLAB syntax. The other conditions are that v_merid and (p-C) have two projections on plane x-y that are aligned (assuming, as seems reasonable, that the North and South pole of your sphere are on the z-axis). Then it must also hold
x/vx==y/vy
We need a third equation to determine the three unknowns vx, vy, vz; I will require that the vector is of unit length, so
vx^2+vy^2+vz^2==1
The system can be solved analytically. The solution is implemented in the following code, where x, y, z are vectors of the same length with the coordinates of your data points.
vx=-(x.*z)./(sqrt(x.^2+y.^2));
vy=-(y.*z)./(sqrt(x.^2+y.^2));
vz=(x.^2+y.^2)./(sqrt(x.^2+y.^2));
Here, for simplicity, I have assumed that the sphere has radius R=1; if this is not the case, divide each vector by R.
The vectors can be plotted on the sphere by the command quiver3, as follows:
quiver3(x, y, z, vx, vy, vz)
The final result (assuming you have already plotted the sphere and the points) is in the following image, where I picked 20 random points on the sphere.