# How to draw slope fields with all the possible solution curves in latex

Sorry, I have no code to submit, mostly because I have no idea how to do this.

I want to type up my homework (I can do the homework by hand) and I am looking for a simple method that I can use repetitively on different equations; without having to code each equation; to make/produce/draw the slope field of that equation.

Currently, I have typed up the rest of my homework with latex, but have no good example from the internet to follow. I want to put the slope fields for both $\frac{\mathrm{d}y}{\mathrm{d}x}=2x$ and $\frac{\mathrm{d}y}{\mathrm{d}x}=x\sqrt{x}$ into my homework.

My system is Fedora 19.

Any suggestions?

ps. sorry if this is a poor quality question. I just used the suggested tags.

• @texenthusiast Honestly, I have no idea which tags to use, so I used the suggested one. Commented Oct 16, 2013 at 4:01
• Fixed my question. You have $\frac{dy}{dx}=$some equation. Draw a graph of all the possible solution curves. Commented Oct 16, 2013 at 4:21
• pst-ode package is written for solving your problem. Commented Oct 16, 2013 at 5:17
• @Marienplatz, could you post an example? I'll upvote it like the GOD you are! Commented Oct 16, 2013 at 5:23
• @MaoYiyi: pst-ode example is here: tex.stackexchange.com/a/139140 Commented Oct 16, 2013 at 14:37

You can use PGFPlots' quiver plot style for drawing the vector fields.

I'm not entirely sure what you mean by "all possible solution curves", since that would just cover the whole plot area. I just drew one possible solution for each equation, all others would just be vertically shifted versions:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\usepackage{amsmath}

\pgfplotsset{ % Define a common style, so we don't repeat ourselves
MaoYiyi/.style={
width=0.6\textwidth, % Overall width of the plot
axis equal image, % Unit vectors for both axes have the same length
view={0}{90}, % We need to use "3D" plots, but we set the view so we look at them from straight up
xmin=0, xmax=1.1, % Axis limits
ymin=0, ymax=1.1,
domain=0:1, y domain=0:1, % Domain over which to evaluate the functions
xtick={0,0.5,1}, ytick={0,0.5,1}, % Tick marks
samples=11, % How many arrows?
cycle list={    % Plot styles
gray,
quiver={
u={1}, v={f(x)}, % End points of the arrows
scale arrows=0.075,
every arrow/.append style={
-latex % Arrow tip
},
}\\
red, samples=31, smooth, thick, no markers, domain=0:1.1\\ % The plot style for the function
}
}
}

\begin{document}
\begin{tikzpicture}[
declare function={f(\x) = 2*\x;} % Define which function we're using
]
\begin{axis}[
MaoYiyi, title={$\dfrac{\mathrm{d}y}{\mathrm{d}x}=2x$}
]
\addplot {x^2+0.15}; % You need to find the antiderivative yourself, unfortunately. Good exercise!
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}[
declare function={f(\x) = \x*sqrt(\x);}
]
\begin{axis}[
MaoYiyi,
title={$\dfrac{\mathrm{d}y}{\mathrm{d}x}=x\sqrt{x}$},
ytick=\empty
]
\end{axis}
\end{tikzpicture}

\end{document}

• Is it possible to set up the y domain depending of x, for example, y domain=0 : cos(x)? Commented Jun 24, 2016 at 2:31

Sage includes the python code for stream lines, a much prettier way to draw stream lines (also called flow lines, or integral curves) of a vector field in the plane.

\documentclass{amsart}
\usepackage{sagetex}
\begin{document}
An elegant plot of the stream lines of the vector field $$\sin x \partial_x + \cos y \partial y$$.
\begin{sagesilent}
x, y = var('x y')
\end{sagesilent}
\begin{center}
\sageplot[width=\textwidth]{streamline_plot((sin(x), cos(y)), (x,-3,3), (y,-3,3))}
\end{center}
\end{document}


• This is beautiful and elegant. I am using this
– eem
Commented Mar 16, 2022 at 0:37

As Charles0349 said above, it is natural way to draw slope field. Moreover, we can control the length of velocity vector of the vector field.

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[declare function={f(\x,\y)=\x+\y;}]
\def\xmax{3} \def\xmin{-3}
\def\ymax{3} \def\ymin{-3}
\def\nx{15}  \def\ny{15}

\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},{\ymin+\j*\hy})}
\draw[teal,-stealth,shift={({\xmin+\i*\hx},{\ymin+\j*\hy})}] (0,0)--(.1,.1*\yprime);
}

% a solution y=(yo+1)e^x-x-1
\def\yo{1}
\draw[magenta] plot[domain=\xmin:1] (\x,{(\yo+1)*exp(\x)-\x-1});

\draw[->] (\xmin-.5,0)--(\xmax+.5,0) node[below right] {$x$};
\draw[->] (0,\ymin-.5)--(0,\ymax+.5) node[above left] {$y$};
\draw (current bounding box.north) node[above]
{Slope field of \quad $y'=x+y$.};
\end{tikzpicture}

\begin{tikzpicture}[declare function={f(\x,\y)=\x+\y;}]
\def\xmax{3} \def\xmin{-3}
\def\ymax{3} \def\ymin{-3}
\def\nx{15}
\def\ny{15}

\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},{\ymin+\j*\hy})}
\draw[blue,shift={({\xmin+\i*\hx},{\ymin+\j*\hy})}]
(0,0)--($(0,0)!2mm!(.1,.1*\yprime)$);
}

% a solution y=(yo+1)e^x-x-1
\def\yo{1}
\draw[magenta] plot[domain=\xmin:.9] (\x,{(\yo+1)*exp(\x)-\x-1});

\draw[->] (\xmin-.5,0)--(\xmax+.5,0) node[below right] {$x$};
\draw[->] (0,\ymin-.5)--(0,\ymax+.5) node[above left] {$y$};
\draw (current bounding box.north) node[above]
{Slope field of \quad $y'=x+y$.};
\end{tikzpicture}
\end{document}


• Your code only produces the first two images. Is the difference between the second and the last only that you added a parameter -> to the draw command? Commented Jul 27, 2019 at 11:07
• Also, how can I solutions with asymptotes and still main the different sizes of vector arrows? Commented Jul 27, 2019 at 12:51
• @ViktorGlombik in the last picture, all arrows have the same length. In the same manner, you can draw with Asymptote. (although Áymptote have the slopefield module for that) Commented Jul 27, 2019 at 15:23
• @ViktorGlombik You can make a new question (click to Ask Question button) with tag Asymptote Commented Jul 28, 2019 at 16:38
• This is a really nice answer. It could be improved bij adding comment lines to the code. This makes is easier for future readers to understand what is going on. Commented Jan 11, 2021 at 14:47

I'm not super good at LaTeX, but I think the following is a pretty straight-forward approach. This is a slope field for dy/dx = x + y.

\documentclass[border=5pt,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[declare function={diff(\x,\y) = \x+\y;}]
\draw[->] (-5.2, 0) -- (5.2, 0) node[right] {$x$};
\draw[->] (0,-6.1) -- (0, 6.1) node[above] {$y$};
\foreach \i in {-5,-4,-3,-2,-1,0,1,2,3,4,5}
\foreach \j in {-5,-4,-3,-2,-1,0,1,2,3,4, 5}
{
\draw[thick] ( {\i -0.1}, {\j - diff(\i, \j) *0.1}) --  ( {\i +0.1}, {\j  + diff(\i, \j) *0.1});
}
\end{tikzpicture}
\end{document}


Here is the output: