
我想在 [0,1] 中繪製一條數軸,以便將節點放置在貝葉斯規則(或任何特定函數)定義的數字處。因此,對於x' 的初始值(由我指定),然後我希望程式將節點放置在每個後續節點x 處,其中x= x'/((1+x')*0.5) 依此類推,直到最大x of 1。
\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}