Search code examples
latextikzpgf

Evaluate a function and assign to a constant to use as tick markin Tikz/pgfplots


I'm defining some functions to plot using pgfplots and that has been successful. Now I would like to add a y tick at a point that's evaluated using these functions (lbound below). I can't seem to be able to use lbound as a constant however. This is to mark the point where the horizontal plot hits the y-axis. What should I do instead?

\begin{tikzpicture}[
    declare function={
        tmin = 0;
        tmax = 1;
        C(\x) = (\x)^2;
        Cinv(\x) = (\x)^(1/2);
        ell(\theta,\tau,\K) = 1 - Cinv( (- (\tau * \theta) / (tmax - tmin) + \K) );
        lbound = ell(1, 0.8, C(1) + 0.8 * tmin / (tmax - tmin));
  }
    ]

    \begin{axis}[
            ytick={0,1},
            yticklabels={$0$,$1$}
            ]
        \addplot[dashed, thick, domain=0:1]{ell(1, 0.8, C(1) + 0.8 * tmin / (tmax - tmin))};
    \end{axis}
\end{tikzpicture}

Solution

  • I may misunderstand the question but one way to read it is that you want this:

    \documentclass[tikz,border=3.14mm]{standalone}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.17}
    \begin{document}
    \begin{tikzpicture}[
        declare function={
            tmin = 0;
            tmax = 1;
            C(\x) = (\x)^2;
            Cinv(\x) = (\x)^(1/2);
            ell(\theta,\tau,\K) = 1 - Cinv( (- (\tau * \theta) / (tmax - tmin) + \K) );
            lbound = ell(1, 0.8, C(1) + 0.8 * tmin / (tmax - tmin));
      }
        ]
    
        \begin{axis}[
                ytick={0,lbound},
                yticklabels={$0$,$\ell_\mathrm{bound}$}
                ]
            \addplot[dashed, thick, domain=0:1]{ell(1, 0.8, C(1) + 0.8 * tmin / (tmax - tmin))};
        \end{axis}
    \end{tikzpicture}
    \end{document}
    

    enter image description here