Rotation matrix from Euler angles.
Rp = erot(angles) Rp = erot(alpha,beta,gamma) [xc,yc,zc] = erot(...,'cols') [xr,yr,zr] = erot(...,'rows')
erot
returns an 3x3 rotation matrix in Rp
. The 3-element vector angles=[alpha beta gamma]
defines the three Euler angles for the rotation in radians. They can be also specified as three separate arguments alpha
, beta
and gamma
.
For a detailed description of the associated rotation, the convention used, and mathematical formulas see the page on relative orientations.
Basically, erot
provides a transformation matrix for vectors and matrices. If the Euler angles describe the transformation of the coordinate frame 1 to the coordinate frame 2, then you can use Rp
to transform a vector v
or tensor A
from coordinates 1 to coordinates 2.
v2 = Rp*v1; % transform v from frame 1 to frame 2 representation A2 = Rp*A1*Rp.'; % transform A from frame 1 to frame 2 representation
This does not rotate the vector or matrix, but transforms them from one coordinate system representation to the other. After the transformation, the vector/matrix is still the same, only its representation has changed. Often, such a coordinate transformation is called a passive rotation.
Ra = Rp.'
is the corresponding active transformation. The active rotation is also a composition of three rotations: first by -gamma
around z, then by -beta
around y, and last by -alpha
around z. In this case it is the vector or matrix rather than the coordinate system which changes orientation.
If the argument 'cols'
is given, erot
returns the three columns of the rotation matrix separately. The three vectors and the rotation matrix are related by Rp = [xc yc zc]
. Similarly, if the argument 'rows'
is given, erot
returns the three rows of the rotation matrix separately. However, they are returned not as rows but as column vectors. These three vectors and the rotation matrix are related by Rp = [xr.'; yr.'; zr.']
.
The inverse of erot
, i.e. calculating the Euler angles from a given rotation matrix, is provided by eulang.
To rotate the basis of the matrix A
so that the final Z axis is along -z, and (X,Y) = (-y,-x), type
A = [1 2 3; 4 5 6; 7 8 9]; Rp = erot(pi/2,pi,0); A1= Rp*A*Rp.'
A1 = 5.0000 4.0000 6.0000 2.0000 1.0000 3.0000 8.0000 7.0000 9.0000
To rotate a magnetic field vector from the z direction to a direction
in the first quadrant specified by the polar angles theta
(down from z axis, elevation complement) and phi
(counterclockwise around z from x, azimuth), use an active rotation.
B = [0;0;350]; theta = 20; phi = 75; Rp = erot([phi theta 0]*pi/180); Ra = Rp.'; B_rotated = Ra*B
B_rotated = 30.9825 115.6281 328.8924
To get the three columns separately, use
[xc,yc,zc] = erot([10 20 40]*pi/180,'cols')
xc = 0.5973 -0.7279 0.3368 yc = 0.7580 0.6495 0.0594 zc = -0.2620 0.2198 0.9397
You can see that these are the three columns of the rotation matrix
R = erot([10 20 40]*pi/180)
R = 0.5973 0.7580 -0.2620 -0.7279 0.6495 0.2198 0.3368 0.0594 0.9397
See relative orientations and coordinate framesfor details.
ang2vec, eulang, rotaxi2mat, rotmat2axi, vec2ang