gnuplot linear fit within for loop -
gnuplot linear fit within for loop -
i have script correctly plots 100 separate info files. want linear fit of each plot within for loop. however, script crashes when gets fit command message "skipping unreadable file "f(x) = a100*x + b%d". can help right syntax? give thanks you.
plotfile = "graph.eps" set output plotfile n=100 filename(n) = sprintf("%d_mod.int", n) fstr(n) = sprintf('f(x) = a%d*x + b%d ' , n) plot [i = 1:n] filename(i) u 1:2 title sprintf("%d", i) w lp fit [i = 1:n] fstr(i) filename(i) u 1:2 via a, b
fit
doesn't back upwards iterations. here how can it, needs fiddling ;)
# define functions depending on current number fstr(n) = sprintf("f%d(x) = a%d*x + b%d", n, n, n) # fitting string specific file , related function fitstr(n) = sprintf("fit f%d(x) 'file%d.dat' via a%d,b%d", n, n, n, n) n = 2 # fits [i=1:n] { eval(fstr(i)) eval(fitstr(i)) } # build finish plotting string plotstr = "plot " [i=1:n] { plotstr = plotstr . sprintf("f%d(x), 'file%d.dat'%s ", i, i, (i == n) ? "" : ", ") } eval(plotstr)
this works fine me, if utilize next 2 test files:
file file1.dat
:
1 1 2 2.1 3 3
and file2.dat
:
1 1.5 2 2.7 3 4
the result 4.6.5 is:
to have actual result of fitting displayed in key must build plotting string plotstr
follows:
plotstr = "plot " [i=1:n] { t = sprintf("f%d(x) = %.2f*x + %.2f", i, value(sprintf('a%d', i)), value(sprintf('b%d', i))) plotstr = plotstr . sprintf("f%d(x) lt %d t '%s', ", i, i, t).\ sprintf(" 'file%d.dat' lt %d %s ", i, i, (i == n) ? "" : ", ") }
with result
gnuplot
Comments
Post a Comment