在matlab中旋转一个向量并检查该角度
rotating a vector in matlab and check that angle
我想在 MATLAB 中旋转一个向量,并在检查原始向量和旋转向量之间的角度后立即旋转:
v = [-1 -12 5]; %arbitrarily rotated vector
theta =30; %arbitrary angle to rotate with
R = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]; %rotate around Z axis
vR = R*v'; %calculate the new vector
angle=atan2d(norm(cross(v,vR)),dot(v,vR));
%the angle between the old and rotated vector, also do normalisation before.
%atan2d is better to resolve extremely small angle
angle =
27.6588
%THIS is the problem
如您所见,我旋转了 30°,但回头看时发现不一样了。
你实际上计算的不是同一个角度。考虑输入向量为 v = [0, 0, 1](即垂直线)的情况。如果将垂直线绕 z 轴旋转 30 度,则会再次得到相同的垂直线,因此 vR = [0, 0, 1]。根据您的分析,v 和 vR 之间的角度将为 0,因为您正在计算相交于某处的两个向量之间的实际角度。
所以,如果您想计算两个向量之间的角度,那么我相信您的代码是正确的。但是,如果您想计算特定坐标系(即 z 轴)中的旋转量,那么在使用您的公式之前,您必须先将 v 和 vR 投影到 x-y 平面上:
v = [-1 -12 5]; %arbitrarily rotated vector
theta =30; %arbitrary angle to rotate with
R = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]; %rotate around Z axis
vR = R*v'; %calculate the new vector
angle=atan2d(norm(cross(v,vR)),dot(v,vR)); %calculate the angle between the vectors
% copy over the vectors and remove the z-component to project onto the x-y
% plane
v_xy = v;
v_xy(3) = 0;
vR_xy = vR;
vR_xy(3) = 0;
angle_xy=atan2d(norm(cross(v_xy,vR_xy)),dot(v_xy,vR_xy)); %calculate the angle between the vectors in the xy-plane
编辑:请注意,您仍然无法获得垂直线情况的角度(那里的解决方案存在奇点)。此外,我的建议仅适用于 z 轴旋转的情况。要对任何通用旋转轴执行此操作只需要更多数学运算:
假设您有一个由单位向量 a
定义的轴,然后围绕轴 a
旋转向量 v
以获得新向量 vR
。将 v
和 vR
投影到 a
正常的平面上,您会得到向量 p
和 pR
:
p = v - dot(v,a)*a;
pR = vR - dot(vR,a)*a;
然后你根据你的公式求出这些投影向量之间的夹角:
angle = atan2d(norm(cross(p,pR)),dot(p,pR));
我想在 MATLAB 中旋转一个向量,并在检查原始向量和旋转向量之间的角度后立即旋转:
v = [-1 -12 5]; %arbitrarily rotated vector
theta =30; %arbitrary angle to rotate with
R = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]; %rotate around Z axis
vR = R*v'; %calculate the new vector
angle=atan2d(norm(cross(v,vR)),dot(v,vR));
%the angle between the old and rotated vector, also do normalisation before.
%atan2d is better to resolve extremely small angle
angle =
27.6588
%THIS is the problem
如您所见,我旋转了 30°,但回头看时发现不一样了。
你实际上计算的不是同一个角度。考虑输入向量为 v = [0, 0, 1](即垂直线)的情况。如果将垂直线绕 z 轴旋转 30 度,则会再次得到相同的垂直线,因此 vR = [0, 0, 1]。根据您的分析,v 和 vR 之间的角度将为 0,因为您正在计算相交于某处的两个向量之间的实际角度。
所以,如果您想计算两个向量之间的角度,那么我相信您的代码是正确的。但是,如果您想计算特定坐标系(即 z 轴)中的旋转量,那么在使用您的公式之前,您必须先将 v 和 vR 投影到 x-y 平面上:
v = [-1 -12 5]; %arbitrarily rotated vector
theta =30; %arbitrary angle to rotate with
R = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]; %rotate around Z axis
vR = R*v'; %calculate the new vector
angle=atan2d(norm(cross(v,vR)),dot(v,vR)); %calculate the angle between the vectors
% copy over the vectors and remove the z-component to project onto the x-y
% plane
v_xy = v;
v_xy(3) = 0;
vR_xy = vR;
vR_xy(3) = 0;
angle_xy=atan2d(norm(cross(v_xy,vR_xy)),dot(v_xy,vR_xy)); %calculate the angle between the vectors in the xy-plane
编辑:请注意,您仍然无法获得垂直线情况的角度(那里的解决方案存在奇点)。此外,我的建议仅适用于 z 轴旋转的情况。要对任何通用旋转轴执行此操作只需要更多数学运算:
假设您有一个由单位向量 a
定义的轴,然后围绕轴 a
旋转向量 v
以获得新向量 vR
。将 v
和 vR
投影到 a
正常的平面上,您会得到向量 p
和 pR
:
p = v - dot(v,a)*a;
pR = vR - dot(vR,a)*a;
然后你根据你的公式求出这些投影向量之间的夹角:
angle = atan2d(norm(cross(p,pR)),dot(p,pR));