Gradient vector fields

The gradient vector $\nabla f$ defines a vector field on the domain of $f$. We can plot this vector field in two ways: with the draw package, or with the ploteq function.

First we need a gradient function. Let's start with $f(x,y)=x^2 - y^2$. Then, of course, $\nabla f = \langle 2x, -2y \rangle$.


\begin{lstlisting}[basicstyle={\ttfamily }]
kill(f, x, y, gdf);
f(x,y) := x^2 - ...
...grad(f(x,y));
ev(express(gdf(x,y)), diff);
define(gdf(x,y), %);
\end{lstlisting}

Next we make the plot. (We have omitted the first few lines because we will use the same values from the last example. However, these lines should not ordinarily be skipped).


\begin{lstlisting}[basicstyle={\ttfamily }]
vf2d(x,y) := vector([x,y], gdf(x,y)/...
...);
apply(draw2d, append([head_length=0.1, color=blue], vect2));
\end{lstlisting}

Figure: The gradient vector field $\nabla f$ for the function $f(x,y)=x^2 - y^2$
\includegraphics{gradvectfield.eps}

Alternatively, we can do it in one line with the ploteq function as long as we remember to put a minus sign in front of the original function (ploteq plots vectors that are the opposite of gradient vectors).


\begin{lstlisting}[basicstyle={\ttfamily }]
ploteq(-(x^2+y^2),[x,y],[x,-4,4],[y,-4,4],[vectors,''blue'']);
\end{lstlisting}



G. Jay Kerns 2009-12-01