简体   繁体   English

Matlab中两个向量之间的角度

[英]Angle between two vectors in Matlab

I have two points, a and b . 我有两点, ab I need to calculate the angle between them, so I treat them like vectors. 我需要计算它们之间的角度,因此我将它们视为矢量。 However vector a will always be defined as [0 0 0]. 但是向量a将始终定义为[0 0 0]。 Reading over the MATLAB Newsreader, " Angle between two vectors ", three solutions are provided: 通过阅读MATLAB Newsreader的“ 两个向量之间的角度 ”,提供了三种解决方案:

x1 = 0;
y1 = 0;
z1 = 0;
x2 = 0;
y2 = 1;
z2 = 0;
a = [x1,y1,z1]; b= [x2,y2,z2];

theta = rad2deg(atan2(norm(cross(a,b)),dot(a,b)))
theta = rad2deg(acos(dot(a,b)))
theta = rad2deg(atan2(x1*y2-x2*y1,x1*x2+y1*y2)) 

However as acos has accuracy problems as theta nears zero, yet out of the three equations, only acos provides the correct solution. 但是,由于acos存在精度问题,因为theta接近零,但在三个方程中,只有acos提供了正确的解决方案。

Should I continue to use acos or is there a better solution? 我应该继续使用acos还是有更好的解决方案?

A vector has magnitude and direction, while a and b are just coordinate points in space. 向量具有大小和方向,而ab只是空间中的坐标点。 When you treat a and b as vectors, you are implicitly defining [0 0 0] as the origin point for the two vectors. 当将ab视为向量时,您隐式地将[0 0 0]定义为两个向量的起点。 However, since point a is at [0 0 0] , then it will be a vector with zero length. 但是,由于点a位于[0 0 0] ,则它将是长度为零的向量。

If a vector has zero length, which direction does it point in? 如果向量的长度为零,则指向哪个方向? The answer is nowhere. 答案无处。 It doesn't point in any direction, and thus you can't find the angle between it and another vector. 它没有指向任何方向,因此您无法找到它与另一个矢量之间的角度。

I think maybe you've defined your problem poorly. 我认为您可能对问题的定义不佳。 Does your coordinate system have an origin other than [0 0 0] ? 您的坐标系是否有[0 0 0]以外的原点? Are you actually trying to find the angle between the line formed by a and b and the xy plane? 您实际上是在寻找由ab形成的线与xy平面之间的角度吗?

The mistake is setting a = [0 0 0] . 错误是设置a = [0 0 0] The point of interest is centered at the origin, and to calculate the angle with respect to vector b , you need to specify the direction the point is traveling. 兴趣点以原点为中心,并且要计算相对于矢量b的角度,您需要指定该点的行进方向。 This can by done by setting a is a unit vector. 可以通过设置a为单位矢量来完成。

If the point is traveling in the "x" direction, then x1=1 如果该点沿“ x”方向行进,则x1=1

x1 = 1;
y1 = 0;
z1 = 0;
x2 = 0;
y2 = 1;
z2 = 0;
a = [x1,y1,z1]; b= [x2,y2,z2];

theta = rad2deg(atan2(norm(cross(a,b)),dot(a,b)))
theta = rad2deg(acos(dot(a,b)))
theta = rad2deg(atan2(x1*y2-x2*y1,x1*x2+y1*y2))

theta =
    90
theta =
    90
theta =
    90

Problem Solved, forget to use the unit vector :P 解决了问题,忘记使用单位矢量:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM