Saturday, September 24, 2016

HP Prime: Mathematical Calculations with 3-Dimensional Vectors


HP Prime:  Mathematical Calculations with 3-Dimensional Vectors

Note: all examples are calculated in Degrees mode.

Rectangular to Spherical Coordinates

The program RECT2SPH converts the coordinates [x, y, z] to [r, θ, ϕ].

Syntax:  RECT2SPH([x, y, z])

HP Prime RECT2SPH:  Rectangular to Spherical Coordinates
EXPORT RECT2SPH(v)
BEGIN
// [x,y,z]→
LOCAL r,θ,φ,x,y,z;
x:=v(1); y:=v(2); z:=v(3);
r:=√(x^2+y^2+z^2);
θ:=ATAN(y/x);
φ:=ACOS(z/r);
RETURN [r,θ,φ];
END;

Example:  RECT2SPH([2, 3, 4]) return [5.38516480713, 56.309932474, 42.0311137741]

Spherical to Rectangular Coordinates

The program SPH2RECT converts the coordinates [r, θ,  φ] to [x, y, y].

Syntax:  SPH2RECT([r, θ,  φ])

HP Prime SPH2RECT:  Spherical Coordinates to Rectangular Coordinates
EXPORT SPH2RECT(v)
BEGIN
// [r,θ,φ]→
LOCAL r,θ,φ,x,y,z;
r:=v(1); θ:=v(2); φ:=v(3);
x:=r*COS(θ)*SIN(φ);
y:=r*SIN(θ)*SIN(φ);
z:=r*COS(φ);
RETURN [x,y,z];
END;

Example:  SPH2RECT([6, 30, 48]) returns [3.86149378532, 2.22943447643, 4.01478363815]

Linear Distance

The program LIN3DIST is the linear distance between two three-dimensional points.  The coordinates are Cartesian.  Enter each coordinate point separately. 

Syntax:  LIN3DIST(x1, x2, y1, y2, z1, z2)

HP Prime LIN3DIST:  Linear distance between coordinates
EXPORT LIN3DIST(x1,x2,y1,y2,z1,z2)
BEGIN
// linear distance
LOCAL d;
d:=√((x2-x1)^2+(y2-y1)^2
+(z2-z1)^2);
RETURN d;
END;

Example:  Find the linear distance between points (2,3,-7) and (-1,8,2).
Input:  LIN3DIST(2, -1, 3, 8, -7, 2) returns 10.7238052948.

Spherical Distance (Arc Length)

The program SPH3DIST is the spherical distance between two three-dimensional points that share the same radius.  This is similar to the great circle distance.

Syntax:  SPH3DIST(r, φ1, φ2 ,λ1 ,λ2)

HP Prime SPH3DIST:  Spherical distance between coordinates
EXPORT SPH3DIST(r,φ1,φ2,λ1,λ2)
BEGIN
// Spherical Distance
LOCAL d;
d:=ACOS(SIN(φ1)*SIN(φ2)+
COS(φ1)*COS(φ2)*COS(λ1-λ2));
d:=d*r;
RETURN d;
END;

Example:  Find the spherical distance between points φ1 = 40°, φ2 = 64°, λ1 = -18°, λ2 = 33°.  The radius is 14.

SPH3DIST(14, 40, 64, -18, 33) returns 519.226883434
   
Angle between Two Three-Dimensional Coordinates

The program VANGLE calculates the angle between two points.  Both points are entered in vector form.

Syntax:  VANGLE([x1,y1,z1], [x2,y2,z2])

HP Prime VANGLE:  Angle between two coordinates
EXPORT VANGLE(v1,v2)
BEGIN
// Angle between 2 vectors
LOCAL θ;
θ:=ACOS(DOT(v1,v2)/
(ABS(v1)*ABS(v2)));
RETURN θ;
END;

Example:  Find the angle between [5,4,5] and [2,0,-3]. 
VANGLE([5,4,5],[2,0,-3]) returns 99.8283573577°

Rotating a Cartesian Coordinate Vector

The program ROT3X, ROT3Y, and ROT3Z rotates the three-dimensional vector [x, y, z] with respect to the x-axis (ax),  respect to the y-axis (ay), and respect to the z-axis (az), respectively.

Syntax:  ROT3X(v, ax),  ROT3Y(v, ay),  ROT3Z(v, az)

Caution:  the result will be a matrix instead of a vector

HP Prime: ROT3X
EXPORT ROT3X(v,ax)
BEGIN
// [x,y,z],θx
v:=TRN(v);
v:=[[1,0,0],[0,COS(ax),−SIN(ax)],
[0,SIN(ax),COS(ax)]]*v;
RETURN TRN(v);
END;

HP Prime:  ROT3Y
EXPORT ROT3Y(v,ay)
BEGIN
// [x,y,z],θy
v:=TRN(v);
v:=[[COS(ay),0,SIN(ay)],
[0,1,0],[−SIN(ay),0,COS(ay)]]*v;
RETURN TRN(v);
END;

HP Prime:  ROT3Z
EXPORT ROT3Z(v,az)
BEGIN
// [x,y,z],θz
v:=TRN(v);
v:=[[COS(az),−SIN(az),0],
[SIN(az),COS(az),0],[0,0,1]]*v;
RETURN TRN(v);
END;

Example:  Rotate the vector [2, 3, 4] 30°, with respect to the x-axis, y-axis, and z-axis, separately and respectfully.

ROT3X([2, 3, 4], 30) returns [[ 2, 0.598076211352, 4.96410161514 ]]

ROT3Y([2, 3, 4], 30) returns [[ 3.73205080757, 3, 2.46410161514 ]]

ROT3Z([2, 3, 4], 30) returns [[ 0.232050807568, 3.59807621135, 4]]


This blog is property of Edward Shore, 2016