简体   繁体   English

如何在MATLAB中绘制矢量(物理2D / 3D矢量)?

[英]How to draw vectors (physical 2D/3D vectors) in MATLAB?

I want to know the simplest way to plot vectors in MATLAB . 我想知道在MATLAB中绘制矢量的最简单方法。 For example: 例如:

a = [2 3 5];
b = [1 1 0];
c = a + b;

I want to visualize this vector addition as head-to-tail/parallelogram method. 我想将此向量加法可视化为头尾/平行四边形方法。 How do I plot these vectors with an arrow-head? 如何用箭头绘制这些向量?

a = [2 3 5];
b = [1 1 0];
c = a+b;

starts = zeros(3,3);
ends = [a;b;c];

quiver3(starts(:,1), starts(:,2), starts(:,3), ends(:,1), ends(:,2), ends(:,3))
axis equal

I agree with Aamir that the submission arrow.m from Erik Johnson on the MathWorks File Exchange is a very nice option. 我同意Aamir观点Erik JohnsonMathWorks File Exchange上提交的arrow.m是一个非常不错的选择。 You can use it to illustrate the different methods of vector addition like so: 您可以使用它来说明矢量加法不同方法,如下所示:

  • Tip-to-tail method: 从头到尾的方法:

     o = [0 0 0]; %# Origin a = [2 3 5]; %# Vector 1 b = [1 1 0]; %# Vector 2 c = a+b; %# Resultant arrowStarts = [o; a; o]; %# Starting points for arrows arrowEnds = [a; c; c]; %# Ending points for arrows arrow(arrowStarts,arrowEnds); %# Plot arrows 
  • Parallelogram method: 平行四边形法:

     o = [0 0 0]; %# Origin a = [2 3 5]; %# Vector 1 b = [1 1 0]; %# Vector 2 c = a+b; %# Resultant arrowStarts = [o; o; o]; %# Starting points for arrows arrowEnds = [a; b; c]; %# Ending points for arrows arrow(arrowStarts,arrowEnds); %# Plot arrows hold on; lineX = [a(1) b(1); c(1) c(1)]; %# X data for lines lineY = [a(2) b(2); c(2) c(2)]; %# Y data for lines lineZ = [a(3) b(3); c(3) c(3)]; %# Z data for lines line(lineX,lineY,lineZ,'Color','k','LineStyle',':'); %# Plot lines 

我在MATLAB Central上找到了这个arrow(start, end)功能,非常适合绘制具有真实大小和方向的矢量。

I did it this way, 我是这样做的

2D 2D

% vectors I want to plot as rows (XSTART, YSTART) (XDIR, YDIR)
rays = [
  1 2   1 0 ;
  3 3   0 1 ;
  0 1   2 0 ;
  2 0   0 2 ;
] ;

% quiver plot
quiver( rays( :,1 ), rays( :,2 ), rays( :,3 ), rays( :,4 ) );

3D 3D

% vectors I want to plot as rows (XSTART, YSTART, ZSTART) (XDIR, YDIR, ZDIR)
rays = [
  1 2 0  1 0 0;
  3 3 2  0 1 -1 ;
  0 1 -1  2 0 8;
  2 0 0  0 2 1;
] ;

% quiver plot
quiver3( rays( :,1 ), rays( :,2 ), rays( :,3 ), rays( :,4 ), rays( :,5 ), rays( :,6 ) );

Based on the quiver and quiver3 documentation 基于quiverquiver3文档

我不确定在3D模式下执行此操作的方法,但是在2D模式下,您可以使用compass命令。

            % draw simple vector from pt a to pt b
            % wtr : with respect to
            scale=0;%for drawin  vectors with true scale
            a = [10 20 30];% wrt origine O(0,0,0)
            b = [10 10 20];% wrt origine O(0,0,0)

            starts=a;% a now is the origine of my vector to draw (from a to b) so we made a translation from point O to point  a = to vector a 
            c = b-a;% c is the new coordinates of b wrt origine a 
            ends=c;%
            plot3(a(1),a(2),a(3),'*b')
            hold on
            plot3(b(1),b(2),b(3),'*g')

             quiver3(starts(:,1), starts(:,2), starts(:,3), ends(:,1), ends(:,2), ends(:,3),scale);% Use scale = 0 to plot the vectors without the automatic scaling.
            % axis equal
            hold off

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM