Calculate the 3D angle between two vectors
Afficher commentaires plus anciens
How do you calculate the angles between two vectors in order to generate a direction cosine matrix? I have MATLAB, Simulink, and Aerospace Toolkit/Toolbox. For reference, I am trying to use MATLAB to calculate a rotation matrix between the body frame of an object and (preferably) a fixed-frame or (not so preferably) an inertial-frame.
Réponses (3)
Roger Stafford
le 5 Mar 2017
Modifié(e) : Roger Stafford
le 5 Mar 2017
The angle between two three-element vectors, P1 and P2, can be calculated using matlab in the following way:
a = atan2(norm(cross(P1,P2)),dot(P1,P2)); % Angle in radians
The angle will lie between 0 and pi radians. To get degrees use ‘atan2d’.
Note: However, the cosine of such an angle can be calculated as:
cosine of the angle = dot(P1,P2)/(norm(P1)*norm(P2))
No need to compute the angle itself.
4 commentaires
Paul Huter
le 5 Mar 2017
Roger Stafford
le 5 Mar 2017
Modifié(e) : Roger Stafford
le 5 Mar 2017
Let a vector be V = [x;y;z]. The three direction cosines would be: V/norm(V). For the three corresponding angles, either use the ‘acos’ function on each such cosine value or else do the somewhat more accurate:
ax = atan2(norm(cross(V,[1;0;0])),dot(V,[1;0;0]));
ay = atan2(norm(cross(V,[0;1;0])),dot(V,[0;1;0]));
az = atan2(norm(cross(V,[0;0;1])),dot(V,[0;0;1]));
Note: These latter are the same as writing:
ax = atan2(sqrt(y^2+z^2),x);
ay = atan2(sqrt(z^2+x^2),y);
az = atan2(sqrt(x^2+y^2),z);
Zhafirah Ariana
le 31 Juil 2020
I tried this and it worked! But when i use this for array it didn't work. Do you have any idea why this happened?
Bruno Luong
le 31 Juil 2020
Modifié(e) : Bruno Luong
le 31 Juil 2020
For array of size 3 x N where 3 rows are x-y-z coordinates you might replace the
- CROSS with CROSS_DIM1
- DOT with DOT_DIM1
- NORM(X) with vecnorm(x, 2, 1) or sqrt(sum(x.^2,1)).
- "^" with ".^"
CROSS_DIM1
function c = cross_dim1(a,b)
% c = cross_dim1(a,b)
% Calculate cross product along the first dimension
% NOTE: auto expansion allowed
c = zeros(max(size(a),size(b)));
c(1,:) = a(2,:).*b(3,:)-a(3,:).*b(2,:);
c(2,:) = a(3,:).*b(1,:)-a(1,:).*b(3,:);
c(3,:) = a(1,:).*b(2,:)-a(2,:).*b(1,:);
end % cross_dim1
and DOT_DIM1
function d = dot_dim1(a,b)
% d = dot_dim1(a,b)
% Calculate dot product along the first dimension
% NOTE: auto expansion allowed
d = sum(a.*b,1);
end % dot_dim1
Bingo
le 25 Mai 2020
0 votes
function r = vrrotvec(a, b, options)
%VRROTVEC Calculate a rotation between two vectors.
% R = VRROTVEC(A, B) calculates a rotation needed to transform
% a 3d vector A to a 3d vector B.
4 commentaires
Image Analyst
le 25 Mai 2020
You forgot to provide the body of the function.
Matlab Helmstaedter
le 25 Mai 2020
Its an inbuilt function.
John D'Errico
le 25 Mai 2020
Not really. It is only there IF you just happen to have the correct toolbox.
help vrrotvec
'vrrotvec' requires Simulink 3D Animation.
Otherwise, you need to use the alteriatives.
Image Analyst
le 25 Mai 2020
Modifié(e) : Image Analyst
le 25 Mai 2020
I don't have that toolbox. Simulink 3D Animation is not on the Products list on the right. I will add it.
Catégories
En savoir plus sur Axes Transformations dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!