Steve Hatcher
Steve Hatcher

Reputation: 715

Understanding MATLAB's quiver plot

I am trying to understand the quiver function, moreover, what exactly specifies the quiver's origin, direction, and length. I understand it is different from plotv.

For instance, the code quiver(0,1); axis equal produces:

first example

which seems to start at [1,1], and end at [1,2] for a length of 2. I'm not too sure how MATLAB worked that out?

Secondly, if I provide a vector if inputs such as quiver([0 0 0 0],[1 2 3 4]), I obtain:

second example

So each one seems to start at an integer value, but again I'm not sure how MATLAB is deducing the length or direction?

Lastly, I was trying to plot a sine wave with quivers starting at the origin and ending at the value of the sine wave on the y-axis. This is how far I got:

x = linspace(0,2*pi,100);
y = sin(x);
h = quiver(x(1:3:end),y(1:3:end));

example 3

Why do the quivers angle forward? And how can I normalize the arrow heads so they are the same size? I tried accessing the property MaxHeadSize, but it only works for the largest one.

Could someone please describe what is going on?

Upvotes: 1

Views: 6001

Answers (1)

Ombrophile
Ombrophile

Reputation: 663

Ans 1: From Matlab's quiver documentation, quiver accepts either x, y, u, v or u, v. You seem to be using the later case, ie, providing just the vector's lengths in the x and y direction. In such a case, Matlab does the quiver plot at equally spaced points in the xy plane which happens to start from (1,1). If you do not want this, consider providing x and y data as well to the quiver function, ie, quiver( 0, 0, 0, 1 ).

Ans 2: Direction and length of the vectors are being provided by you. Lengths of the vectors in the plot are being scaled though [Ref: quiver properties]. If you do not want this, use 'AutoScale', 'off' with the quiver function, ie,

quiver( [0, 0, 0, 0], [1, 2, 3, 4], 'AutoScale', 'off' )

Ans 3: Vectors in the sine graph lean forward because you have provided a non-zero x component to the vectors in your quiver command. Try this instead

x = linspace( 0, 2*pi, 20 );    % x-coordinates of the vectors' origin.
y = zeros( size( x ) );         % y-coordinates of the vectors' origin.
u = zeros( size( x ) );         % x-components of the vectors.
v = sin( x );                   % y-components of the vectors.

h = quiver( x, y, u, v, 'AutoScale', 'off' );

Ans 4: Sadly there is no such option to have quiver arrow heads of the same size. All you can do is modify the 'MaxHeadSize' option to suit your needs. (See the quiver properties reference for more info on this.) Other than this, I found this answer which obtains the desired effect using annotations.

Upvotes: 3

Related Questions