Clear Filters
Clear Filters

Defining a 3d XYZ vector aligned with Z axis and then rotating it along the XY plane by pi/4

4 views (last 30 days)
x= zeros(1,101);
y= zeros(1,101);
z= 0:0.01:1;
figure(1)
plot3(x,y,z,'LineWidth',2, 'Color',[1 0 0])
grid on
axis square
rot = xrot(pi/4);
xr = rot(1,:);
yr = rot(2,:);
zr = rot(3,:);
xr = x.*xr';
yr = y.*yr';
zr = z.*zr';
figure(2)
plot3(xr,yr,zr,'LineWidth',2, 'Color',[1 0 0])
grid on
axis square
function Rx=xrot(phi)
Rx = [1 0 0; 0 cos(phi) -sin(phi);0 sin(phi) cos(phi)];
This is the code I have currently. I have not used plot3 or rotated graphs before. I was wondering if this is even the correct way to define a vector along the z axis in the first place and if so how do I rotate the vector if rotates at all.
  1 Comment
Rajeev
Rajeev on 25 Jan 2023
By rotating the vector along the x-y plane, you mean the final position of the rotated vector would have an angle of with its projection on the x-y plane?

Sign in to comment.

Answers (1)

Nehemiae
Nehemiae on 10 Mar 2023
Hello,
The coordinates defined indeed are aligned with the z-axis. To rotate a matrix by some degree, the “rotx”, “roty” and “rotz” functions can be used to rotate a 3D matrix. These functions return a rotation matrix which need to be multiplied by matrix multiplication - i.e. * (not element-wise multiplication .*) - with the coordinate matrix.
rot = rotx(90); % Give angle in degrees
rotMat = rot * [x; y; z];
rot = rotz(45); % Give angle in degrees
rotMat = rot * rotMat;
plot3(rotMat(1, :), rotMat(2, :), rotMat(3, :),'LineWidth', 2, 'Color', [1 0 0])
The documentation on the “rotz” function (https://www.mathworks.com/help/releases/R2021b/phased/ref/rotz.html?s_tid=doc_ta) is helpful in this regard.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!