gnuplot线性拟合在for循环内 [英] gnuplot linear fit within for loop
问题描述
我有一个脚本可以正确绘制100个单独的数据文件.现在我想做一个线性 for 循环中每个图的拟合.但是,当我的脚本进入 fit 命令并显示消息正在跳过不可读的文件"f(x)= a100 * x + b%d")时,崩溃.有人可以提供正确语法的帮助吗?你.
I have a script that correctly plots 100 separate data files. Now I want to do a linear fit of each plot within a for loop. However, my script crashes when it gets to the fit command with the message "Skipping unreadable file "f(x) = a100*x + b%d". Can anyone help with the correct syntax? Thank 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 for [i = 1:n] filename(i) u 1:2 title sprintf("%d", i) w lp
fit for [i = 1:n] fstr(i) filename(i) u 1:2 via a, b
推荐答案
fit
不支持迭代.这是您可以执行的操作,但是需要一些摆弄;)
fit
doesn't support iterations. Here is how you can do it, but it needs some fiddling ;)
# define the functions depending on the current number
fstr(N) = sprintf("f%d(x) = a%d*x + b%d", N, N, N)
# The fitting string for a specific file and the related function
fitstr(N) = sprintf("fit f%d(x) 'file%d.dat' via a%d,b%d", N, N, N, N)
n = 2
# Do all the fits
do for [i=1:n] {
eval(fstr(i))
eval(fitstr(i))
}
# construct the complete plotting string
plotstr = "plot "
do for [i=1:n] {
plotstr = plotstr . sprintf("f%d(x), 'file%d.dat'%s ", i, i, (i == n) ? "" : ", ")
}
eval(plotstr)
如果我使用以下两个测试文件,这对我来说很好:
This works fine for me, if I use the following two test files:
文件file1.dat
:
1 1
2 2.1
3 3
和file2.dat
:
1 1.5
2 2.7
3 4
4.6.5的结果是:
The result with 4.6.5 is:
要使拟合的实际结果显示在键中,必须按以下方式构造绘图字符串plotstr
:
To have the actual result of the fitting displayed in the key you must construct the plotting string plotstr
as follows:
plotstr = "plot "
do for [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) ? "" : ", ")
}
结果
这篇关于gnuplot线性拟合在for循环内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!