Setting a 3rd vector with specific angles to 2 other vectors
11 views (last 30 days)
Show older comments
Ozzy
on 27 Oct 2021
Commented: David Goodmanson
on 29 Oct 2021
Hello, I want to define 2 vectors in the 3D coordinate space (A and B) and the angle between them should be 92.1 degrees. I then want to set a 3rd vector (C) which will be at a 84.4 degrees angle with A and 86.2 degrees with B. Here is the code I am using for that:
% Define angles I want
A_B = 92.1;
A_C = 84.4;
B_C = 86.2;
% Create a unit vector A
A = [0; 0; 1];
% Define B by rotating A by A_B angle around x-axis
rotationx = [1 0 0 ; ...
0 cosd(A_B) -sind(A_B) ; ...
0 sind(A_B) cosd(A_B)] ;
B = rotationx*A;
% Define C with respect to A by rotating A by A_C angle around y-axis
rotationy = [cosd(A_C) 0 sind(A_C) ; ...
0 1 0 ; ...
-sind(A_C) 0 cosd(A_C)];
C = rotationy*A;
% Calculate the angle difference between current C and B
angleCurrent = atan2d(norm(cross(C,B)), dot(C,B));
% Find the difference between the current angle and the C-B value I want to
% get
angleDiff = B_C-angleCurrent;
% Move C again around the z-axis
rotationz = [cosd(angleDiff) -sind(angleDiff) 0; ...
sind(angleDiff) cosd(angleDiff) 0; ...
0 0 1];
C = rotationz*C;
With this code I get the following values. Looks like the angle between A_B and A_C is fine, but the B_C is very slightly off. I guess I should move my C vector around the y and z axes at the same time instead of moving it in separate steps with repsect to A and B. Am I correct? If so, how should the rotation matrix look? Thanks!
A_B = 92.1;
A_C = 84.4;
B_C = 86.2223;
0 Comments
Accepted Answer
David Goodmanson
on 28 Oct 2021
Hi Huseyin,
The method you are using gets you into an iterative process of successive rotations about the x and z axis that eventually converge on the result. The method below for C uses a standard rotation about the y axis like you started with, followed by a rotation about z by a yet to be determined angle theta. Then you use a bit of algebral to find theta.
A_B = 92.1;
A_C = 84.4;
B_C = 86.2;
A = [0; 0; 1 ];
% find B, per your rotation
B = [0; -sind(A_B); cosd(A_B)];
% rotate in spherical coordinates to find C.
% note that that angle (A_C) is still satisfied
% C = [sind(A_C)*cosd(theta); sind(A_C)*sind(theta); cosd(A_C)];
% now BdotC = -sind(A_B)*sind(A_C)*sind(theta) + cosd(A_B)*cosd(A_C) = cosd(B_C)
% solve
sindtheta = (cosd(A_B)*cosd(A_C)-cosd(B_C))/(sind(A_B)*sind(A_C));
theta = asind(sindtheta)
Cnew = [sind(A_C)*cosd(theta); sind(A_C)*sind(theta); cosd(A_C)]
% check, should be small
dot(A,B)-cosd(A_B)
dot(A,Cnew)-cosd(A_C)
dot(B,Cnew)-cosd(B_C)
D = C -Cnew
theta = -4.0273
Cnew =
0.9928
-0.0699
0.0976
D =
1.0e-03 *
0.0273
0.3886
0
0 Comments
More Answers (1)
Ozzy
on 28 Oct 2021
1 Comment
David Goodmanson
on 29 Oct 2021
Huseyin,
I mentioned that what you were doing was the start of an iterative process. The method I used is not iterative. Starting with A, it defines C by using a known rotation angle (A_C) about y, followed by an unknown rotation angle theta about z. It finds the dot product of B and C, which has to equal cosd(B_C), and solves for sind(theta) algebraically in one step. Then it uses that value of theta to find C.
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!