Search code examples
plotgnuplotcurve-fitting

Fit in gnuplot using the logarithm of a column


I'm having some troubles fitting a dataset to calculate the Response Time of a Thermometer. So I have my dataset in the form of

Time (s) - Temperature (K)
0.4820 295.0772
0.4840 295.0772
0.4860 295.1651
0.4880 295.1651
0.4900 295.1651
0.4920 295.2531
0.4940 295.2091
0.4960 295.2531
0.4980 295.2972
0.5000 295.3412
0.5020 295.2972
0.5040 295.3853
0.5060 295.3412

and I want to linearize only the second column by doing the operation

y = log($2 - 325.6)

so I've written my .gp file this way

f(x) = a*x+b
fit f(x) 'termom_COST_SCALED.txt' via a, b u 1:(log($2 - 325.6))

p 'termom_COST_SCALED.txt' u 1:(log($2 - 325.6)) title 'T(t)', 
f(x) title "Linear fit" 

but someway is not working, even if I plot the graph without the fit

p 'termom_COST_SCALED.txt' u 1:(log($2 - 325.6)) 

the outcome is the graph desired, which I want to make the fit from. Is the syntax of the fit wrong?

via a, b u 1:(log($2 - 325.6))

Solution

  • I think using should come before via, and you should ignore the header in your data file with every ::1:

    f(x) = a*x+b
    fit f(x) 'termom_COST_SCALED.txt' u 1:(log($2 - 325.6)) every ::1 via a,b
    
    p 'termom_COST_SCALED.txt' u 1:(log($2 - 325.6)) every ::1 title 'T(t)',\
      f(x) title "Linear fit"
    pause mouse
    

    The syntax is now correct.

    Your 2nd column is close to 295, which means that $2-325.6 is close to -31, and its log will be complex :

    gnuplot> print log(-31)
    {3.43398720448515, 3.14159265358979}
    

    Is it really what you want?