Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Are there any PSTricks packages for drawing vector fields?

I have had a look at http://tug.org/PSTricks/main.cgi?file=examples#vector but I don't fully understand the syntax in the example(s). Also I have had a look at page 23 on ftp://ftp.chinatex.org/TeXDoc/TeXDraw/pstricks/pstricksexample/seminarSS08.pdf but here the code is missing. :(

Specifically, I would like to draw the vector field

{1, x}

in the range from -5 to 5 on both axes. Here is the vector field drawn using Mathematica:

vectorfield

Update

I have figured out how to do it (more or less):

\documentclass{article}

\usepackage{
  pst-plot,
  pst-ode
}

\psset{
  unit=5,
  algebraic,
  algebraicOutputFormat
}

\begin{document}

\begin{figure}[htbp]
 \centering
  \begin{pspicture}(-1.1,-1.1)(1.1,1.1)
   \psaxes[
     dx=0.2,
     Dx=1,
     dy=0.2,
     Dy=1
   ]{->}(0,0)(-1.1,-1.1)(1.1,1.1)
    \psVectorfield[
      arrows=->,
      linecolor=black!60
    ](-1,-1)(1,1){x}
    \pstODEsolve{y0_a}{t | x[0]}{-1}{1}{100}{-0.1}{t}
    \pstODEsolve{y0_b}{t | x[0]}{-1}{1}{100}{0.1}{t}
    \pstODEsolve{y0_c}{t | x[0]}{-1}{1}{100}{0.3}{t}
    \pstODEsolve{y0_d}{t | x[0]}{-1}{1}{100}{0.5}{t}
    \pstODEsolve{y0_e}{t | x[0]}{-1}{1}{100}{0.7}{t}
    \pstODEsolve{y0_f}{t | x[0]}{-1}{1}{100}{0.9}{t}
   \psset{
     arrows=-,
     linewidth=1.5pt
   }
    \listplot[linecolor=gray  ]{y0_a}
    \listplot[linecolor=red   ]{y0_b}
    \listplot[linecolor=green ]{y0_c}
    \listplot[linecolor=blue  ]{y0_d}
    \listplot[linecolor=purple]{y0_e}
    \listplot[linecolor=yellow]{y0_f}
  \end{pspicture}
 \caption{Vektorfeltet~$\mathbf{F}(x,y) = (1,x)$ med nogle str{\o}mningskurver.}
\end{figure}

\end{document}

output2

The problem is that the picture is really only drawn from -1 to 1 on both axes (but I have changed the axes labels). This means that the vector arrows and the curves are too 'flat' compared to if they were drawn from -5 to 5 on both axes.

I tried to change the code to

\documentclass{article}

\usepackage{
  pst-plot,
  pst-ode
}

\psset{
  unit=1,
  algebraic,
  algebraicOutputFormat
}

\begin{document}

\begin{figure}[htbp]
 \centering
  \begin{pspicture}(-5.7,-5.7)(5.7,5.7)
   \psaxes[
     dx=1,
     Dx=1,
     dy=1,
     Dy=1
   ]{->}(0,0)(-5.5,-5.5)(5.5,5.5)
    \psVectorfield[
      arrows=->,
      linecolor=black!60
    ](-5,-5)(5,5){x}
    \pstODEsolve{y0_a}{t | x[0]}{-5}{5}{100}{-0.5}{t}
    \pstODEsolve{y0_b}{t | x[0]}{-5}{5}{100}{0.5}{t}
    \pstODEsolve{y0_c}{t | x[0]}{-5}{5}{100}{1.5}{t}
    \pstODEsolve{y0_d}{t | x[0]}{-5}{5}{100}{2.5}{t}
    \pstODEsolve{y0_e}{t | x[0]}{-5}{5}{100}{3.5}{t}
    \pstODEsolve{y0_f}{t | x[0]}{-5}{5}{100}{4.5}{t}
   \psset{
     arrows=-,
     linewidth=1.5pt
   }
    \listplot[linecolor=gray  ]{y0_a}
    \listplot[linecolor=red   ]{y0_b}
    \listplot[linecolor=green ]{y0_c}
    \listplot[linecolor=blue  ]{y0_d}
    \listplot[linecolor=purple]{y0_e}
    \listplot[linecolor=yellow]{y0_f}
  \end{pspicture}
 \caption{Vektorfeltet~$\mathbf{F}(x,y) = (1,x)$ med nogle str{\o}mningskurver.}
\end{figure}

\end{document}

but then I get the error

Runaway definition?
->/ArrowA { moveto } def /ArrowB { BeginArrow 1.  1.  scale  false 0.\ETC.
! TeX capacity exceeded, sorry [main memory size=5000000].
\pst@code ....setopacityalpha  Arrow  EndArrow  } 
                                                  def  
l.28     ](-5,-5)(5,5){x}

How do I fix this?

Update 2

Here is what I ended up with:

\documentclass{article}

\usepackage{pst-plot}
\usepackage{pst-ode}

