\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 軸上繪製向量?
- 如何在圖表上繪製特定半徑的圓(我想出的方法顯然不起作用!)?
感謝您的任何幫助。
編輯:我在文件類中添加了投影儀,但它的工作原理與文章相同。
答案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 程式碼。這是連結。