2015-10-09 21 views
1

我想旋轉在MATLAB的載體,並檢查旋轉一個原始的和之間的角度後右:旋轉在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°旋轉但是在檢查時不同。

+0

打電話給你最後一次變量'角度'是不是一個好主意......(內置matlab函數) – bla

+0

這只是一個演示,這不是問題在這裏 – Bolee

+0

有趣的是'rad2deg(subspace(v(:),vR(:))''會給出相同的答案 – bla

回答

2

你實際上並沒有計算相同的角度。考慮輸入矢量爲v = [0,0,1](即垂直線)的情況。如果將垂直線圍繞z軸旋轉30度,則只需再次獲得相同的垂直線,所以vR = [0,0,1]。根據您的分析,v和vR之間的角度將爲0,因爲您正在計算與某處相交的兩個向量之間的實際角度。

所以,如果你想計算兩個向量之間的角度,那麼我相信你的代碼是正確的。但是,如果要計算特定幀(即z軸)中的旋轉量,則在使用公式之前,必須先將v和vR投影到xy平面上:

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。項目vvR到平面這a是正常的,你會得到載體ppR

p = v - dot(v,a)*a; 
pR = vR - dot(vR,a)*a; 

然後你會發現基礎上的公式,這些投影向量之間的角度:

angle = atan2d(norm(cross(p,pR)),dot(p,pR)); 
+0

謝謝Brian! – Bolee