4

I'm trying to plot a scatter plot with curve fitting trendline of polynomial degrre 3. I have been able to create the required graph with trendline in excel. I tried to plot the same graph with the latex/tiktz but i'm failing to get the trendline in the latex graph. The expected graph is as below enter image description here

Although my scatter plot image from my latex code is as below

enter image description here

The latex code for image is as below

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[    enlargelimits=false, 
]
\addplot+[
color=black,
only marks,
scatter,
mark=halfcircle*,
mark size=2.9pt
]
table[meta=SPL(dB)]{data2.dat};
\addplot [ smooth,thick, red]
gnuplot [raw gnuplot] { % "raw gnuplot" allows us to use arbitrary gnuplot commands
            f(x) =  a*x^3+b*x^2+c*x+d;  % Define the function to fit
            a=0; b=0,c=0,d=0;          % Set reasonable starting values here
            % Select the x-range, the file, the columns (indexing starts at 1) and the variables for fitting
            fit [0:8000] f(x) 'data2.dat'% u 1:2 via a,b,c,d; 
            plot [x=0:8000] f(x); % Specify the range to plot
    };
%table[ y={create col/linear regression={y=SPL(dB)}}]{data2.dat};
\end{axis}
\end{tikzpicture}
\end{document}

I would like that latexshould draw the curve by auto fitting maximum points with smooth curve, I'm not sure what sort of code needs to be used here.

The data file used in the graphs has data below:- contents of data2.dat are as below

Frequency(Hz) SPL(dB)
50  63.8
100 65.5
200 71.7
300 74.5
400 78.1
500 83.8
600 82.8
700 85.9
800 88
900 87.8
1000 87.1
1500    84.8
2000    93.8
2500    95.1
3000    93.3
3500    89.1
4000    89
4500    86
5000    83.3
5500    84.7
6000    81.2
6500    85.4
7000    80.3
7500    83.3
8000    81.7
Torbjørn T.
  • 206,688
4022ax
  • 43
  • Hi, welcome. If you don't get any answers, you could do the fitting in Excel (or other), and plot the obtained equation with pgfplots, e.g. \addplot {4e-10*x^3 - 6e-6*x^2 + 0.022*x + 68.74}; – Torbjørn T. Apr 30 '18 at 09:59

1 Answers1

6

There were a few mistakes in your Gnuplot code. I'm no expert, but on comparing with Adding values to pgfplot legend and testing a bit:

  • You need a semi-colon between each of the variable declarations, not a comma, so a=1; b=2;, not a=1, b=2.
  • You also need to set non-zero values for a, b, c, d. When set to zero, you get this warning in the .log file:

    Warning: Initial value of parameter 'a' is zero.
    Warning: Initial value of parameter 'b' is zero.
    Warning: Initial value of parameter 'c' is zero.
    Warning: Initial value of parameter 'd' is zero.
      Please provide non-zero initial values for the parameters, at least of
      the right order of magnitude. If the expected value is zero, then use
      the magnitude of the expected error. If all else fails, try 1.0
    
  • You need fit f(x) 'data2.dat' using 1:2 via a,b,c,d;, i.e. remove the domain setting, and specify columns and fitting parameters.

Of course, as you're using Gnuplot, you need to compile with shell-escape enabled.

enter image description here

\documentclass{article}
\usepackage{pgfplots} % loads tikz which loads graphicx
\begin{document}
\begin{tikzpicture}
\begin{axis}[    enlargelimits=false, 
]
\addplot+[
  color=black,
  only marks,
  scatter,
  mark=halfcircle*,
  mark size=2.9pt
]
table[meta=SPL(dB)]{data2.dat};

\addplot [thick, red] 
gnuplot [raw gnuplot] { % "raw gnuplot" allows us to use arbitrary gnuplot commands
 f(x)=a*x^3+b*x^2+c*x+d;  % Define the function to fit
 % set initial parameter values
 % make them non-zero
 % semi-colon after every one
 a=0.0001;
 b=0.0001;
 c=0.1;
 d=80;
 % Select the x-range, the file, the columns (indexing starts at 1) and the variables for fitting
 % specify columns and fitting variables
 % don't specify range
 fit f(x) 'data2.dat' using 1:2 via a,b,c,d; 
 plot [x=0:8000] f(x); % Specify the range to plot
};

\end{axis}
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688