# [pstricks] Draw a vector field

Michael Sharpe msharpe at ucsd.edu
Tue Nov 9 07:52:38 CET 2010

On Nov 8, 2010, at 6:40 PM, David Tarazona wrote:

> Hi again and thanks for the support
>
> My question is simple, how i can to draw a vector field of the gradient of a z=f(x,y) function???
>
> i mean, the gradient of z is:
>
> ∇Z=(∂z/∂x) î + (∂z/∂y) ĵ
>
> so i want to graph the vector field as seen on pages 9 and 23 on this PDF:
>
> i think that i could use simply a "double multido" for x and y and graph the vectors, but i dont know how to get the color effect shown in the PDF...
>
> thanks for the support again
>

Well, since no-one else is stepping up, here's an example that goes part way to what you want. It isn't limited to the gradient of a function but works for any vector field (vx,vy). The color range is not correct (this example goes from black to green), and the line thickness isn't either.

\psset{unit=3cm}%
\begin{pspicture}[showgrid=true](-.5,-.5)(1.5,1.5)
% assume vector field magnitude in range [0,20]
\multido{\nx=0+.2}{6}{
\multido{\ny=0+.2}{6}{% specify vx vy in PostScript code
\pstVerb{ \nx\space dup mul 20 mul neg /vx exch def \ny
\space dup mul 20 mul /vy exch def vx dup mul vy dup mul add sqrt /radius exch def /ndx radius .5 add cvi def }\definecolor[ps]{thecolor}{rgb}{0 ndx 20 div 0}
\psrline[linecolor=thecolor]{->}(\nx,\ny)(10pt;{(! vx vy )})}}
\end{pspicture}

Michael