15

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. – MaoYiyi Oct 16 '13 at 4:01
  • Fixed my question. You have $\frac{dy}{dx}=$some equation. Draw a graph of all the possible solution curves. – MaoYiyi Oct 16 '13 at 4:21
  • 2
    pst-ode package is written for solving your problem. – kiss my armpit Oct 16 '13 at 5:17
  • 1
    @Marienplatz, could you post an example? I'll upvote it like the GOD you are! – MaoYiyi Oct 16 '13 at 5:23
  • 2
    @MaoYiyi: pst-ode example is here: tex.stackexchange.com/a/139140 – AlexG Oct 16 '13 at 14:37
18

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$}
]
\addplot3 (x,y,0);
\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
]
\addplot3 (x,y,0);
\addplot +[domain=0.001:1.1] {x^(2.5)/2.5+0.15};
\end{axis}
\end{tikzpicture}

\end{document} 
  • 1
    Is it possible to set up the y domain depending of x, for example, y domain=0 : cos(x)? – Sigur Jun 24 '16 at 2:31
4

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:

Screenshot

3

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}

stream lines of a vector field

2

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}

enter image description here

enter image description here

enter image description here

  • 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? – Viktor Glombik Jul 27 '19 at 11:07
  • Also, how can I solutions with asymptotes and still main the different sizes of vector arrows? – Viktor Glombik Jul 27 '19 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) – Black Mild Jul 27 '19 at 15:23
  • @ViktorGlombik You can make a new question (click to Ask Question button) with tag Asymptote – Black Mild Jul 28 '19 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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