2

I'm trying to plot the decharging curve of a capacitor. The code is as follows:

  \documentclass{article}      
  \usepackage{pgfplots}      
  \begin{document}

    \begin{tikzpicture}
    \begin{axis}[domain = -360:360, samples=40,
    width=.6\linewidth, height=7cm, xmin=0, xmax=10, ymin=0]

    \addplot[color=blue] {100 * 2.7^(-x/0.0001)};

    \end{axis}      
    \end{tikzpicture}

\end{document}

In fact, there are two issues. First up, there is an internal error which is probably caused by the division by 0.0001. The compilation seems to take forever.

But even if you omit the division and only raise to the power of -x, there is a problem. It says:

Package PGF Math Error: You've asked me to divide 97.26378000000000 by 0.0, but I cannot divide any number by 0.0.

A second error below simply says Arithmetic overflow.

Does anyone know what are the issues here and how to solve them? Any workarounds?

  • I think it might be because x=0 is one sample at which the function is evaluated. – nidhin Dec 8 '18 at 22:13
  • 1
    I would guess that if you omit the division, 2.7^360 causes an Arithmetic overflow - the number is just too big to deal with. I would recommend figuring out what ymax you really want. – Teepeemm Dec 8 '18 at 22:14
  • @nidhin While that's true, I don't understand how that should be an issue. Raising any number to the power of 0 should evaluate to 1. Though apart from that, the problem persists anyway if I set xmin=1 – R. Hahnemann Dec 8 '18 at 22:14
  • 1
    @R.Hahnemann My bad. It should not matter. But hanging samples=41 works. – nidhin Dec 8 '18 at 22:19
2

Since your xmin=0 and xmax=10, you can change the domain also that range. Otherwise 2.7^(360/0.0001) will cause overflow. Reducing the domain will work.

Since the capacitor is discharging exponentially with a time constant 0.0001, The samples should also be spaced in the same range. Hence number of samples required to get a good plot in the range 0:10 will be high. It is better to restrict the domain to a smaller value. I used 0:0.01.

  \documentclass{article}
  \usepackage{pgfplots}
  \begin{document}
    \begin{tikzpicture}
    \begin{axis}[domain = 0:0.01, samples=40,
%    width=.6\linewidth, height=7cm, xmin=0, xmax=10,
    ]
    \addplot[color=blue] {100 * 2.7^(-x/0.0001)};
    \end{axis}
    \end{tikzpicture}

\end{document}

enter image description here

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.