![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1273630/image.png)
Defining a 3d XYZ vector aligned with Z axis and then rotating it along the XY plane by pi/4
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rajeev
2023-1-25
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1273630/image.png)
回答(1 个)
Nehemiae
2023-3-10
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])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1320160/image.png)
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!