1

I am trying to graph two functions on the same graph: y=1 and g(x) = (e^(1/x))^3 - 1.

But when I graph my function g with a sample size greater than 10, my graph completely screws up. I've confirmed that this is the correct equation and should graph correctly but for some reason it is not. Here is my code below

    \begin{tikzpicture}
    \begin{axis}[
       axis lines = left,
       xlabel = $x$,
       ylabel = {$f(x)$},
       ]
   \addplot [
       domain=0:10, 
       samples=100, 
       color=red,
       ]
   {exp(3/x) - 1};
   \addlegendentry{$(e^{(1/\alpha)})^3-1$}
   \addplot [
       domain=0:10, 
       samples=100, 
       color=black,
       ]
       {1};
   \end{axis}
   \end{tikzpicture}
5

The reason that your plot is not displaying properly is that PGFplots calculates the maximum (non-infinite) value and uses that in order to set the bounds on the y axis. As a result, when you have a step size smaller than ~10, you start probing points very close to 0 which diverge to infinity really fast.

You can set the bounds on the axes with ymax and ymin (and similarly for the x axis); however, this will cause an error as it will encounter overflows since PGFplots does some clever tricks to work with TeX's limited numerical capabilities.

The best option then in your case is the restrict y to domain key. This options tells PGFplots to discard value which exceed the bounds and thus avoids entirely overflows.

\documentclass[tikz,convert]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      axis lines=left,
      xlabel=$x$,
      ylabel={$f(x)$},
      restrict y to domain=0:100,
    ]
    \addplot [
        domain=0:10, 
        samples=100, 
        color=red,
      ]
      {exp(3/x) - 1};
    \addlegendentry{$(e^{(1/\alpha)})^3-1$}
    \addplot [
        domain=0:10, 
        samples=100, 
        color=black,
      ]
      {1};
  \end{axis}
\end{tikzpicture}
\end{document}

output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.