登录论坛

查看完整版本 : Matlab中两个向量之间的角度


poster
2019-12-10, 20:48
我有两点, a和b 。我需要计算它们之间的角度,因此我将它们视为矢量。但是向量a将始终定义为[0 0 0]。通过阅读MATLAB Newsreader的“ 两个向量之间的角度 (http://www.mathworks.com/matlabcentral/newsreader/view_thread/151925) ”,提供了三种解决方案:

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还是有更好的解决方案?



回答:

错误是设置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



更多&回答... (https://stackoverflow.com/questions/4336835)