Matlab中两个向量之间的角度
我有两点,a
和 b
。我需要计算它们之间的角度,所以我将它们视为向量。然而向量a
将始终被定义为[0 0 0]。阅读 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))
然而,由于 acos
在 theta 接近于零时存在精度问题,因此在三个方程中,只有 acos
提供了正确的解。
我应该继续使用acos
还是有更好的解决方案?
I have two points, a
and b
. 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]. Reading over the MATLAB Newsreader, "Angle between two vectors", three solutions are provided:
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.
Should I continue to use acos
or is there a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
向量有大小和方向,而
a
和b
只是空间中的坐标点。当您将a
和b
视为向量时,您就隐式地将[0 0 0]
定义为这两个向量的原点。但是,由于点a
位于[0 0 0]
,因此它将是一个长度为零的向量。如果向量的长度为零,它指向哪个方向?答案无处可寻。它不指向任何方向,因此您无法找到它与另一个向量之间的角度。
我想也许你没有很好地定义你的问题。您的坐标系是否有
[0 0 0]
之外的原点?您是否真的想找到由a
和b
形成的线与 xy 平面之间的角度?A vector has magnitude and direction, while
a
andb
are just coordinate points in space. When you treata
andb
as vectors, you are implicitly defining[0 0 0]
as the origin point for the two vectors. However, since pointa
is at[0 0 0]
, then it will be a vector with zero length.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]
? Are you actually trying to find the angle between the line formed bya
andb
and the x-y plane?错误在于设置
a = [0 0 0]
。兴趣点以原点为中心,要计算相对于向量 b 的角度,您需要指定该点行进的方向。这可以通过将a
设置为单位向量来完成。如果该点沿“x”方向行进,则
x1=1
问题已解决,忘记使用单位向量:P
The mistake is setting
a = [0 0 0]
. The point of interest is centered at the origin, and to calculate the angle with respect to vectorb
, you need to specify the direction the point is traveling. This can by done by settinga
is a unit vector.If the point is traveling in the "x" direction, then
x1=1
Problem Solved, forget to use the unit vector :P