6

I need to make a linear approximation. However it needs to be in a log scale.

Here is my gnuplot script:

f(x)= a*x+b
fit f(x) "d0.dat" via a,b
set logscale x
set logscale y
plot "d0.dat" with points lt rgb "#ff0000" title "Points", \
f(x) with lines lt rgb "#ff00ff" title "Approximation"

enter image description here

Clearly the approximation is wrong. Can anyone help me to fix it. I didn't find any thing in google.

Rafael Castro
  • 519
  • 4
  • 12

2 Answers2

4

Gnuplot is correctly fitting your data to the function you provided--a straight line.

The problem is that using a log scale for the y axis does not scale the data--just how the data are plotted.

Try fitting it to a power law:

f(x)= a*x**b
fit f(x) "d0.dat" via a,b
set logscale x
set logscale y
plot "d0.dat" with points lt rgb "#ff0000" title "Points", \
f(x) with lines lt rgb "#ff00ff" title "Approximation"
andyras
  • 14,482
  • 3
  • 48
  • 71
4

I actually recommend a fit in logscale directly:

fl(x) = a+b*x
fit fl(x) 'data.dat'  u (log($1)):(log($2))  via a,b
replot exp(fl(log(x))) t 'log approx'

The difference is appreciable when (a few) values for large x are out of the fit. The cost-function is otherwise too strongly affected (because x and y are exponentially large).

Demi-Lune
  • 1,446
  • 2
  • 11
  • 20