Main Content

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

图包含一个轴对象。The axes object contains an object of type parameterizedfunctionsurface.

Create Rotation Matrices

Create 3-by-3 matricesRx,Ry, andRzrepresenting plane rotations by an angletabout thex-,y-, andz-axis, respectively.

symstRx = [1 0 0; 0 cos(t) -sin(t); 0 sin(t) cos(t)]
Rx =

( 1 0 0 0 cos ( t ) - sin ( t ) 0 sin ( t ) cos ( t ) )

Ry = [cos(t) 0 sin(t); 0 1 0; -sin(t) 0 cos(t)]
Ry =

( cos ( t ) 0 sin ( t ) 0 1 0 - sin ( t ) 0 cos ( t ) )

rz = [cos(t)-sin(t)0;sin(t)cos(t)0;0 0 1]
Rz =

( cos ( t ) - sin ( t ) 0 sin ( t ) cos ( t ) 0 0 0 1 )

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

图包含一个轴对象。The axes object with title R o t a t i n g blank b y blank pi / 4 blank a b o u t blank x , blank c o u n t e r c l o c k w i s e contains an object of type parameterizedfunctionsurface.

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

图包含一个轴对象。带有标题的轴对象r o t a t i n g空白b y空白pi / 2空白a b o u t blank z,空白c l o c k w i s e包含一个类型的参数化functionsurface的对象。

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

图包含一个轴对象。The axes object with title R o t a t i n g blank b y blank pi / 4 blank a b o u t blank y , blank c l o c k w i s e contains an object of type parameterizedfunctionsurface.

Scale and Rotate

Scale the surface by the factor 3 along thez-axis. You can multiply the expression forzby 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 =

( cos ( u ) sin ( v ) sin ( u ) sin ( v ) 3 cos ( v ) sin ( v ) )

fsurf(xyzscaled(1),xyzscaled(2),xyzscaled(3))title('Scaling by 3 along z') axisequal

图包含一个轴对象。The axes object with title Scaling by 3 along z contains an object of type parameterizedfunctionsurface.

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 =

( cos ( t ) 2 - cos ( t ) sin ( t ) sin ( t ) σ 1 cos ( t ) 2 - sin ( t ) 3 - cos ( t ) sin ( t ) sin ( t ) 2 - cos ( t ) 2 sin ( t ) σ 1 cos ( t ) 2 ) where σ 1 = cos ( t ) sin ( t ) 2 + cos ( t ) sin ( t )

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

图包含一个轴对象。带有标题r o t a t a t i n g空白的轴对象空白pi / 4空白a b o u t blank x,blank y,blank a n d vall z,空白c l o c k w i s e包含一个类型的参数化functionsurface的对象。

Check Properties of Rotation MatrixR

旋转矩阵是正交矩阵。因此,转置的Ris also its inverse, and the determinant ofRis 1.

simplify(R.'*R)
ans =

( 1 0 0 0 1 0 0 0 1 )

简化(det(r))
ans =
                  
                   
                    
                     1