[Tex/LaTex] Fitting on log log axis

gnuplotpgfplotstikz-pgf

I try to fit data which I plotted on a double logarithmic scale.

I use LaTeX and gnuplot. [It works with pdfLaTeX with adding the additional argument "–enable-write18" and directing the path of "gnuplot.exe" to the PATH variable.]

My code is:

\documentclass{standalone}



\usepackage[ngerman]{babel}

\usepackage{pgfplots}



\begin{document}

\centering
\begin{tikzpicture}
    \begin{loglogaxis}[width=14cm, height=14cm]
        \addplot [color=blue, thick, mark=*, only marks
        ] table [x index=0, y index=1] {err_double2.txt};
        \addlegendentry{trapezoid}
        \addplot [color=red, thick, mark=*, only marks
        ] table [x index=0, y index=2] {err_double2.txt};
        \addlegendentry{simpson}
        \addplot [color=black, thick, mark=*, only marks
        ] table [x index=0, y index=3] {err_double2.txt};
        \addlegendentry{gauss}
        \addplot gnuplot [raw gnuplot, id=ball, mark=none, color=green]{
            set xrange [2:36];
            t(x)=a*x**b;
            s(x)=c*x**d;
            fit t(x) 'err_double2.txt' u 1:2 via a,b;
            fit s(x) 'err_double2.txt' u 1:3 via c,d;
            plot t(x), s(x);
            };
        \addplot gnuplot [raw gnuplot, id=ball2, mark=none, color=green]{
            set xrange [2:6];
            g(x)=h*x**f;
            fit g(x) 'err_double2.txt' u 1:4 via h,f;
            plot g(x);
            };
    \end{loglogaxis}
\end{tikzpicture}


\end{document}

Here is the resulting pdf-file:

enter image description here

The fits are the green lines.
For "trapezoid" and "simpson" it works fine. But for "gauss" I want to fit just the first 5 data points in range [2:6]; this somehow does not do the job. The green line is far away from the points.

You can find the txt-file ("err_double2.txt") here: http://www.file-upload.net/download-8235966/err_double2.txt.html

Does anyone have an idea how to fix this?
I am looking forward for your answers!

Best Answer

This has nothing to do with pgfplots, it is the way you fit your lines.

gnuplot as any other fitting program will guess the starting conditions. And those starting conditions can prove to be very wrong.
I would advice you to read up on statistics and fitting. It is not trivial.
The biggest problem is that you would like to fit to only three values (doing residual and mean values of 3 points is, at best, very wrong). The residuals of the fit will largely be dominated by the first point (the highest y-value), hence the fit will converge fast and will only go through the first point.

You can force gnuplot to start the guess at another point (but it will probably still be wrong).

Do

h=120
f=-20

and it will fit better, but still not quite. Please try and do it outside of pgfplots and see the messages gnuplot tells you, they will show that the fit is bad.

You can also toy with the variable:

FIT_LIMIT=<number>

to increase precision (but in this case with 3 numbers it won't do much).

Related Question