3

I am trying to plot 't' distribution with area of interest shaded. I have done both plotting and sharing area. Next tried to draw vertical lines closing the shaded area on both sides. For this, I need to calculate the point on the curve for upper end point of the line to be drawn.

For this, I tried to use \pgfmathsetmacro which is throwing dimensions too large error. I use texstudio and tried \tracingmacros2 but unable to find the problem. I wonder why this error, because the code is able to draw the related curve using same function successfully, so why too large number returned for specific point (which is in the range of the x axis of curve drawn). Kindly help.

MWE:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}



\begin{document}
%\tracingmacros2
%\tracingassigns2


\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{student}{2}{%
    \pgfmathparse{gamma((#2+1)/2.)/(sqrt(#2*pi) *gamma(#2/2.)) *((1+(#1*#1)/#2)^(-(#2+1)/2.))}%
}


\begin{tikzpicture}

\begin{axis}[
    axis lines=left,
    enlargelimits=upper,
    samples=100
]

    \def\df{3}
    \def\cilow{-3.182}
    \def\cihigh{3.182}
    \addplot [draw=none, fill=yellow!25, domain=\cilow:\cihigh] {student(x, \df)} \closedcycle;
    \addplot [very thick,cyan!50!black] {student(x, \df)};

    %\pgfmathsetmacro\valueA{student(1, 3)}
    %\draw [gray] (axis cs:\cilow,0) -- (axis cs:\cilow,\valueA);   

\end{axis}
\end{tikzpicture}
\end{document}

Output (with problematic line commented):
enter image description here

Online editor for easy reproduction and analysis: Here

1 Answer 1

1

Not really sure what happens, but if you use the student function in the coordinate directly, it works fine. I.e.

\draw [gray] (axis cs:\cilow,0) -- (axis cs:\cilow,{student(\cilow, \df)});

Alternatively use a ycomb plot to make those two vertical lines:

\addplot [ycomb, gray, no markers, samples at={\cilow, \cihigh}] {student(x, \df)};

Complete code:

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}


\begin{document}
%\tracingmacros2
%\tracingassigns2


\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{student}{2}{%
    \pgfmathparse{gamma((#2+1)/2.)/(sqrt(#2*pi) *gamma(#2/2.)) *((1+(#1*#1)/#2)^(-(#2+1)/2.))}%
}


\begin{tikzpicture}

\begin{axis}[
    axis lines=left,
    enlargelimits=upper,
    samples=100
]

    \def\df{3}
    \def\cilow{-3.182}
    \def\cihigh{3.182}
    \addplot [draw=none, fill=yellow!25, domain=\cilow:\cihigh] {student(x, \df)} \closedcycle;
    \addplot [very thick,cyan!50!black] {student(x, \df)};


    %\draw [gray] (axis cs:\cilow,0) -- (axis cs:\cilow,{student(\cilow, \df)});
    \addplot [ycomb, gray, no markers, samples at={\cilow, \cihigh}] {student(x, \df)};

\end{axis}
\end{tikzpicture}
\end{document}
2
  • great workaround, thank you very much. this works. should i close this question because we still not sure why pgfmathsetmacro is throwing error. Sep 29, 2018 at 10:00
  • I guess that the error appears since you are using an ordinary \draw command, which does, unlike \addplot, not use fpu. (Of course, one might make it use that.)
    – user121799
    Sep 29, 2018 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .