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

4 ビュー (過去 30 日間)
Jessica
Jessica 2020 年 12 月 7 日
回答済み: Bruno Luong 2020 年 12 月 9 日
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

回答 (2 件)

Alan Stevens
Alan Stevens 2020 年 12 月 7 日
The dot product is zero if the vectors are at right angles.
help dot
  1 件のコメント
Jessica
Jessica 2020 年 12 月 9 日
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.

サインインしてコメントする。


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

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by