3D vector plot having starting and ending points

조회 수: 4 (최근 30일)
Botong Zhu
Botong Zhu 2024년 9월 21일
편집: Star Strider 2024년 9월 22일
Hello, I'm currently a starter in 3d plotting in MATLAB and i was confused when I was trying to do a 3d vector plotting. I have the starting and ending point of a vector and I tried plotting it in 3D space, however, somehow the plot doesn't seems to be correct since the ending point of the vector doesn't seems to be the same point as given. I just want to ask how 3D vectors are defined and how should they be plotted based on starting and ending points especially when the starting point is not the origin. It would be helpful if some sample codes and examples are provided.

답변 (1개)

Star Strider
Star Strider 2024년 9월 21일
It would help to have your code.
The way I usually plot vectors in 3-space —
x = [1 2];
y = [3 4];
z = [2 8];
figure
plot3(x, y, z)
grid on
axis('padded')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Plotted Vector')
figure
plot3(x, y, z)
hold on
stem3(x, y, z, 'sr--', 'filled')
hold off
grid on
axis('padded')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Plotted Vector With ‘stem3’ Enhancement')
.
  댓글 수: 2
Botong Zhu
Botong Zhu 2024년 9월 21일
Thanks for the answer, however I would like to ask another question about the mechanics of vectors in 3d space. Do you calculate the vector first by deducting the starting point coordinates from the ending point coordinates as we do in mathematics and then plot the vector using quiver3, or just plot starting point and ending point using plot3 then connect them with a line? I was doing it in the previous approach and found it somehow incorrect, and based on your answer I assume the correct method is the later one. Am I correct about that?
Star Strider
Star Strider 2024년 9월 22일
편집: Star Strider 2024년 9월 22일
Calculating them can be done any of severel ways, depending on what you want to do. There seems to be only one way to plot them in MATLAB, and I demonstrated that here.
Plotting them as a vector filed (uwing quiver or quiver3) requires an additional set of arguments, those being the derivatives of the vector components (calculated with the gradient function here) —
x = [1 2];
y = [3 4];
z = [2 8];
gx = gradient(x);
gy = gradient(y);
gz = gradient(z);
figure
quiver3(x, y, z, gx, gy, gz)
grid on
axis('padded')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Plotted Vector')
figure
quiver3(x, y, z, gx, gy, gz)
hold on
stem3(x, y, z, 'sr--', 'filled')
hold off
grid on
axis('padded')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Plotted Vector With ‘stem3’ Enhancement')
This works in MATLAB Online, however I’m getting some sort of weird ‘Authentication failed’ error, so I can’t run it here. I guess MathWorks must be doinig site maiintenance.
EDIT — (22 Sep 2024 at 16:13)
It works this morning, so I ran my previously-posted (and unchanged) code
.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by