# [pstricks] Making Direction Fields; also coordinates as functions inside loops

Michael J. Sharpe msharpe at euclid.ucsd.edu
Tue Jan 9 19:09:00 CET 2001

>Fellow PsTricksters,
>
>I would like to plot a direction field for some simple
>ordinary differential equations without plotting the
>vectors or line segments each individually.  In other
>words I would like to use loops, perhaps to make
>a vector or line segment at/through each point with integer
>coordinates.  For instance, I would like to plot
>a field for $dy/dx=-x/y$ (and later plot some circles
>which are the solution curves--that I can do).  I would
>like the method to be more general than for just this one, but
>hopefully this one will suffice to start.
>
>Therefore I need the coordinates of the line segments/vectors
>to be functions of $x$ and $y$.  I was hoping to use multido
>but I do not know how to calculate the coordinates of
>the endpoints of the line segments or vectors within the loop.
>This is my first attempt at multido, but I do understand how
>to plot functions (so I know about RPN).  Here's the code
>I'm looking at:
>
>\begin{pspicture}(-6,-5)(6,5)
>\psaxes{<->}(0,0)(-6,-5)(6,5)
>\multido{\n=-5+1}{11}{%
>  \multido{\N=-5+1}{11}{%
>%%%%%%%  WHAT TO PUT HERE?
>%%%%%%%  Want\psline((\n-1/(2\sqrt{1+x^2/y^2}),\N-x/(2*y\sqrt{1+%
>%%%%%%%  x^2/y^2})),((n+1/(2\sqrt{1+x^2/y^2}),\N+x/(2*y\sqrt{1+%
>%%%%%%%  x^2/y^2})).
>}}
>\end{pspicture}
>
>
>Please don't let the ugliness of the calculation distract.
>I only would like to know if it is possible to compute these
>coordinates inside the loop and enter them as arguments
>of \psline and the like.  (Of course I will have to
>avoid the (y=)\N=0 and do those points separately to avoid
>dividing by zero.)
>
>Or if anyone has found another way to draw direction fields
>without resorting to Mathematica, etc., but inside LaTeX
>I would appreciate any guidance.
>
>
>
>--
>Michael M. Dougherty
>Assistant Professor of Mathematics
>Southwestern Oklahoma State University
>(580) 774-3057
>_______________________________________________

It is possible to do what you ask using embedded ps code, though I
wouldn't think the methods would adapt easily to very general
direction fields. Basically, you have to write ps code for the y and
x coordinates of a vector in the direction of the direction field,
expressed in terms of the variables x->\N, y->\n. I haven't tried to
make this anything but a basic demo---it assumes the unit is cm. The
Postscript code is simple in this case. For each line segment, the
origin is translated to the center of the segment and the axes are
rotated so the x axis lies in the direction of the specified
direction vector, after which a line segment of width 18 pt is drawn.

For a different field, only one line (\n\space neg \N\space ) needs
to be changed.

%&Plain
\input pstricks
\input pst-plot
Direction field example ($y'=-x/y$).
\vskip2pc
\def\transl#1#2{#1\space 72 2.54 div mul #2\space 72 2.54 div
mul translate  0 0 moveto }
\newif\ifnotbothz

\pspicture(-6,-6)(6,6)
\psaxes{<->}(0,0)(-6,-6)(6,6)
\multido{\n=-5+1}{11}{%
\multido{\N=-5+1}{11}{%
\notbothztrue%
\ifnum\N=0 %
\ifnum\n=0 \notbothzfalse\fi%
\fi%
\ifnotbothz%
\psdot(0,0)%seems to be required to keep reference point correct
\pstverb{\transl{\n}{\N}\space
%next line gives a direction vector (y=-\n,x=\N)
\n\space neg \N\space
atan rotate -9 0 moveto 18 0 rlineto stroke }
\fi
}
}

\endpspicture
\end