我正在绘制各种正态和学生 T 分布的图片。下面显示的是显示两种分布的简单图片的 MWE。我直接使用学生 T 计算绘制一条垂直线,一切正常。我将相同的计算放入 pgfmathsetmacro 中,它失败了,提示“!维度太大。”
在我的实际应用中,rgtCH 的值在多个地方使用。计算需要 TeX 一段时间,所以我试图让它进行一次计算并将结果放入变量中以供重复使用,而不是每次都重新计算。在我的实际应用中,这一切都是高度参数化的。为了使 MWE 尽可能简单,我删除了尽可能多的参数化。我承认我是 pgf 的新手,所以如果有人能帮助我理解我做错了什么,我将不胜感激。
\documentclass[11pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{DistAxis/.style={%
no markers,
domain=-4:4, % Only display z values between -4 and 4.
samples=100,
xlabel=\textbf{t},
every axis x label/.style={at={(axis description cs:1.0, 0.0)}, anchor=west},
height=5cm, width=12cm,
xtick=\empty, ytick=\empty,
enlargelimits=false,
clip=false,
axis on top=true,
hide y axis,
axis x line*=middle,
axis line style ={thick,latex-latex}}
}
\pgfmathdeclarefunction{std_norm}{1}{%
\pgfmathparse{1/(sqrt(2*pi))*exp(-((#1)^2)/(2))}%
}
\pgfmathdeclarefunction{gamma}{1}{%
\pgfmathparse{2.506628274631*sqrt(1/#1) + 0.20888568*(1/#1)^(1.5) + %
0.00870357*(1/#1)^(2.5) - (174.2106599*(1/#1)^(3.5))/25920 - %
(715.6423511*(1/#1)^(4.5))/1244160)*exp((-ln(1/#1)-1)*#1}%
}
\pgfmathdeclarefunction{std_stud}{2}{%
\pgfmathparse{gamma(.5*(#1+1))/(sqrt(#1*pi)*gamma(.5*#1))*((1+(#2*#2)/#1)^(-.5*(#1+1)))}%
}
\begin{document}
\begin{tikzpicture}[scale=0.6]
\begin{axis}[DistAxis]
% If you comment the first and uncomment the second, this fails to compile. Why?
\pgfmathsetmacro{\rgtCH}{ 0.075+std_norm(1.0))};
% \pgfmathsetmacro{\rgtCH}{ 0.075+std_stud(9, 1.0))};
\draw [thick,magenta,dashed] (axis cs:1.0, -0.02) -- (axis cs:1.0, {0.075+std_stud(9,1.0)});
\draw [thick,magenta,dashed] (axis cs:1.0, -0.02) -- (axis cs:1.0, {\rgtCH});
\addplot [very thick,blue] {std_norm(x)};
\addplot [very thick,red] {std_stud(9,x)};
\end{axis}
\end{tikzpicture}
\end{document}
该图显示正态分布和学生 T 分布计算均在进行中。
答案1
默认情况下,它\pgfmathsetmacro
fpu
是关闭的。如果您将其打开,dimension too large
错误就会消失。
\documentclass[11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{DistAxis/.style={%
no markers,
domain=-4:4, % Only display z values between -4 and 4.
samples=100,
xlabel=\textbf{t},
every axis x label/.style={at={(axis description cs:1.0, 0.0)}, anchor=west},
height=5cm, width=12cm,
xtick=\empty, ytick=\empty,
enlargelimits=false,
clip=false,
axis on top=true,
hide y axis,
axis x line*=middle,
axis line style ={thick,latex-latex}}
}
\newcommand{\pgfmathparseFPU}[1]{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathparse{#1}%
\pgfmathsmuggle\pgfmathresult\endgroup}
\pgfmathdeclarefunction{std_norm}{1}{%
\pgfmathparseFPU{1/(sqrt(2*pi))*exp(-((#1)^2)/(2))}%
}
\pgfmathdeclarefunction{gamma}{1}{%
\pgfmathparseFPU{2.506628274631*sqrt(1/#1) + 0.20888568*(1/#1)^(1.5) + %
0.00870357*(1/#1)^(2.5) - (174.2106599*(1/#1)^(3.5))/25920 - %
(715.6423511*(1/#1)^(4.5))/1244160)*exp((-ln(1/#1)-1)*#1}%
}
\pgfmathdeclarefunction{std_stud}{2}{%
\pgfmathparseFPU{gamma(.5*(#1+1))/(sqrt(#1*pi)*gamma(.5*#1))*((1+(#2*#2)/#1)^(-.5*(#1+1)))}%
}
\begin{document}
\begin{tikzpicture}[scale=0.6]
\begin{axis}[DistAxis]
% If you comment the first and uncomment the second, this fails to compile. Why?
\pgfmathsetmacro{\rgtCH}{ 0.075+std_norm(1.0))};
\pgfmathsetmacro{\rgtCH}{ 0.075+std_stud(9, 1.0))};
\draw [thick,magenta,dashed] (axis cs:1.0, -0.02) -- (axis cs:1.0, {0.075+std_stud(9,1.0)});
\draw [thick,magenta,dashed] (axis cs:1.0, -0.02) -- (axis cs:1.0, {\rgtCH});
\addplot [very thick,blue] {std_norm(x)};
\addplot [very thick,red] {std_stud(9,x)};
\end{axis}
\end{tikzpicture}
\end{document}