# Curve fitted parameter appears as 0 in legend (pgfplots, gnuplot, pgfplotstable)

I fitted a curve on a dataset, but parameter c remains always 0 in the legend of the plot's fitting equation. Can you please help me why? I also checked the generated parameter.dat file and it contains the value of c (the second): 1.59029739803992 3.3388191096414e-09 0.000482995469000494

But it does not go to the legend for some reason...

The code:

    \begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xlabel={$f[Hz]$},
ylabel={\textcolor{blue}{$Z[Ohm]$}},ytick pos=left]
\addplot [mark=x, thin, blue, only marks, mark size=1pt, forget plot] table [x=Frequency, y=Z, col sep=comma] {Data/LC.csv};
\addplot [blue, thin, mark = none] gnuplot [raw gnuplot] {
r=2;
c=3*10**(-9);
l=490*10**-6;
f(x)=sqrt(r**2+(1/(2*pi*x*c)-2*pi*x*l)**2);

set xrange [100000:150000];
set datafile separator ',';
fit f(x) 'Data/LC.csv' u 1:2 via r,c,l;
plot f(x);
set print "parameters.dat";  % Open a file to save the parameters
print r,c,l;                  % Write the parameters to file
};

% Open the file Gnuplot wrote

\pgfplotstablegetelem{0}{0}\of\parameters \pgfmathsetmacro\paramr{\pgfplotsretval} % Get first element, save into \paramA
\pgfplotstablegetelem{0}{1}\of\parameters \pgfmathsetmacro\paramc{\pgfplotsretval}
\pgfplotstablegetelem{0}{2}\of\parameters \pgfmathsetmacro\paraml{\pgfplotsretval}
$Z=\sqrt{\pgfmathprintnumber{\paramr}^2 + (1/(2*\pi*\pgfmathprintnumber{\paramc}*f)-2*\pi*\pgfmathprintnumber{\paraml}*f)^2}$
}

\end{axis}
\end{tikzpicture}
\end{center}
\end{figure}

• Welcome to TeX.SX! Please help us (and also you) and edit your code to get a full minimal working example (MWE), that illustrates your problem, starting with \documentclass and ending with \end{document}. – Bobyandbob Sep 19 '18 at 19:52
• I guess there will be an easy solution using declare function. However, as your example does not compile (mainly because the you do not provide us with the file Data/LC.csv), I refrain from spelling this out. You also seem to have received a reasonable workaround. – user121799 Sep 19 '18 at 23:46

The problem seems to be that c is written in 3.3e-9 syntax which seems to confuse pgf/TikZ. If you rewrite it in the parameters.dat file as 0.0000000033 it will work. And to make the first number work, activate the fpu libary.

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$f / \text{Hz}$},
ylabel={\textcolor{blue}{$Z / \text{Ohm}$}},
ytick pos=left,
]
%% you didn't provide the data file ...
%        \addplot [mark=x, thin, blue, only marks, mark size=1pt, forget plot] table [x=Frequency, y=Z, col sep=comma] {Data/LC.csv};
%        \addplot [blue, thin, mark = none] gnuplot [raw gnuplot] {
%            r=2;
%            c=3*10**(-9);
%            l=490*10**-6;
%            f(x)=sqrt(r**2+(1/(2*pi*x*c)-2*pi*x*l)**2);
%
%            set xrange [100000:150000];
%            set datafile separator ',';
%            fit f(x) 'Data/LC.csv' u 1:2 via r,c,l;
%            plot f(x);
%            set print "parameters.dat";  % Open a file to save the parameters
%            print r,c,l;                  % Write the parameters to file
%        };

% thus, I just use a dummy \addplot'
/pgf/number format/precision=3,
]{%
% -----------------------------------------------------------------
% activate the fpu' library and it will work
\pgfkeys{/pgf/fpu=true}%
% -----------------------------------------------------------------
% Open the file Gnuplot wrote
\pgfplotstablegetelem{0}{0}\of\parameters%
\pgfmathsetmacro\paramr{\pgfplotsretval}%
\pgfplotstablegetelem{0}{1}\of\parameters%
\pgfmathsetmacro\paramc{\pgfplotsretval}%
\pgfplotstablegetelem{0}{2}\of\parameters%
\pgfmathsetmacro\paraml{\pgfplotsretval}%
% and deactivate it again
\pgfkeys{/pgf/fpu=false}%
%
$Z = \sqrt{\pgfmathprintnumber{\paramr}^2 + (1/(2 \, \pi \cdot \pgfmathprintnumber{\paramc} \cdot f) - 2 \, \pi \cdot \pgfmathprintnumber{\paraml} \cdot f)^2}$
}

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