I would like rotate a vector in MATLAB and right after check the angle between the original and the rotated one:
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
As you can see I rotated with 30° but when checking back it's different.
You are not actually calculating the same angle. Consider the situation where your input vector is v = [0, 0, 1] (ie, a vertical line). If you rotate the vertical line about the z-axis by 30 deg then you just get the same vertical line again, so vR = [0, 0, 1]. The angle between v and vR would be 0 based on your analysis because you are calculating the actual angle between two vectors that intersect somewhere.
So, if you want to calculate the angle between two vectors then I believe your code is correct. But, if you want to calculate the amount of rotation in a specific frame (ie, the z-axis), then you'll have to project v and vR onto the xy plane first before using your formula:
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
Edit: Note you still won't be able to get the angle for the vertical line case (there is a singularity in the solution there). Also, my suggestion works only for the case of a z-axis rotation. To do this for any general rotation axis just requires a bit more math:
Say you have an axis defined by unit vector a
, then rotate vector v
about axis a
to get the new vector vR
. Project v
and vR
onto the plane for which a
is normal and you get vectors p
and pR
:
p = v - dot(v,a)*a;
pR = vR - dot(vR,a)*a;
Then you find the angle between these projected vectors based on your formula:
angle = atan2d(norm(cross(p,pR)),dot(p,pR));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.