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:
\documentclass{report}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\tikzexternalenable
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=2*\x/(\x^4+1);}]
\begin{axis}[
xmin=-4,xmax=4,
ymin=-4,ymax=4,
domain=-4:4,
]
\def\xmax{4} \def\xmin{-4}
\def\ymax{4} \def\ymin{-4}
\def\nx{15}
\def\ny{15}
\def\yo{1}
\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
\pgfmathsetmacro{\yprime}{f({\xmin+\i*\hx})}
\addplot [blue,shift={({\xmin+\i*\hx},{\ymin+\j*\hy})}]
(0,0)--($(0,0)!2mm!(.1,.1*\yprime)$);
}
\addplot [<->,red] {-1/4*pi+rad(atan(x^2))+1};
\end{axis}
\end{tikzpicture}
\end{document}
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:
\foreachdoes not work as you expect in\axisuse\pgfplotsinvokeforeach.shiftis not a suitable option to\addplot. You need to specify plot type. You can not just use--inside a plot. Maybe you want aquiverplot? It is difficult to tell, what you are trying to do. Can you draw a picture of the desired output? – hpekristiansen Mar 09 '24 at 00:22