7

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!

  • You should do the quadrature for all points in between to get a better sample of the Gaussian quadrature method. – zeroth Oct 30 '13 at 8:02
  • I tried it as well, with 5 points in the region [2:6]. Still there was nearly the same fitting curve. – Andy Oct 30 '13 at 15:21
6

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).

  • Thank you for your answer! I tried it with 5 points in the region [2:6], it still was not sufficient. Then I fitted it in zunzun.com with the fitting target "Lowest sum of squared absolute error" and got the same problem. Now I used "Lowest sum of squared relative error" and got approximately the same values that you recommended. Fazit: I have to fit with another method than the one default in gnuplot. Is there a fit option to select another fitting algorithm? – Andy Oct 30 '13 at 15:13
  • @Andy The problem is most likely not the fitting algorithm, it is the very few points. So I think you are at the limits of fitting, even with 5 points. – zeroth Oct 30 '13 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.