![Рисование уклонов на изоклинах в TikZ](https://rvso.com/image/399812/%D0%A0%D0%B8%D1%81%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5%20%D1%83%D0%BA%D0%BB%D0%BE%D0%BD%D0%BE%D0%B2%20%D0%BD%D0%B0%20%D0%B8%D0%B7%D0%BE%D0%BA%D0%BB%D0%B8%D0%BD%D0%B0%D1%85%20%D0%B2%20TikZ.png)
Мне нужно нарисовать изоклины для ОДУ ( $y' = x^2 - y^2$
в моем примере). Я использую код изэтот вопрос(с небольшими изменениями) и работает довольно хорошо, но я столкнулся с тремя проблемами.
- Я не могу нарисовать уклоны на зеленых изоклинах (функции
g5(x)
иg6(x)
в коде), потому чтоx
переменная не определена при-1 < x < 1
. Можно ли сделать так, чтобы\foreach
"пропустить" интервал(0,1)
? Я пытался использовать что-то вроде этого:\ifnum \xmin+\i*\hx < -1 draw...
но\ifnum
работает только с целыми числами. Я также пытался использовать ,\breakforeach
но это не сработало. - Можно ли поместить центры склонов на изоклины? Пусть
(x0,y0)
будет левым концом склона, а(x1,y1)
будет правым концом склона. Я предполагаю, что центр каждого склона будет на соответствующей изоклине, если я сделаю переводx0 -> x0 - abs(x0 - (x0+x1)/2), y0 -> y0 - abs(y0 - (y0+y1)/2)
. Но я не знаю, как это записать в коде LaTeX, когда естьatan2
для правого конца склона. - На левой зеленой кривой есть небольшой зазор. На правой зеленой кривой такого зазора нет.
МВЭ
\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}
решение1
Зелёные кривые возникают из красных вращением. Следующее можно упростить ещё больше, но IMHO ваш вопрос не очень ясно написан. Вещи можно поместить в середину пути с помощью ключа midway
.
\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}
решение2
С помощью @user121799 я получил то, что хотел. Достаточно изменить ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
на ({atan2(0,1)}:-0.075) ++ ({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)
, чтобы разместить центры склонов на изоклинах. Может быть, кому-то это будет полезно.
\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}