我需要绘制一个具有 2 个常数 (\varepsilon 和 \k) 和 1 个变量 (\m) 的函数。我使用\declarefunction
。
我的问题是 y 轴太小,我不知道如何在代码中设置它们以获得我期望的输出。函数如下:
\[
f(m,\varepsilon,k)=\frac{\left( 1-\varepsilon \right) }{2}\left( 1-\left(
1-\varepsilon \right) ^{m}-m\left( 1-\varepsilon \right) ^{m-1}\varepsilon
\right) +\frac{\varepsilon }{2}\left( 2-(1-k\varepsilon )^{m}\right) -\frac{m%
}{2}\varepsilon (1-k\varepsilon )^{m-1}k\varepsilon \left( 1+\frac{\left(
1-\varepsilon \right) \left( 1-\varepsilon \right) ^{m-1}\varepsilon }{%
\left( 1-\varepsilon \right) \varepsilon +\varepsilon (1-k\varepsilon )^{m}}%
\right)
\]
然后,我的代码是:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{every non boxed x axis/.append style={x axis line style=-},
every non boxed y axis/.append style={y axis line style=-}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[xlabel={b},
ylabel={r},
axis lines=left,
y tick label style={/pgf/number format/.cd,sci,sci e},
declare function={f(\m,\e,\k)=
((1-\e)/(2))*
(1-pow((1-\e),\m)-\m*\e*(pow((1-\e),(\m-1))))+
(\e/2)*(2-pow((1-\k*\e),\m))-
(\m/2)*\e*\k*\e*pow((1-\k*\e),(\m-1))*
(1+(((1+\e)*pow((1-\e),(\m-1))*\e)/((1-\e)*\e+\e*(pow(1-\k*\e),\m))))
;},
domain=1:8,no marks,samples=20
,smooth, ymin=0.00044, ymax=0.000445, xmin=2, xmax=5,
extra x ticks={2.25, 2.75, 3.25, 3.75, 4.25, 4.75},
extra x tick labels={ },
% extra y ticks={1.425, 1.435, 1.445, 1.455},
% extra y tick labels={ },
tick label style={font=\scriptsize}]
\addplot[color=black, domain=2:5, variable = \m]{f(m, 0.001, 100)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
预期输出如下。
有人能帮我设置 y 轴值来获得该图表吗?我尝试了一些值,但没有成功。
答案1
为了娱乐和锻炼:不错的小题大做 @Ñako回答(+1):
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\pgfkeys{/pgf/number format/.cd,
sci,
sci generic={mantissa sep={\,},exponent={\textsc{e}^{#1}}},
precision=1
}
\begin{tikzpicture}[
declare function={f(\m,\e,\k)=
((1-\e)/2)*(1-pow((1-\e),\m)-\m*\e*(pow((1-\e),(\m-1))))+
(\e/2)*(2-pow((1-\k*\e),\m))-
(\m/2)*\e*\k*\e*pow((1-\k*\e),(\m-1))*(1+(((1+\e)*pow((1-\e),(\m-1))*\e)/((1-\e)*\e) +
\e*(pow((1-\k*\e),\m)))));}
\begin{axis}[
axis lines=left,
xlabel={$b$},
ylabel={$r$},
x label style={at={(1,0)}, anchor=north east},
y label style={at={(0,1)}, rotate=-90, anchor=north east},
scaled ticks=false,
minor tick num=1,
ticklabel style={font=\scriptsize},
xticklabel style={/pgf/number format/.cd, fixed, fixed zerofill},
yticklabel style={/pgf/number format/sci},
xmax=5.4,
domain=2:5,
samples=401, no marks
]
\addplot +[thick, variable=\m]{f(m, 0.001, 100)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
编辑:实施gnuplot
gnuplot
绘制与所需输出结果相同的函数,结果与pgfplots
所需输出不同,请参见原始答案。
梅威瑟:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{every non boxed x axis/.append style={x axis line style=-},
every non boxed y axis/.append style={y axis line style=-}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={b},
ylabel={r},
axis lines=left,
scaled ticks=false,
yticklabel style={/pgf/number format/precision=3,
/pgf/number format/.cd,
sci,sci e,
/tikz/.cd,},
xmin=2, xmax=5,
ymin=0.000441, ymax=0.000456,
extra x ticks={2.25, 2.75, 3.25, 3.75, 4.25, 4.75},
extra x tick labels={ },
ytick={0.000441,0.000443,...,0.000456},
]
\addplot[color=red, samples=20, smooth,domain = 2:5, no markers] gnuplot [raw gnuplot]{ %
f(x)=((1-eps)/2)*(1-(1-eps)**x-x*(1-eps)**(x-1)*eps)+eps/2*(2-(1-k*eps)**x)-x/2*eps*k*eps*(1-k*eps)**(x-1)*(1+((1+eps)*(1-eps)**(x-1)*eps)/((1-eps)*eps+eps*(1-k*eps)**x));
eps=0.001;k=100;
plot[x=2:5] f(x);
};
\end{axis}
\end{tikzpicture}
\end{document}
原始答案
我对您的函数的输出与您的预期有一点不同(也许我错过了什么!?):
必须在环境中实现,而不是在定义中declare function
实现。您还忘记了一个括号,请参见最后的。此外,在将目录更改为之后,您必须改回 tikz 目录。tikzpicture
axis
pow((1-\k*\e,\m)
number format
yticklabel style
使用 y 轴限制ymin=0.00044
和ymax=0.00045
梅威瑟:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{every non boxed x axis/.append style={x axis line style=-},
every non boxed y axis/.append style={y axis line style=-}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
declare function={f(\m,\e,\k)=
((1-\e)/(2))*
(1-pow((1-\e),\m)-\m*\e*(pow((1-\e),(\m-1))))+
(\e/2)*(2-pow((1-\k*\e),\m))-
(\m/2)*\e*\k*\e*pow((1-\k*\e),(\m-1))*
(1+(((1+\e)*pow((1-\e),(\m-1))*\e)/((1-\e)*\e+\e*(pow((1-\k*\e),\m)))) % <-- bracket
;},
]
\begin{axis}[
xlabel={b},
ylabel={r},
axis lines=left,
scaled ticks=false,
yticklabel style={/pgf/number format/precision=1,
/pgf/number format/.cd,
sci,sci e,
/tikz/.cd,}, %<-- change back to tikz directory
% domain=1:8,
% no marks,
samples=20,
smooth,
% ymin=0.00044, ymax=0.00045,
xmin=2, xmax=5,
extra x ticks={2.25, 2.75, 3.25, 3.75, 4.25, 4.75},
extra x tick labels={ },
% extra y ticks={1.425, 1.435, 1.445, 1.455},
% extra y tick labels={ },
ticklabel style={font=\scriptsize}]
\addplot[color=red, domain=2:5, variable = \m]{f(m, 0.001, 100)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
编辑:关于功能和pgfplots
为了比较,我绘制了函数图:
((1-eps)/2)*(1-(1-eps)^x-x*(1-eps)^(x-1)*eps)+eps/2*(2-(1-k*eps)^x)-x/2*eps*k*eps*(1-k*eps)^(x-1)*(1+((1+eps)*(1-eps)^(x-1)*eps)/((1-eps)*eps+eps*(1-k*eps)^x))
现在很清楚差异的来源pgfplots
以及它如何计算函数。在我看来,它与幂有关。