Matlab中两个向量之间的角度

发布于 2024-10-05 11:11:44 字数 614 浏览 4 评论 0原文

我有两点,ab。我需要计算它们之间的角度,所以我将它们视为向量。然而向量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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所谓喜欢 2024-10-12 11:11:44

向量有大小和方向,而ab 只是空间中的坐标点。当您将 ab 视为向量时,您就隐式地将 [0 0 0] 定义为这两个向量的原点。但是,由于点 a 位于 [0 0 0],因此它将是一个长度为零的向量。

如果向量的长度为零,它指向哪个方向?答案无处可寻。它不指向任何方向,因此您无法找到它与另一个向量之间的角度。

我想也许你没有很好地定义你的问题。您的坐标系是否有 [0 0 0] 之外的原点?您是否真的想找到由 ab 形成的线与 xy 平面之间的角度?

A vector has magnitude and direction, while a and b are just coordinate points in space. When you treat a and b as vectors, you are implicitly defining [0 0 0] as the origin point for the two vectors. However, since point a 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 by a and b and the x-y plane?

小矜持 2024-10-12 11:11:44

错误在于设置a = [0 0 0]。兴趣点以原点为中心,要计算相对于向量 b 的角度,您需要指定该点行进的方向。这可以通过将 a 设置为单位向量来完成。

如果该点沿“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

问题已解决,忘记使用单位向量: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 vector b, you need to specify the direction the point is traveling. This can by done by setting a is a unit vector.

If the point is traveling in the "x" direction, then 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文