![使用 tikz 时斜率场不对称](https://linux22.com/image/402277/%E4%BD%BF%E7%94%A8%20tikz%20%E6%97%B6%E6%96%9C%E7%8E%87%E5%9C%BA%E4%B8%8D%E5%AF%B9%E7%A7%B0.png)
使用此代码尝试绘制 xy' - y = 0(即 y = cx)的斜率场,两侧明显缺乏对称性。
\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}
取自这里
我能够使用这画出我想要的,但我不喜欢输出。另外,我真的想知道为什么我发布的代码不对称。我甚至用 python 输入了它来尝试弄清楚:
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)
它没有帮助,但也许对其他人有用。
答案1
它不对称,因为线的末端不在
\draw[blue,shift={({\xmin+\i*0.4},{\ymin+\j*0.4})}]
(0,0)--($(0,0)!2mm!(.1,.1*\yprime)$);
使它们对称很简单,只需将其替换为
\draw[blue,shift={({\xmin+\i*0.4},{\ymin+\j*0.4})}]
($(0,0)!1mm!(-.1,-.1*\yprime)$)--($(0,0)!1mm!(.1,.1*\yprime)$);
完整 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}
附录:至于您的评论:您需要避免奇点。为了简化这一点,我稍微重写了代码,以便从输入参数计算步长。为了避免 处的奇点x=-1
,我将 设置\nx
为16
(并且 也设置为 以\ny
保持对称性)。
\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}