1

Currently I am trying to convert a plot I made with python and matplotlib to a pgfplot, but the plot is constant zero. I checked the formula, it appears to be right, so I dont have a clue why the plots differ. Here is the tex and the python code:

\begin{tikzpicture}
\pgfmathsetmacro{\r}{4}
\pgfmathsetmacro{\l}{(20e-6)}
\pgfmathsetmacro{\c}{((1/20)*1e-6)}

\begin{axis}[ xmin=1, xmax=2e6, samples=5000, xlabel=$\omega$]

\addplot[domain=2:2e6, black, thick] {1/(1 + ((x*\l)/\r - 1/(x*\r*\c))^2)};

\end{axis} \end{tikzpicture}

def frac(a, b): return a / b

def h(w, r, l, c): return frac(1, 1 + (frac(wl, r) - frac(1, wrc))*2)

I am glad for any help!

lyding
  • 13
  • Please, next time post a compilable example like the one in my answer. And by the way, welcome! – Rmano May 28 '21 at 17:42

1 Answers1

0

Your problem is that \pgfmathsetmacro{} is rounding the number and the range of numbers of pgfmath is quite limited (see note 2). I have added a debug node to show you that your \l (see note 1) is 0.0...

I suggest using the declare function facility for this kind of thing (besides, the syntax is much better). There will be still limitations in the range of numbers, and the dimension too large error is very easy to have, but it's much more stable.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning,calc}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro{\l}{(20e-6)}
    \tikzset{declare function={
            L=20e-6;
            R=4;
            C=(1/20)*1e-6;
        }
    }
\node[red] at (3,3) {l is \l}; % just to debug

\begin{axis}[
    xmin=1,
    xmax=2e6,
    samples=150,
    xlabel=$\omega$]

    \addplot[domain=2:2e6, black, thick] {1/(1 + (x*L/R - 1/(x*R*C))^2)};
\end{axis}

\end{tikzpicture} \end{document}

enter image description here


note 1: try not to use one-letter macros. It is very dangerous because a lot of them are defined for accents in the core LaTeX, and things can explode in unfathomable ways.

note 2: The pgfplots manual says:

enter image description here enter image description here

but be warned that using the fpu manually can be tricky.

There are other mathematical engines around, like xfp for example, which are more "standard", but not so well integrated into the rest of pgf. For very complex calculations, is better to use external tools and then import the data points.

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 1
    Thank you very much for your quick response. There is no way I would have figured that out by myselfe! – lyding Jun 01 '21 at 13:30