\begin{document}

\begin{figure}[htbp]
\centering
 \psset{
   algebraic,
   algebraicOutputFormat
 }
  \begin{pspicture}(-5.47,-5.47)(5.85,5.9)      
    \psaxes[
      linecolor=lightgray
    ]{->}(0,0)(-5.5,-5.5)(5.5,5.5)[$x$,0][$y$,90]
    \psVectorfield[
      Ox=10,
      Dx=1,
      Dy=1,
      linecolor=blue
    ](-5,-5)(5,5){x}
    \pstODEsolve{y0_a}{t | x[0]}{-2.1}{2.1}{100}{5.205}{t}
    \pstODEsolve{y0_b}{t | x[0]}{-2.533}{2.533}{100}{5.208}{t}
    \pstODEsolve{y0_c}{t | x[0]}{-2.9}{2.9}{100}{5.205}{t}
    \pstODEsolve{y0_d}{t | x[0]}{-3.226}{3.226}{100}{5.2025}{t}
    \pstODEsolve{y0_e}{t | x[0]}{-3.523}{3.523}{100}{5.2055}{t}
    \pstODEsolve{y0_f}{t | x[0]}{-3.797}{3.797}{100}{5.208}{t}
    \pstODEsolve{y0_g}{t | x[0]}{-4.051}{4.051}{100}{5.205}{t}
   \psset{
     linewidth=1.5pt
   }
    \listplot[linecolor=red!20]{y0_a}
    \listplot[linecolor=red!30]{y0_b}
    \listplot[linecolor=red!40]{y0_c}
    \listplot[linecolor=red!50]{y0_d}
    \listplot[linecolor=red!60]{y0_e}
    \listplot[linecolor=red!70]{y0_f}
    \listplot[linecolor=red!80]{y0_g}
%    \listplot[linecolor=purple]{y0_a}
%    \listplot[linecolor=green ]{y0_b}
%    \listplot[linecolor=red   ]{y0_c}
%    \listplot[linecolor=blue  ]{y0_d}
%    \listplot[linecolor=orange]{y0_e}
%    \listplot[linecolor=gray  ]{y0_f}
%    \listplot[linecolor=yellow]{y0_g}
  \end{pspicture}
\end{figure}

\end{document}

output2

Update 3

When I use the procedure mentioned by AlexG in the comment to the answer by Marienplatz, I still get an error when compiling from ps to pdf, i.e. when invoking ps2pdf, (but the PDF file looks okay):

\documentclass{article}

\usepackage{pst-plot}
\usepackage{pst-ode}

\begin{document}

\begin{figure}[htbp]
\centering
 \psset{
   algebraic,
   algebraicOutputFormat
 }
  \begin{pspicture}(-5.47,-5.47)(5.85,5.9)      
    \psVectorfield[
      Ox=10,
      Dx=1,
      Dy=1,
      linecolor=blue
    ](-5,-5)(5,5){x}
    \pstODEsolve{y0_a}{t | x[0]}{-2.1}{2.1}{100}{5.205}{t}
    \pstODEsolve{y0_b}{t | x[0]}{-2.533}{2.533}{100}{5.208}{t}
    \pstODEsolve{y0_c}{t | x[0]}{-2.9}{2.9}{100}{5.205}{t}
    \pstODEsolve{y0_d}{t | x[0]}{-3.226}{3.226}{100}{5.2025}{t}
    \pstODEsolve{y0_e}{t | x[0]}{-3.523}{3.523}{100}{5.2055}{t}
    \pstODEsolve{y0_f}{t | x[0]}{-3.797}{3.797}{100}{5.208}{t}
    \pstODEsolve{y0_g}{t | x[0]}{-4.051}{4.051}{100}{5.205}{t}
    \psaxes[
      linecolor=lightgray
    ]{->}(0,0)(-5.5,-5.5)(5.5,5.5)[$x$,0][$y$,90]
   \psset{
     linewidth=1.5pt
   }
    \listplot[linecolor=red!20]{y0_a}
    \listplot[linecolor=red!30]{y0_b}
    \listplot[linecolor=red!40]{y0_c}
    \listplot[linecolor=red!50]{y0_d}
    \listplot[linecolor=red!60]{y0_e}
    \listplot[linecolor=red!70]{y0_f}
    \listplot[linecolor=red!80]{y0_g}
%    \listplot[linecolor=purple]{y0_a}
%    \listplot[linecolor=green ]{y0_b}
%    \listplot[linecolor=red   ]{y0_c}
%    \listplot[linecolor=blue  ]{y0_d}
%    \listplot[linecolor=orange]{y0_e}
%    \listplot[linecolor=gray  ]{y0_f}
%    \listplot[linecolor=yellow]{y0_g}
  \end{pspicture}
\end{figure}

\end{document}

The error is as follows:

