在 TikZ 中绘制等值线的斜率

在 TikZ 中绘制等值线的斜率

我需要绘制 ODE 的等值线($y' = x^2 - y^2$在我的示例中)。我使用的代码来自这个问题(经过小改动)并且效果很好但我遇到了三个问题。

  1. 我无法在绿色等值线上绘制斜率(函数g5(x)g6(x)代码中),因为x时未定义变量-1 < x < 1。是否可以\foreach“跳过”间隔(0,1)?我尝试使用类似这样的方法:\ifnum \xmin+\i*\hx < -1 draw...\ifnum仅适用于整数。我也尝试过使用,\breakforeach但没有用。
  2. 是否可以将斜率的中心放在等斜线上?设(x0,y0)是斜率的左端,设(x1,y1)是斜率的右端。我猜如果我进行平移,每个斜率的中心都会在相应的等斜线上。但是当斜率的右端x0 -> x0 - abs(x0 - (x0+x1)/2), y0 -> y0 - abs(y0 - (y0+y1)/2)有时,我不知道如何在 LaTeX 代码中将其写入。atan2
  3. 左侧绿色曲线上有一个小间隙。右侧绿色曲线上没有这样的间隙。

平均能量损失

\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

绿色曲线通过旋转从红色曲线中出现。以下内容可以进一步简化,但恕我直言,您的问题写得不是很清楚。可以使用键将东西放在路径中间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

({\xmin+\i*\hx},{g1(\xmin+\i*\hx)}) -- ++ ({atan2(0,1)}:0.15)在@user121799的帮助下,我得到了我想要的。只需更改为将斜坡中心放置在等斜线上即可({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}

相关内容