2

Using this code to try and plot the slope fields for xy' - y = 0 (which is y = cx) and there's a clear lack of symmetry on both sides.

\documentclass{article}

\usepackage{amsmath}            
\usepackage{amsfonts}           
\usepackage{amssymb}        
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[declare function={f(\x,\y)=\y/\x;}]
    \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*0.4},{\ymin+\j*0.4})}] 
    (0,0)--($(0,0)!2mm!(.1,.1*\yprime)$);
    }

    \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}

Taken from here

I was able to use this to draw what I wanted, but I don't like the output. Also I really want to know why the code I posted is not symmetric. I even typed it up in python to try and figure it out:

xmax = 3
xmin = -3
ymax = 3
ymin = -3
nx = 15
ny = 15
hx = (xmax - xmin)/nx
hy = (ymax - ymin)/ny
for i in range(0, nx+1):
    for j in range(0, ny+1):
        yprime = (xmin+(i*hx))/(ymin+(j*hy))

        if -1 == yprime or yprime == 1:
            print((xmin+(i*hx)))
            print((ymin+(j*hy)))

            print("xmin", xmin,"i", i,"hx", hx,"ymin", ymin,"j", j,"hy", hy)
            print ("yprime ", yprime)

It didn't help, but maybe it would be useful for someone else.

1 Answers1

3

It is not symmetric because the ends of the lines are not in

\draw[blue,shift={({\xmin+\i*0.4},{\ymin+\j*0.4})}] 
(0,0)--($(0,0)!2mm!(.1,.1*\yprime)$);

Making them symmetric is as easy as replacing this by

     \draw[blue,shift={({\xmin+\i*0.4},{\ymin+\j*0.4})}] 
    ($(0,0)!1mm!(-.1,-.1*\yprime)$)--($(0,0)!1mm!(.1,.1*\yprime)$);

Full MWE:

\documentclass{article}

\usepackage{amsmath}            
\usepackage{amsfonts}           
\usepackage{amssymb}        
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[declare function={f(\x,\y)=\y/\x;}]
    \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*0.4},{\ymin+\j*0.4})}] 
    ($(0,0)!1mm!(-.1,-.1*\yprime)$)--($(0,0)!1mm!(.1,.1*\yprime)$);
    }

    \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

ADDENDUM: As for your comment: you need to avoid singularities. To make this simpler, I slightly rewrote the code such that the step length gets computed from the input parameters. To avoid the singularity at x=-1, I set \nx to 16 (and the same for \ny to keep the symmetry).

\documentclass{article}

\usepackage{amsmath}            
\usepackage{amsfonts}           
\usepackage{amssymb}        
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

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

    \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*(\xmax-\xmin)/\nx},{\ymin+\j*(\ymax-\ymin)/\ny})}] 
    ($(0,0)!1mm!(-.1,-.1*\yprime)$)--($(0,0)!1mm!(.1,.1*\yprime)$);
    }

    \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'=-2y/(x+1)$.};
    \end{tikzpicture}

\end{document}

enter image description here

  • Thanks!!! Wasted so much time, I'll be sure to post here sooner :) – MQStacked Sep 26 '19 at 20:40
  • If I have a solution to the ODE, how would I use that instead? Also I changed the function to y' = -2y/(x+1) and it fails terribly :( . – MQStacked Sep 26 '19 at 21:07
  • @MQStacked It fails because a some samples hit the singularity. I added a way to avoid this. (LaTeX is not a computer algebra system, so an automatic solution will be more difficult to obtain.) –  Sep 26 '19 at 23:35