Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Direction and Velocity Vector Graphs

### Functions for Graphing Vector Quantities

Four MATLAB® functions display data consisting of direction vectors and velocity vectors; three create 2-D plots and one creates 3-D plots.

Function

Description

Displays vectors emanating from the origin of a polar plot.

Displays vectors extending from equally spaced points along a horizontal line.

Displays 2-D vectors specified by (u,v) components.

Displays 3-D vectors specified by (u,v,w) components.

For feather and compass plots, you define the vectors using one or two arguments. The arguments specify the u and v components of the vectors relative to the origin. If you specify two arguments, the first specifies the u components of the vectors, and the second specifies the v components of the vectors. If you specify one argument, the functions treat the elements as complex numbers. The real parts are the u components, and the imaginary parts are the v components.

For quiver plots, in addition to the u-v components, you also specify x,y locations (or x,y,z locations in the case of quiver3) to establish an origin for each vector.

### Compass Plots

The compass function shows vectors emanating from the origin of a graph. The function takes Cartesian coordinates and plots them on a circular grid.

#### Example — Compass Plot of Wind Direction and Speed

This example shows a compass plot indicating the wind direction and strength during a 12-hour period. Two vectors define the wind direction and strength:

```wdir = [45 90 90 45 360 335 360 270 335 270 335 335];
knots = [6 6 8 6 3 9 6 8 9 10 14 12];```

Convert the wind direction, given as angles, into radians before converting the wind direction into Cartesian coordinates:

```rdir = wdir * pi/180;
[x,y] = pol2cart(rdir,knots);
compass(x,y) ```

Create text to annotate the graph:

```desc = {'Wind Direction and Strength at',
'Logan Airport for ',
'Nov. 3 at 1800 through',
'Nov. 4 at 0600'};
text(-28,15,desc)```

### Feather Plots

The feather function shows vectors emanating from a straight line parallel to the x-axis. For example, create a vector of angles from 90° to 0° and a vector the same size, with each element equal to 1.

```theta = 90:-10:0;
r = ones(size(theta));
```

Before creating a feather plot, transform the data into Cartesian coordinates and increase the magnitude of r to make the arrows more distinctive:

```[u,v] = pol2cart(theta*pi/180,r*10);
feather(u,v)
axis equal ```

#### Plotting Complex Numbers

If the input argument Z is a matrix of complex numbers, feather interprets the real parts of Z as the x components of the vectors and the imaginary parts as the y components of the vectors:

```t = 0:0.5:10;    % Time limits
s = 0.05+i;      % Spiral rate
Z = exp(-s*t);   % Compute decaying exponential
feather(Z)```

#### Printing the Graph

This particular graph looks better if you change the figure's aspect ratio by stretching the figure lengthwise using the mouse. However, to maintain this shape in the printed output, set the figure's PaperPositionMode to auto.

```set(gcf,'PaperPositionMode','auto')
```

In this mode, MATLAB prints the figure as it appears on screen.

### Two-Dimensional Quiver Plots

The quiver function shows vectors at given points in two-dimensional space. The x and y components define the vectors.

A quiver plot is useful when displayed with another plot. For example, create 10 contours of the peaks function.

```n = -2.0:.2:2.0;
[X,Y,Z] = peaks(n);
contour(X,Y,Z,10)```

Now use gradient to create the vector components to use as inputs to quiver:

`[U,V] = gradient(Z,.2);`

Set hold to on and add the contour plot:

```hold on
quiver(X,Y,U,V)
hold off ```

### Three-Dimensional Quiver Plots

Three-dimensional quiver plots (quiver3) display vectors consisting of (u,v,w) components at (x,y,z) locations. For example, you can show the path of a projectile as a function of time,

Assign values to the constants vz and a:

```vz = 10;            % Velocity
a = -32;            % Acceleration```

Calculate the height z as time varies from 0 to 1 in increments of 0.1:

```t = 0:.1:1;
z = vz*t + 1/2*a*t.^2;```

Calculate the position in the x and y directions:

```vx = 2;
x = vx*t;
vy = 3;
y = vy*t;```

Compute the components of the velocity vectors and display the vectors using the 3-D quiver plot:

```u = gradient(x);