MATLAB Answers

0

Finding angle of rotations from a given unit vector to rotate a given vector using those angle to align with the previous unit vector

Asked by Nafisa Amin on 12 Nov 2019 at 0:15
Latest activity Commented on by Nafisa Amin on 17 Nov 2019 at 0:34
I have a unit vector , let's say, b=[0.1844 -0.7417 0.6449] and I want to find the angle of rotations so that I can align a given vector a=[0 0 1] onto that unit vector,b. The way I started this problem is by finding the angle of rotations from the vector and then plugging them back inti the rotation matrix to allign a onto b in the same direction. But I'm stuck at this point. It's not giving me the angles correctly. I've attached my code below.
a=[0,0,1];
[R,theta_x,theta_y,Ry,Rx]=rot_xy(a(1,1),a(1,2),a(1,3));
test=R*a';
function [w,w_k,w_l,w_jk,w_kl ] = rot_xy( x,y,z)
% theta_x=-atand(y/sqrt(x^2+z^2));
% theta_y=pi-atand(x/z);
w_k=-atan(y/sqrt(x^2+z^2));
w_l=pi-atan(x/z);
%rot about y: tilt rotation
w_jk = [ cos(w_k) 0 sin(w_k) ;
0 1 0 ;
-sin(w_k) 0 cos(w_k) ];
%rot about x: twist rotation
w_kl = [ 1 0 0 ;
0 cos(w_l) -sin(w_l) ;
0 sin(w_l) cos(w_l) ];
w = w_jk * w_kl;
end
figure
plot3([0,a(1,1)],[0,a(1,2)],[0,a(1,3)],'r')
grid on
ax=gca;
ax.XColor='green';
ax.YColor='magenta';
grid on
axis([-.5 .5 -2.5 1 -1.5 1.5])
hold on
plot3([0,1.5*(b(1,1))],[0,1.5*(b(1,2))],[0,1.5*(b(1,3))],'k')
hold on
c=test';
hold on
plot3([0,c(1,1)],[0,c(1,2)],[0,c(1,3)],':r')

  1 Comment

Generally speaking, representing the alignment as a rotation about x followed by y will either be impossible, or have two solutions. For example, it is easy to see that there is no solution for
a=[1 0 0 ].';
b=[0 1 0].';
Direct substitution into the equation b = w_jk * w_kl *a leads to
[0 1 0] = [cos(w_k) 0 -sin(w_k)]
which clearly has no solution.
Conversely, the following data has two solutions and two correspondingly different rotation matrices. You can reach b from a with either a rotation about x of t degrees followed by a 78 degree rotation about y. Or, you can do a rotation about x of -t degrees followed by a rotation about y of 102 degrees.
a =[
-0.874881810907645
0.484336470795830
0.000000000000000]
b =[
0
0.447213595499958
0.894427190999916]
t = 22.578683389390513;

Sign in to comment.

1 Answer

Answer by Matt J
on 12 Nov 2019 at 0:48
Edited by Matt J
on 12 Nov 2019 at 0:50
 Accepted Answer

The following might be what you're looking for. It uses my AxelRot utility from the File Exchange
Or, if you only want theta, you could modify the code to return only that, which then wouldn't require AxelRot.
function [M,theta, Nrm]=vecrot(vstart,vend)
%Find rotation carrying one vector toward another about their common perpendicular
%axis.
%
%IN:
%
% vstart: Initial vector
% vend: Final vector
%
%OUT:
%
% M: homogeneous 4x4 rotation matrix carrying vstart to vend in a
% rotation about the axis Nrm=cross(vstart,vend)
% theta: the rotation angle in degrees
% Nrm: the rotation axis
vstart=vstart(:)/norm(vstart);
vend=vend(:)/norm(vend);
Nrm=cross(vstart,vend);
b=vend.'*vstart;
theta = atan2d(sqrt(1-b^2),b);
M=AxelRot(theta,Nrm,[]);

  12 Comments

Hi, I am facing one problem for which I need help. If I have two unit bectors a and b and if a=b or a=-b, the file you have given is showing NaN.
a=[0 0 1];
b=[0 0 -1];
[M,theta, Nrm]=vec_rot(a,b);
R=M(1:3,1:3);
Warning: Matrix is singular, close to singular or badly scaled.
Results may be inaccurate. RCOND = NaN.
> In AxelRot (line 94)
In vec_rot (line 24)
>> rot=R*a(:);
>> rot
rot =
NaN
NaN
NaN
How would you define the result in those cases? There are infinite choices, but this one may serve:
if all(a==b)
R=eye(3);
elseif all(a==-b)
N=null(a(:).');
c=N(:,1);
M=vec_rot(a,c);
R=M(1:3,1:3)^2;
else
[M,theta, Nrm]=vec_rot(a,b);
R=M(1:3,1:3);
end

Sign in to comment.