Asked by Nafisa Amin
on 12 Nov 2019 at 0:15

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')

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,[]);

Nafisa Amin
on 16 Nov 2019 at 0:11

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

Matt J
on 16 Nov 2019 at 0:51

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

Nafisa Amin
on 17 Nov 2019 at 0:34

Thanks. It works.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Matt J (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490545-finding-angle-of-rotations-from-a-given-unit-vector-to-rotate-a-given-vector-using-those-angle-to-al#comment_766449

Sign in to comment.