Search code examples
gnuplotdata-fitting

Fit log-log data with gnuplot


i try to fit this plot enter image description here as you cans see the fit is not so good for the data.

My code is:

    clear
reset


set terminal pngcairo size 1000,600 enhanced font 'Verdana,10'
set output 'LocalEnergyStepZoom.png'
set ylabel '{/Symbol D}H/H_0'
set xlabel 'n_{step}'
set format y '%.2e'

set xrange [*:*]
set yrange [1e-16:*]

f(x) = a*x**b
fit f(x) "revErrEnergyGfortCaotic.txt" via a,b


set logscale

plot 'revErrEnergyGfortCaotic.txt' w p,\
 'revErrEnergyGfortRegular.txt' w p,\
f(x) w l lc rgb "black" lw 3 

exit

So the question is how mistake i compute here? because i suppose that in a log-log plane a fit of the form i put in the code should rappresent very well the data.

Thanks a lot

Finally i can be able to solve the problem using the suggestion in the answer of Christop and modify it just a bit.

I found the approximate slop of the function (something near to -4) then taking this parameter fix i just fit the curve with only a, found it i fix it and modify only b. After that using the output as starting solution for the fit i found the best fit. enter image description here


Solution

  • You must find appropriate starting values to get a correct fit, because that kind of fitting doesn't have one global solution. If you don't define a and b, both are set to 1 which might be too far away. Try using

    a = 100
    b = -3
    

    for a better start. Maybe you need to tweak those value a bit more, I couldn't because I don't have the data file.

    Also, you might want to restrict the region of the fitting to the part above 10:

    fit [10:] f(x) "revErrEnergyGfortCaotic.txt" via a,b
    

    Of course only, if it is appropriate.