We can picture vector fields by selecting a grid of points in the plane
and drawing the vector
at the point P. Matlab does this with
the command quiver (a ``quiver'', you may recall, is a carrying case
for arrows). Here is an m-file which draws the gradient field for the function
above, as well as a contour plot for the same function
![\begin{displaymath}
\begin{tabular}[c]
{c}\texttt{grad1.m}\\ \hline
\multicolumn...
...x,y,px,py), contour(xx,yy,zz),
hold off}}\\ \hline\end{tabular}\end{displaymath}](img75.gif)
Here are some comments about this m-file.
- Note first that there are two grids, one for the gradient field and the
other for the contours. This is because you want a fine grid (every 0.2) for
drawing smooth contours, but such a fine grid would draw so many arrows for
the vector field that the screen would be too cluttered. Therefore we use the
gap 1.0 instead of 0.2 for the gradient.
- The arrays have to match up: [px,py] goes with x and
y, zz goes with xx and yy; otherwise
you'll get an error message.
- You can get a more colorful plot by replacing the contour
command with contourf. However, if you do this, you'll want to draw
the arrows in black, so use quiver(x,y,px,py,'k').
- Matlab scales the arrows so that they fit nicely in the screen and don't
touch each other. They can also be scaled manually -- look it up by typing:
help quiver at the Matlab prompt.