0

I need to draw isoclines for an ODE ($y' = x^2 - y^2$ in my example). I use the code from this question (with small changes) and it works pretty well but there are three problems that I came across.

  1. I can't draw slopes on the green isoclines (functions g5(x) and g6(x) in the code) because x variable is not defined when -1 < x < 1. Is it possible to make \foreach "skip" an interval (0,1)? I tried to use something like this: \ifnum \xmin+\i*\hx < -1 draw... but \ifnum works only with integers. I also tried to use \breakforeach but it didn't work.
  2. Is it possible to put centers of the slopes on isoclines? Let (x0,y0) be the left end of the slope, and (x1,y1) be the right end of the slope. I guess the center of each slope would be on the corresponding isocline if I make a translation x0 -> x0 - abs(x0 - (x0+x1)/2), y0 -> y0 - abs(y0 - (y0+y1)/2). But I don't know how to write it in LaTeX code when there is atan2 for the right end of the slope.
  3. There is a small gap on the left green curve. There is no such gap on the right green curve.

MWE

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[declare function=
{f(\x,\y)=\x*\x-\y*\y;
g1(\x)=\x;
g2(\x)=-\x;
g3(\x)=sqrt(\x*\x+1);
g4(\x)=-sqrt(\x*\x+1);
g5(\x)=sqrt((\x*\x)-1);
g6(\x)=-sqrt((\x*\x)-1);
},
scale=2.5]


\def\xmax{2.0} \def\xmin{-2.0}
\def\ymax{2.0} \def\ymin{-2.0}
\def\nx{15}  \def\ny{15}

\draw[red] plot[domain=\xmin-0.1:\xmax+0.1] (\x,{g1(\x)});
\draw[red] plot[domain=\xmin-0.1:\xmax+0.1] (\x,{g2(\x)});
\draw[red] plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
\draw[red] plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
\draw[green] plot[samples=1000, smooth,domain=\xmin-0.1:-1.0] (\x,{g5(\x)});
\draw[green] plot[samples=100, smooth,domain=1:\xmax+0.1]  (\x,{g5(\x)});
\draw[green] plot[samples=1000, smooth,domain=\xmin-0.1:-1.0] (\x,{g6(\x)});
\draw[green] plot[samples=100, smooth,domain=1:\xmax+0.1]  (\x,{g6(\x)});

\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15);
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g2(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15);
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
}

\draw[-latex] (\xmin-.1,0)--(\xmax+.1,0) node[below right] {$x$};
\draw[-latex] (0,\ymin-.1)--(0,\ymax+.1) node[above left] {$y$};
\end{tikzpicture}
\end{document}

Isoclines

AndréC
  • 24,137
RubenM
  • 11

2 Answers2

2

The green curves emerge from the red ones by rotation. The following can be simplified further but IMHO your question is not very clearly written. Stuff can be put in the middle of a path with the midway key.

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
[declare function=
{f(\x,\y)=\x*\x-\y*\y;
g1(\x)=\x;
g2(\x)=-\x;
g3(\x)=sqrt(\x*\x+1);
g4(\x)=-sqrt(\x*\x+1);
},
scale=2.5]


\def\xmax{2.0} \def\xmin{-2.0}
\def\ymax{2.0} \def\ymin{-2.0}
\def\nx{15}  \def\ny{15}
\begin{scope}[red]
\draw plot[domain=\xmin-0.1:\xmax+0.1] (\x,{g1(\x)});
\draw plot[domain=\xmin-0.1:\xmax+0.1] (\x,{g2(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
\end{scope}
\begin{scope}[green,rotate=90]
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
\draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
\end{scope}

\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
    node[midway,sloped]{$\times$};
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g2(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
    node[midway,sloped]{$\times$};
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15)
    node[midway,sloped]{$\times$};
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15)
    node[midway,sloped]{$\times$};
}
\begin{scope}[rotate=90]
\foreach \i in {0,...,\nx}
\foreach \j in {0,...,\ny}{
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
    node[midway,sloped]{$\times$};
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g2(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
    node[midway,sloped]{$\times$};
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15)
    node[midway,sloped]{$\times$};
    \draw[blue,-] 
    ({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15)
    node[midway,sloped]{$\times$};
}
\end{scope}
\draw[-latex] (\xmin-.1,0)--(\xmax+.1,0) node[below right] {$x$};
\draw[-latex] (0,\ymin-.1)--(0,\ymax+.1) node[above left] {$y$};
\end{tikzpicture}
\end{document}

enter image description here

1

With @user121799 's help, I got what I wanted. It is enough to change ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15) with ({atan2(0,1)}:-0.075) ++ ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15) to place centers of the slopes on the isoclines. Maybe it will be useful to someone.

enter image description here

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
    [declare function=
    {f(\x,\y)=\x*\x-\y*\y;
        g1(\x)=\x;
        g2(\x)=-\x;
        g3(\x)=sqrt(\x*\x+1);
        g4(\x)=-sqrt(\x*\x+1);
    },
    scale=2.5]


    \def\xmax{2.0} \def\xmin{-2.0}
    \def\ymax{2.0} \def\ymin{-2.0}
    \def\nx{15}  \def\ny{15}
    \begin{scope}[red]
    \draw plot[domain=\xmin-0.3:\xmax+0.3] (\x,{g1(\x)});
    \draw plot[domain=\xmin-0.3:\xmax+0.3] (\x,{g2(\x)});
    \draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
    \draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
    \end{scope}
    \begin{scope}[red,rotate=90]
    \draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g3(\x)});
    \draw plot[samples=100, smooth,domain=\xmin-0.1:\xmax+0.1] (\x,{g4(\x)});
    \end{scope}

    \pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
    \pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
    \foreach \i in {0,...,\nx}
    \foreach \j in {0,...,\ny}{
        \draw[blue,-] 
        ({atan2(0,1)}:-0.075) ++ ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15);
        \draw[blue,-] 
        ({atan2(0,1)}:-0.075) ++ ({\xmin+\i*\hx},{g2(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15);
        \draw[blue,-] 
        ({atan2(-1,1)}:-0.075) ++ ({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
        \draw[blue,-] 
        ({atan2(-1,1)}:-0.075) ++ ({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
    }
    \begin{scope}[rotate=90]
    \foreach \i in {0,...,\nx}
    \foreach \j in {0,...,\ny}{
        \draw[blue,-] 
        ({atan2(-1,1)}:-0.075) ++ ({\xmin+\i*\hx},{g3(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
        \draw[blue,-] 
        ({atan2(-1,1)}:-0.075) ++ ({\xmin+\i*\hx},{g4(\xmin+\i*\hx)}) -- ++ ({atan2(-1,1)}:0.15);
    }
    \end{scope}


    \draw[-latex] (\xmin-.1,0)--(\xmax+.1,0) node[below right] {$x$};
    \draw[-latex] (0,\ymin-.1)--(0,\ymax+.1) node[above left] {$y$};
    \end{tikzpicture}
\end{document}
RubenM
  • 11