Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gnuplot- printing fit parameters automatically

Tags:

gnuplot

In gnuplot, is there a way to print the fit parameters automatically on the generated figure? For example, If I fit the data table

1   1
2   2
3   3

using:

a=1
b=1
f(x) = a*x + b
fit f(x) 'data' using 1:2 via a, b

I'll the result a=1 and b=0. I want to print them using something like

set label 'a=$a, b=$b' at (1,1)
show label

The $ trick doesn't work so I hoped you could give me some tips...

like image 594
Yotam Avatar asked Jul 06 '11 07:07

Yotam


2 Answers

What you are trying to do is very well possible. The problem you are encounting is, that your fitting algorithm crashes due to a singular matrix inversion. You can resolve that problem in a couple of ways. The easiest is to limit the amount of iteration to find the fitting curve. So this script:

a=1
b=1
FIT_MAXITER = 1
f(x) = a*x + b
ti = sprintf("%.2fx+%.2f", a, b)
fit f(x) 'data' using 1:2 via a, b
plot [0:3] f(x) t ti, "data" w l

should do exactly what you are aiming for.

Note that the singular matrix inversion problem should not arise when your data is noisy or your setup function does not have the exact structure as your data. For example this

f(x) = a*x**2 + b

function should work just fine without limiting the number of iterations.

Further ways to control the fitting process are described in the gnuplot documentation (gnuplot.pdf or help set fit).

like image 55
Woltan Avatar answered Nov 13 '22 20:11

Woltan


I've found something very interesting that may solve your problem here. The solution seems to be using the function sprintf and the usual syntax of C to print on a string. I. E., as in the link:

f(x) = m*x + c
fit f(x) "file" using 3:1 via m,c
set label 1 sprintf("m = %3.4f",m) at 510,75 font ",18"
set label 2 sprintf("c = %3.4f",c) at 510,70 font ",18"
like image 26
opisthofulax Avatar answered Nov 13 '22 20:11

opisthofulax