Matrix Rotations and Transformations
This example shows how to do rotations and transforms in 3D using Symbolic Math Toolbox™ and matrices.
Define and Plot Parametric Surface
Define the parametric surfacex(u,v)
,y(u,v)
,z(u,v)
as follows.
symsuvx = cos(u)*sin(v);y = sin(u)*sin(v);z = cos(v)*sin(v);
Plot the surface usingfsurf
.
fsurf(x,y,z) axisequal
Create Rotation Matrices
Create 3-by-3 matricesRx
,Ry
, andRz
representing plane rotations by an anglet
about thex
-,y
-, andz
-axis, respectively.
symstRx = [1 0 0; 0 cos(t) -sin(t); 0 sin(t) cos(t)]
Rx =
Ry = [cos(t) 0 sin(t); 0 1 0; -sin(t) 0 cos(t)]
Ry =
rz = [cos(t)-sin(t)0;sin(t)cos(t)0;0 0 1]
Rz =
Rotate About Each Axis in Three Dimensions
First, rotate the surface about thex
-axis by 45 degrees counterclockwise.
xyzRx = Rx*[x;y;z]; Rx45 = subs(xyzRx, t, pi/4); fsurf(Rx45(1), Rx45(2), Rx45(3)) title('Rotating by \pi/4 about x, counterclockwise') axisequal
Rotate about thez
-axis by 90 degrees clockwise.
xyzRz = Rz*Rx45; Rx45Rz90 = subs(xyzRz, t, -pi/2); fsurf(Rx45Rz90(1), Rx45Rz90(2), Rx45Rz90(3)) title('Rotating by \pi/2 about z, clockwise') axisequal
Rotate about they
- 顺时针轴上45度。
xyzRy = Ry*Rx45Rz90; Rx45Rz90Ry45 = subs(xyzRy, t, -pi/4); fsurf(Rx45Rz90Ry45(1), Rx45Rz90Ry45(2), Rx45Rz90Ry45(3)) title('大约y旋转\ pi/4,顺时针方向') axisequal
Scale and Rotate
Scale the surface by the factor 3 along thez
-axis. You can multiply the expression forz
by 3,z = 3*z
. The more general approach is to create a scaling matrix, and then multiply the scaling matrix by the vector of coordinates.
S = [1 0 0; 0 1 0; 0 0 3]; xyzScaled = S*[x; y; z]
xyzScaled =
fsurf(xyzscaled(1),xyzscaled(2),xyzscaled(3))title('Scaling by 3 along z') axisequal
Rotate the scaled surface about thex
-,y
-, andz
- 顺时针顺时针方向轴轴线45度z
, theny
, thenx
. The rotation matrix for this transformation is as follows.
r =Rx*Ry*Rz
r =
Use the rotation matrix to find the new coordinates.
xyzScaledRotated = R*xyzScaled; xyzSR45 = subs(xyzScaledRotated, t, -pi/4);
Plot the surface.
fsurf(xyzSR45(1), xyzSR45(2), xyzSR45(3)) title('大约x,y和z旋转\ pi/4,顺时针') axisequal
Check Properties of Rotation MatrixR
旋转矩阵是正交矩阵。因此,转置的R
is also its inverse, and the determinant ofR
is 1.
simplify(R.'*R)
ans =
简化(det(r))
ans =