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

Michael J. Sharpe msharpe at euclid.ucsd.edu
Wed Jan 10 00:41:12 CET 2001

At 8:13 PM +0100 1/9/01, Denis Girou wrote:
>  >>>>> "Michael.Dougherty" == Michael M Dougherty, Ph D
><doughem at swosu.edu> writes:
>
>     Michael.Dougherty> I would like to plot a direction field for some simple
>     Michael.Dougherty> ordinary differential equations without plotting the
>     Michael.Dougherty> vectors or line segments each individually.  In other
>     Michael.Dougherty> words I would like to use loops, perhaps to make
>     Michael.Dougherty> a vector or line segment at/through each
>point with integer
>     Michael.Dougherty> coordinates.  For instance, I would like to plot
>     Michael.Dougherty> a field for $dy/dx=-x/y$ (and later plot some circles
>     Michael.Dougherty> which are the solution curves--that I can do).  I would
>     Michael.Dougherty> like the method to be more general than for
>just this one, but
>     Michael.Dougherty> hopefully this one will suffice to start.
>
>     Michael.Dougherty> Therefore I need the coordinates of the line
>segments/vectors
>     Michael.Dougherty> to be functions of $x$ and $y$.  I was hoping
>to use multido
>     Michael.Dougherty> but I do not know how to calculate the coordinates of
>     Michael.Dougherty> the endpoints of the line segments or vectors
>within the loop.
>     Michael.Dougherty> This is my first attempt at multido, but I do
>understand how
>     Michael.Dougherty> to plot functions (so I know about RPN).
>Here's the code
>     Michael.Dougherty> I'm looking at:
>
>     Michael.Dougherty> \begin{pspicture}(-6,-5)(6,5)
>     Michael.Dougherty> \psaxes{<->}(0,0)(-6,-5)(6,5)
>     Michael.Dougherty> \multido{\n=-5+1}{11}{%
>     Michael.Dougherty>  \multido{\N=-5+1}{11}{%
>     Michael.Dougherty> %%%%%%%  WHAT TO PUT HERE?
>     Michael.Dougherty> %%%%%%%
>Want\psline((\n-1/(2\sqrt{1+x^2/y^2}),\N-x/(2*y\sqrt{1+%
>     Michael.Dougherty> %%%%%%%
>x^2/y^2})),((n+1/(2\sqrt{1+x^2/y^2}),\N+x/(2*y\sqrt{1+%
>     Michael.Dougherty> %%%%%%%  x^2/y^2})).
>     Michael.Dougherty> }}
>     Michael.Dougherty> \end{pspicture}
>
>   Another solution than the one posted by Michael Sharpe is to use a \psline
>macro with PostScript coordinates (see the documentation page 72, (!ps)
>paragraph). I use the formula given by Michael Dougherty.
>
>   Probably that this solution would be easier to understand, because it
>require less PostScript management as the drawing of the vectors is done by
>a PSTricks macro, which avoid to use the translate, rlineto, etc. PostScript
>macros. Nevertheless, probably that the solution of Michael is more powerful
>and flexible, as it seems easier to change the definition of the vectors.
>If you experience more on this field, tell us the results of your work...
>
>   P.S. for Michael Dougherty: comparing to the version I sent you yesterday,
>this one is a little more complicated with a conditional in the PostScript
>code, because I manage this time the cases where \ia or \ib is equal to 0.
>
>\documentclass[a4paper]{article}
>
>\usepackage{pst-plot}
>
>\pagestyle{empty}
>
>\begin{document}
>
>\SpecialCoor
>
>\begin{pspicture}(-6,-6)(6,6)
>   \psaxes{<->}(0,0)(-6,-6)(6,6)
>   \psset{arrows=->}
>   \multido{\ia=-5+1}{11}{%
>     \multido{\ib=-5+1}{11}{%
>       \pstVerb{/x \ia\space def
>                /y \ib\space def
>                y 0 eq
>                  {/ValueTempA 0 def
>                   /ValueTempB 0.5 def}
>                  {/ValueTempZ 2 1 x x mul y y mul div add sqrt mul def
>                   /ValueTempA 1 ValueTempZ div def
>                   /ValueTempB x y ValueTempZ mul div def}
>                  ifelse}
>       \psline(! x ValueTempA sub y ValueTempB sub)
>\end{pspicture}
>
>\begin{pspicture}(-6,-6)(6,6)
>   \psaxes{<->}(0,0)(-6,-6)(6,6)
>   \psset{arrows=->}
>   \multido{\ia=-5+1}{11}{%
>     \multido{\ib=-5+1}{11}{%
>       \pstVerb{/x \ia\space def
>                /y \ib\space def
>                y 0 eq
>                  {/ValueTempA 0 def
>                   /ValueTempB 0.5 def}
>                  {/ValueTempZ 2 1 x x mul y y mul div add sqrt mul def
>                   /ValueTempA 1 ValueTempZ div def
>                   /ValueTempB x neg y ValueTempZ mul div def}
>                  ifelse}
>       \psline(! x ValueTempA sub y ValueTempB sub)
>\end{pspicture}
>
>% From Michael Sharpe
>
>\def\transl#1#2{%
>#1\space 72 2.54 div mul #2\space 72 2.54 div mul translate 0 0 moveto}
>
>\newif\ifnotbothz
>
>\begin{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)
>         \pstverb{\transl{\n}{\N}\space
>                  %next line gives a direction vector (y=\n,x=\N)
>                  \n\space \N\space
>                  atan rotate -9 0 moveto 18 0 rlineto stroke}
>       \fi}}
>\end{pspicture}
>
>\begin{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)
>         \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}}
>\end{pspicture}
>
>\end{document}
>
>D.G.
>_______________________________________________

Here's another way making use of the ability of \SpecialCoor pointed
out by Denis to do the picture using \pstcustom.

%&Plain
\input pstricks
\input pst-plot
\def\drawseg#1#2{\pscustom{%
\translate(#1,#2)
\rotate{(-#2,#1)}%vector in direction of field at x=#1,y=#2
\psline(-.25,0)(.25,0)
}}
Direction field example ($y'=-x/y$).

\vglue2pc
\SpecialCoor

\pspicture(-6,-6)(6,6)
\psaxes{<->}(0,0)(-6,-6)(6,6)
\multido{\n=-5+1}{11}{%
\multido{\N=-5+1}{11}{%
\ifnum\N=0 %
\ifnum\n=0 \else\drawseg{\n}{\N}
\fi
\else\drawseg{\n}{\N}\fi
}
}

\endpspicture
\end