Search code examples
gnuplotdata-fitting

How to fit a logarithmic function with gnuplot?


I have the following data and want to fit a function with gnuplot. It seems that the function is:

f(x) = 0.000855*(1 + b*log(x/a)) with b=0.45 and a=32.23

fits with the data. I used the following command in gnuplot but I couldn't fit this function with the data.

6.77    0.000165774
8.13    0.00034866
9.48    0.000440373
10.83   0.000473223
16.25   0.000589812
18.28   0.000629904
20.31   0.000661883

I used the following command:

p "./data.txt" u 1:2
f(x) = 0.000855*( 1 + b*log(x/a))
fix f(x) "data.txt" using 1:2 via b,a
p "./data.txt", f(x)

could you please suggest any more functions?

Thanks in advance,


Solution

  • How do you know that your function should fit the data and from where do you get the values a,b?

    In general, you can shift and scale basic functions, e.g. like log(). So, if you think log(x) is a suitable function to describe your data then try to shift and scale it. Shift x by x0, shift y by y0 and scale with some factor a.

    Altogether:

    f(x) = a*log(x-x0) + y0
    

    In the script example below it seems that you don't even need approximate starting values to let the fit converge. However, the mathematical description of your system should give you the function. Without knowing your system, nobody can tell whether f(x) is an appropriate function to fit your data.

    Script:

    ### fitting a log function
    reset session
    
    $Data <<EOD
    6.77    0.000165774
    8.13    0.00034866
    9.48    0.000440373
    10.83   0.000473223
    16.25   0.000589812
    18.28   0.000629904
    20.31   0.000661883
    EOD
    
    f(x) = a*log(x-x0) + y0
    
    set fit nolog
    fit f(x) $Data u 1:2 via a,x0,y0
    set key top left
    
    plot $Data u 1:2 w lp pt 7, \
         f(x) w l lc "red", \
         0.000855*(1 + 0.45*log(x/32.23)) w l dt 3 lc "blue" ti "your guess"
    ### end of script
    

    Result:

    Final set of parameters            Asymptotic Standard Error
    =======================            ==========================
    a               = 0.000153601      +/- 8.592e-06    (5.594%)
    x0              = 6.17491          +/- 0.1102       (1.785%)
    y0              = 0.000245976      +/- 1.982e-05    (8.057%)
    

    enter image description here