我在数据集上拟合了一条曲线,但参数 c 在图的拟合方程图例中始终为 0。你能帮我解释一下这是为什么吗?
我还检查了生成的parameter.dat文件,它包含c的值(第二个):1.59029739803992 3.3388191096414e-09 0.000482995469000494
但由于某种原因,它并没有达到传说中的那样……
代码:
\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
};
\addlegendentry[/pgf/number format/precision=3]{
\pgfplotstableread{parameters.dat}\parameters
% 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}
答案1
问题似乎是语法c
写的3.3e-9
,这似乎混淆了 pgf/TikZ。如果您在文件中重写它,parameters.dat
它0.0000000033
就会起作用。要使第一个数字起作用,请激活fpu
库。
有关详细信息,请查看代码中的注释。
% 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'
\addplot coordinates {(0,0)};
\addlegendentry[
/pgf/number format/precision=3,
]{%
% -----------------------------------------------------------------
% activate the `fpu' library and it will work
\pgfkeys{/pgf/fpu=true}%
% -----------------------------------------------------------------
\pgfplotstableread{parameters.dat}\parameters%
% 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}