pgfmathsetmacro 在學生 T 計算中失敗?

pgfmathsetmacro 在學生 T 計算中失敗?

我正在為正態分佈和學生 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

預設情況下,in\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}

在此輸入影像描述

相關內容