\documentclass{beamer}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{frame}
\def\length{sqrt(1+(-x/y)^2)}
\begin{tikzpicture}
\begin{axis}[
domain=-3:3,
view={0}{90},
title={$\displaystyle\frac{dy}{dx}=-\frac{x}{y}$},
xmin=-3,
xmax=3,
ymin=-3,
ymax=3,
ytick={-3,-2,-1,0,1,2,3}
]
\addplot3[gray, quiver={u={1/(\length)}, v={(-x/y)/(\length)}, scale arrows=0.15}, -,samples=40] {0};
\addplot +[blue, samples=31, smooth, thick, no markers, domain=-sqrt(2):sqrt(2)]{sqrt(2-x^2)};
\addplot +[red, samples=31, smooth, thick, no markers, domain=-2:2]{sqrt(4-x^2)};
\addplot +[orange, samples=31, smooth, thick, no markers, domain=-sqrt(6):sqrt(6)]{sqrt(6-x^2)};
\addplot +[blue, samples=31, smooth, thick, no markers, domain=-sqrt(2):sqrt(2)]{-sqrt(2-x^2)};
\addplot +[red, samples=31, smooth, thick, no markers, domain=-2:2]{-sqrt(4-x^2)};
\addplot +[orange, samples=31, smooth, thick, no markers, domain=-sqrt(6):sqrt(6)]{-sqrt(6-x^2)};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
这是 $\frac{dy}{dx}=-\frac{x}{y}$ 的方向场。我有两个问题。
- 我如何让程序在负 x 轴上绘制矢量?
- 如何在图表上绘制特定半径的圆(我想到的方法显然不起作用!)?
谢谢您的帮助。
编辑:我在文档类中添加了 beamer,但它的工作方式与文章相同。
答案1
\documentclass{beamer}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{frame}
\def\length{sqrt(1+(-x/y)^2)}
\begin{tikzpicture}
\begin{axis}[
domain=-3:3,
view={0}{90},
title={$\displaystyle\frac{dy}{dx}=-\frac{x}{y}$},
xmin=-3,
xmax=3,
ymin=-3,
ymax=3,
ytick={-3,-2,-1,0,1,2,3}
]
\addplot3[gray, quiver={u={1/(\length)}, v={(-x/y)/(\length)}, scale arrows=0.1}, -{Stealth[width=2pt,length=2pt]},samples=24,domain=-3:3] {0};
\addplot[blue, samples=50, thick, no markers, domain=0:2*pi] ({cos(deg(x))},{sin(deg(x))});
\addplot[red, samples=50, thick, no markers, domain=0:2*pi] ({2*cos(deg(x))},{2*sin(deg(x))});
\addplot[orange, samples=50, thick, no markers, domain=0:2*pi] ({3*cos(deg(x))},{3*sin(deg(x))});
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
我不确定这对你来说是否足够。圆被绘制为参数图,即某种形式\addplot ({r*cos(x)}, {r*sin(x)});
x < 0 时箭头之间的间隙是由于坐标 (x,y) 指的是箭头的起点,据我所知,由于箭头指向相反的方向(见上图),因此会在那里出现间隙,而 x > 0 时会重叠。稍微减小箭头的缩放比例可以解决重叠问题。对于上图,样本数量减少到 24,如果您确实需要 40 个样本,则需要将箭头缩放更多,例如
\addplot3[gray, quiver={u={1/(\length)}, v={(-x/y)/(\length)}, scale arrows=0.07}, -{Stealth[width=1pt,length=1pt]},samples=40,domain=-3:3] {0};
在 x 轴上获得垂直箭头的一种方法是添加单独的箭头,其中 u=0,v=±1。箭头的缩放比例可能有所不同。
\documentclass{beamer}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{frame}
\def\length{sqrt(1+(-x/y)^2)}
\begin{tikzpicture}
\begin{axis}[
domain=-3:3,
view={0}{90},
title={$\displaystyle\frac{dy}{dx}=-\frac{x}{y}$},
xmin=-3,
xmax=3,
ymin=-3,
ymax=3,
ytick={-3,-2,-1,0,1,2,3}
]
\addplot3[gray, quiver={u={1/(\length)}, v={(-x/y)/(\length)}, scale arrows=0.07}, -{Stealth[width=1pt,length=1pt]},samples=20,domain=-3:3] {0};
\addplot3[gray, quiver={u={0}, v={1}, scale arrows=0.05}, -{Stealth[width=1pt,length=1pt]},samples=10,samples y=1,domain=-3:-0.17,y domain=0] {0};
\addplot3[gray, quiver={u={0}, v={-1}, scale arrows=0.05}, -{Stealth[width=1pt,length=1pt]},samples=10,samples y=1,domain=-3:-0.17,y domain=0] {0};
\addplot[blue, samples=50, thick, no markers, domain=0:2*pi] ({cos(deg(x))},{sin(deg(x))});
\addplot[red, samples=50, thick, no markers, domain=0:2*pi] ({2*cos(deg(x))},{2*sin(deg(x))});
\addplot[orange, samples=50, thick, no markers, domain=0:2*pi] ({3*cos(deg(x))},{3*sin(deg(x))});
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
答案2
该解决方案还解决了由于命令quiver
将pgfplots
箭头的尾部而不是中心放置在等距网格的每个点上而导致的箭头之间间距不一致的问题。
为了解决这个问题,我们可以先按通常的方式画出一个箭头,然后再画出第二个箭头,这个箭头指向的方向与第一个箭头完全相反,但是没有任何箭头头;也就是说,第二个“箭头”本质上只是一条线。这两个物体合在一起就构成了一个指向第一个箭头方向的箭头。这让人产生一种错觉,觉得箭头位于一个点的中心。
例如:
\begin{tikzpicture}[
declare function = {f(\x) = -\x/\y;} % Define which function we're using
]
\begin{axis}[
zmax = 1,
zmin = 0,
xtick = {-1,-0.5,...,1},
axis equal image = true, % Unit vectors for both axes have the same length
view = {0}{90}
]
\addplot3[% Arrows' first-half
blue,
-stealth,
domain = -1:1,
samples = 20,
quiver = {
u=1/(2*sqrt(1^2 + f(x)^2)),
v=f(x)/(2*sqrt(1^2 + f(x)^2)),
scale arrows=0.075,
},
] (x,y,0);
\addplot3[% Arrow's second-half
blue,
domain = -1:1,
samples = 20,
quiver = {
u=-1/(2*sqrt(1^2 + f(x)^2)),
v=-f(x)/(2*sqrt(1^2 + f(x)^2)),
scale arrows=0.075,
},
] (x,y,0);
\addplot [domain=-180:180, samples=100, color=red, thick] ({0.75*cos(x)},{0.75*sin(x)});
\end{axis}
\end{tikzpicture}
该解决方案的灵感来自马里兰大学的 Tobias von Petersdorff 编写的 Matlab 代码。以下是链接。