![如何使用序列的集合公式在 TikZ 中绘制数轴?](https://linux22.com/image/352987/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E5%BA%8F%E5%88%97%E7%9A%84%E9%9B%86%E5%90%88%E5%85%AC%E5%BC%8F%E5%9C%A8%20TikZ%20%E4%B8%AD%E7%BB%98%E5%88%B6%E6%95%B0%E8%BD%B4%EF%BC%9F.png)
我想在 [0,1] 中绘制一条数轴,以便将节点放置在由贝叶斯规则(或任何特定函数)定义的数字上。因此,对于 x' 的初始值(由我指定),我希望程序将节点放置在每个后续节点 x 处,其中 x= x'/((1+x')*0.5) 依此类推,直到最大 x 为 1。这是我从另一篇类似的帖子中设计的 MWE:
\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=none,
width=\textwidth,
xmin=0,xmax=1,
xtick={0,1},
xticklabels={$0$,$1$},
xlabel=$\beta$
]
\addplot[samples at={1,...,100},only marks,mark size=0.5,blue] (x/((1+x)*0.5),0);
\end{axis}
\end{tikzpicture}
现在,我不希望它使用样本运行,而是以初始值(我可以指定)运行,然后再逐渐增加到其他数字,直到 x 达到 1。
此外,我想识别这些节点,以便在这些数字之间画弯曲的箭头来表示跳跃。
我尝试了多种方法,但无法让 TikZ 根据具有初始值的公式绘制此类序列的特定数字。当然,我可以轻松地分别解决所有问题,然后绘制它们,但我不想使用这种丑陋的蛮力方法。
任何帮助都将不胜感激。提前致谢 :)
答案1
我认为这类似于您想要的。初始值以点为单位设置,使用语句\setlength
。我在 while 循环中使用了 0.99pt,因为该值永远不会达到 1。
代码有很多注释,如果有任何不清楚的地方可以询问,或者如果我误解了就告诉我。
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen} % for whiledo
% set initial value
\newlength\MyX
\setlength\MyX{0.1pt}
% counter for giving a different name to each node
\newcounter{XCnt}
\setcounter{XCnt}{1}
% for convenience
\newcommand\XScale{200}
\newcommand\XMax{1}
\begin{document}
Diagram using the values set up in preamble:
\begin{center}
\begin{tikzpicture}
\draw[gray, very thin,|-stealth] (0,0) node[below] {$0$} -- (\XMax*\XScale pt,0) node[below] {$\XMax$};
% basic while loop
\whiledo{\lengthtest{\MyX<0.99pt}}{%
%draw filled node at x-position given by the \MyX length
\node [inner sep=0pt,minimum size=2pt,fill,circle] (n-\theXCnt) at (\MyX*\XScale,0) {};
% calculate new length
\pgfmathsetlengthmacro{\MyX}{\MyX/((1+\MyX)*0.5)}
% add 1 to the naming counter
\stepcounter{XCnt}
% to see the values of \MyX in the log, not necessary
\typeout{\MyX}
}
% one example to show that the nodes can be referenced
\draw [red,latex-latex] (n-1) to[bend left] (n-4);
\end{tikzpicture}
\end{center}
Then a second diagram, with slightly different equation, different initial value and scale:
\begin{center}
% set different initial value
\setlength\MyX{0.02pt}
% reset node naming counter
\setcounter{XCnt}{1}
% change scale, if necessary
\renewcommand\XScale{50}
\renewcommand\XMax{4}
\begin{tikzpicture}
\draw[gray, very thin,-stealth] (0,0) -- (\XMax*\XScale pt+3mm,0); % +3mm to extend the line a bit
% draw tick marks:
\foreach \x in {0,...,\XMax}
\draw [gray,very thin] (\x*\XScale pt,3pt) -- +(0,-6pt) node[below]{$\x$};
\whiledo{\lengthtest{\MyX<3.999pt}}{% note changed limit
\node [inner sep=0pt,minimum size=2pt,fill,circle] (n-\theXCnt) at (\MyX*\XScale,0) {};
\pgfmathsetlengthmacro{\MyX}{\MyX/((1+\MyX)*0.2)}
\stepcounter{XCnt}
\typeout{\MyX}
}
% one example to show that the nodes can be referenced
\draw [red,latex-latex] (n-1) to[bend left] (n-4);
\end{tikzpicture}
\end{center}
\end{document}