pstODEsolve RKF45 method; '-' failed step, '+' successful step, 'o' output step : 
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
pstODEsolve RKF45 method; '-' failed step, '+' successful step, 'o' output step : 
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
pstODEsolve RKF45 method; '-' failed step, '+' successful step, 'o' output step : 
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
pstODEsolve RKF45 method; '-' failed step, '+' successful step, 'o' output step : 
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
pstODEsolve RKF45 method; '-' failed step, '+' successful step, 'o' output step : 
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
pstODEsolve RKF45 method; '-' failed step, '+' successful step, 'o' output step : 
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
pstODEsolve RKF45 method; '-' failed step, '+' successful step, 'o' output step : 
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooError: /invalidrestore in --restore--
Operand stack:
   (1)   1786   1388   5.0   (2)   1786   1152   5.0   (3)   1786   916   5.0   (4)   1786   679   5.0   (5)   1721   2093   5.0   (1)   1721   2329   5.0   (2)   1721   2566   5.0   (3)   1721   2802   5.0   (4)   1721   3038   5.0   (5)   2117   1962   5.0   (5)   1902   1834   5.0   (x)   1902   1834   5.0   (y)   1902   1834   5.0   --nostringval--   --nostringval--
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1884   1   3   %oparray_pop   1883   1   3   %oparray_pop   1867   1   3   %oparray_pop   1755   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   1807   53   4   %oparray_pop   --nostringval--   1791   53   4   %oparray_pop   --nostringval--
Dictionary stack:
   --dict:1171/1684(ro)(G)--   --dict:8/20(G)--   --dict:117/200(L)--   --dict:187/300(L)--
Current allocation mode is local
Last OS error: Numerical result out of range
Current file position is 259609
GPL Ghostscript 9.10: Unrecoverable error, exit code 1
share|improve this question
5  
The pgfplots package has these plots under the name quiver. There are also PSTricks solution in there. –  Qrrbrbirlbel Nov 3 '13 at 3:06
    
@Qrrbrbirlbel Thank you. Will you make this comment into an answer or should we just close it? –  Svend Tveskæg Nov 3 '13 at 3:28
add comment

1 Answer 1

up vote 5 down vote accepted
\documentclass[pstricks,border=12pt]{standalone}

\usepackage{pst-plot}
\usepackage{pst-ode}

\psset{algebraic,algebraicOutputFormat}

\begin{document}
\begin{pspicture}(-5.7,-14.7)(5.7,5.7)      
    \psaxes[linecolor=lightgray]{->}(0,0)(-5.5,-14.5)(5.5,5.5)[$x$,0][$y$,90]
    \psVectorfield[Dx=1,Dy=1,Ox=10](-5,-14.5)(5,5){x}
    \pstODEsolve{y0_a}{t | x[0]}{-5}{5}{100}{-0.5}{t}
    \pstODEsolve{y0_b}{t | x[0]}{-5}{5}{100}{0.5}{t}
    \pstODEsolve{y0_c}{t | x[0]}{-5}{5}{100}{1.5}{t}
    \pstODEsolve{y0_d}{t | x[0]}{-5}{5}{100}{2.5}{t}
    \pstODEsolve{y0_e}{t | x[0]}{-5}{5}{100}{3.5}{t}
    \pstODEsolve{y0_f}{t | x[0]}{-5}{5}{100}{4.5}{t}
    \psset{linewidth=1.5pt}
    \listplot[linecolor=gray  ]{y0_a}
    \listplot[linecolor=red   ]{y0_b}
    \listplot[linecolor=green ]{y0_c}
    \listplot[linecolor=blue  ]{y0_d}
    \listplot[linecolor=purple]{y0_e}
    \listplot[linecolor=yellow]{y0_f}
\end{pspicture}
\end{document}

enter image description here

My compilation steps

echo off    
rem %1 TeX input filename without extension

latex "%~1.tex"

dvips -t unknown "%~1.dvi"

rem I invoke ghostscript directly rather than via ps2pdf

gswin64c -dCompatibilityLevel=1.5 -dAutoRotatePages=/None -sDEVICE=pdfwrite -o "%~1.pdf" "%~1.ps"
share|improve this answer
    
Thank you. I guess you compiled using xelatex; I tried latex --> dvips --> ps2pdf and started to get a bit frustrated. :) –  Svend Tveskæg Nov 3 '13 at 14:22
    
Hmmm! Then I get some wierd output and an compile error; with xelatex, it works all fine. –  Svend Tveskæg Nov 3 '13 at 14:27
    
@SvendTveskæg: Even with calling gs directly the error occurs. Just move the \psaxes line behind the \pstODEsolve block and latex --> dvips --> ps2pdf works fine. –  AlexG Nov 4 '13 at 8:00
    
@AlexG Please see update 3 to my question. –  Svend Tveskæg Nov 4 '13 at 8:17
    
@SvendTveskæg: There is some sort of interference between \psVectorfield and \psaxes. Just swap both and the error disappears. You may want to temporarily comment out the \pstODEsolve block to see that it is not related to the pst-ode package. I think you should report this to Herbert. –  AlexG Nov 4 '13 at 9:08
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.