I have the following differential equation I would like to graph differential equation: dy/dx = \frac{2x}{x^4+1}. I would also like to graph the solution that goes through the point (1,1). Here's what I currently have:



\begin{tikzpicture}[declare function={f(\x)=2*\x/(\x^4+1);}]

    \def\xmax{4} \def\xmin{-4}
    \def\ymax{4} \def\ymin{-4}

    \foreach \i in {0,...,\nx}
    \foreach \j in {0,...,\ny}{
      \addplot [blue,shift={({\xmin+\i*\hx},{\ymin+\j*\hy})}]

    \addplot [<->,red] {-1/4*pi+rad(atan(x^2))+1};

But I keep getting the following error:

! Missing number, treated as zero.
<to be read again> 
l.30     }

I originally had \pgfmathsetmacro{\yprime}{f({\xmin+\i*\hx},{\ymin+\j*\hy})} in replace of \pgfmathsetmacro{\yprime}{f({\xmin+\i*\hx})} because I don't need to pass in the \y since the function doesn't use it. But even when I do so and update the function definition from f(\x) = ... to f(\x,\y) = ..., the program still crashes giving:

! Undefined control sequence.
\pgfmath@dimen@ ...men@@ #1=0.0pt\relax \pgfmath@ 
l.32   \end{axis}

I'm aware of the following questions. I've used them and none of them worked out:

  1. How to draw slope fields with all the possible solution curves in latex
  2. How to plot a function and its derivative
  3. Differential Equation direction plot with pgfplots
  • Have you looked at tex.stackexchange.com/questions/170664/… ? No time now, but probably there is a problem there...
    – Rmano
    Commented Jan 16 at 10:20
  • I don't think that that's my problem though. Commented Jan 16 at 11:09
  • Hi. First, I think that your equation can be solved explicitly (using a change of variable $t=x^2$). Then you can draw the solution. Second, I gave some answers that can be applied to such problems; see (1) and (2).
    – Daniel N
    Commented Jan 17 at 18:20
  • \foreach does not work as you expect in \axis use \pgfplotsinvokeforeach. shift is not a suitable option to \addplot. You need to specify plot type. You can not just use -- inside a plot. Maybe you want a quiver plot? It is difficult to tell, what you are trying to do. Can you draw a picture of the desired output? Commented Mar 9 at 0:22
  • Please add some more information on your desired output Commented Mar 11 at 22:11


Browse other questions tagged .