
使用 pgfplots,我繪製了函數 f(x,y) 的等值線:諸如 f(x,y)=C 的線條,其中 C 是選定的數字。
請參閱下面的範例:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.85]
\begin{axis}[ xmin=-4,xmax=12, ymin=-4,ymax=4,x=1cm,y=1cm,at={(-4cm,-4cm)}]]
\addplot +[no markers,
raw gnuplot,
thick,dashed,
empty line = jump, % not strictly necessary, as this is the default behaviour in the development version of PGFPlots
] gnuplot {
set contour base;
set cntrparam levels discrete -2,-1.1,-1.4;
unset surface;
set view map;
set isosamples 500;
set samples 500;
splot -2/sqrt((x-7.5)^2+y^2)-3/sqrt((x-0.5)^2+y^2);
};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
我可以在任何給定點繪製梯度向量,但我不知道如何將它們放置在等值線曲線上的不同位置。
我怎麼才能得到藍色虛曲線上各點的位置?
答案1
您可以利用可以存取等值線圖座標的事實。由於這些是幾個不相連的段落,僅指定pos
是不夠的。相反,您還需要使用pos segment
,這在第 14 頁中進行了解釋。 pgfplots 手冊 v1.16 的 358。然而,並不清楚哪個段具有哪個索引。為了簡化問題,我添加了一種添加漸變箭頭的樣式,該箭頭與此時輪廓的切線正交。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{center}
\begin{tikzpicture}[gradient arrow/.style={
insert path={coordinate[pos=#1,sloped,
above=\pgfkeysvalueof{/tikz/ga/above}] (aux-1)
coordinate[pos=#1,sloped,
above=\pgfkeysvalueof{/tikz/ga/above}+\pgfkeysvalueof{/tikz/ga/length}] (aux-2)
(aux-1) edge[/tikz/ga/arrow]
(aux-2)}},ga/.cd,
above/.initial=3pt,
length/.initial=12pt,
arrow/.style={-stealth,black,solid,thick}]
\begin{axis}[scale=0.85,xmin=-4,xmax=12, ymin=-4,ymax=4,x=1cm,y=1cm,at={(-4cm,-4cm)}]]
\addplot +[no markers,name=contour,
raw gnuplot,
thick,dashed,
empty line = jump, % not strictly necessary, as this is the default behaviour in the development version of PGFPlots
] gnuplot {
set contour base;
set cntrparam levels discrete -2,-1.1,-1.4;
unset surface;
set view map;
set isosamples 500;
set samples 500;
splot -2/sqrt((x-7.5)^2+y^2)-3/sqrt((x-0.5)^2+y^2);
}
[pos segment=1]
[gradient arrow/.list={0.2,0.8}]
[pos segment=3,/tikz/ga/arrow/.append style={red},/tikz/ga/length=10pt]
[gradient arrow/.list={0.2,0.8}];
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}