Trying to simplify code to see if 3 vectors are at right angle. Unsure of outcome.

4 views (last 30 days)
I have an older code, which is rather long, and just to see if any angle is degrees, seems unnecessary, so I have tried to use "cross", but is is unsure of the output.
The vectors are at right angle, when I rotate the plot.
Old code:
P = [3 2 4]
Q = [5 0 2]
R = [6 4 5]
% distance between points √((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)
sides = @(x1, x2, y1, y2, z1, z2) sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)
PQ = sides(P(1), Q(1), P(2), Q(2), P(3), Q(3))
QR = sides(Q(1), R(1), Q(2), R(2), Q(3), R(3))
RP = sides(R(1), P(1), R(2), P(2), R(3), P(3))
Ra = rat(RP)
Rb = rat(QR)
Rc = rat(PQ)
% angle a^2 + b^2 = c^2 * 180/pi
angle = @(x1, x2, x3)(180/pi) * acos(-(x1^2-(x2^2+x3^2))/(2*x2*x3))
AngleA = angle(RP,QR,PQ)
AngleB = angle(QR,PQ,RP)
AngleC = angle(PQ,QR,RP)
% plot
x = [P(1) Q(1) R(1) P(1)]
y = [P(2) Q(2) R(2) P(2)]
z = [P(3) Q(3) R(3) P(3)]
xmax = max(x)
xmin = min(x)
ymax = max(y)
ymin = min(y)
zmax = max(z)
zmin = min(z)
figure('defaultAxesFontSize',18)
plot3(x,y,z)
the new code:
P = [3 2 4]
Q = [5 0 2]
R = [6 4 5]
PQ2 = cross(P,Q)
QR2 = cross(Q,R)
RP2 = cross(R,P)
Running the new shows that one element is 0, with the other 8 not.
Math says if the cross-product is = 0, then the angle is right.
Is this single Zero a sign of the 90-degree angle? Feels so insignificant (pun not intended).
Jessica

Answers (2)

Alan Stevens
Alan Stevens on 7 Dec 2020
The dot product is zero if the vectors are at right angles.
help dot
  1 Comment
Jessica
Jessica on 9 Dec 2020
dot - ok
but... it still does not give any zero...
P = [3 2 4]
Q = [5 0 2]
R = [6 4 5]
PQ2 = dot(P,Q) % returns 23
QR2 = dot(Q,R) % returns 40
RP2 = dot(R,P) % returns 46
and there is a right angle, as is calculated by the longer code.

Sign in to comment.


Bruno Luong
Bruno Luong on 9 Dec 2020
>> dot(Q-P,R-P)
ans =
0
So PQ is right angle with PR, etc...

Categories

Find more on Elementary Math in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!