在gnuplot中拟合和绘制序列 [英] Fitting and plotting a series in gnuplot

查看:96
本文介绍了在gnuplot中拟合和绘制序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于此问题

This is similar to this question and this but my problem is that I have several hundred files that need to be simultaneously fitted and plotted on one graph. Unlike the other questions posted I'm looking for the best fits for each file, not for the global data set so cat won't work.

我希望像使用fit.. for一样使用fit.. for,但是效果不是很好.这是到目前为止我得到的:

I was hoping to use fit.. for like I do for plot but it's not working so well. Here's what I've got so far:

f(x) = 1+d*exp(-(x-f)**2/(2*(g**2)))+a*exp((-(x-b)**2)/(2*(c**2)))

filename(n) = sprintf("rheosaxs_saxs_%005d_0001_var_rebin_div.dat", n)
fit f(x) for [n=575:584] filename(n) u 1:2 via a,b,c,d,f,g
plot for [n=575:584] filename(n) using 1:2, f(x)

我得到的错误是:line 60: undefined variable: for 对应于fit f(x) for [n=a:b]

the error I get is: line 60: undefined variable: for which corresponds to the fit f(x) for [n=a:b]

我知道我的起始参数是合理的,因为我可以在不使用fit命令的情况下绘制它们,并且它们看起来很合理.同样,我的plot for可以正常工作.

I know that my starting parameters are reasonable because I can plot them without the fit command and they look sensible. Similarly my plot for works ok.

有什么想法吗?谢谢:)

Any ideas? Thank you :)

推荐答案

在5.2版中,gnuplot引入了数组,使您可以保存每次拟合的结果并在以后进行绘制.

In version 5.2 gnuplot introduces arrays, which allow you to save the results of each fit and plot those later.

一个简化的示例脚本是

file(n) = sprintf('myfile_%d.dat', n)
f(a, x) = a*x

array A[10]
do for [i=1:10] {
    tmpA = 1
    fit f(tmpA, x) file(i) via tmpA
    A[i] = tmpA
}

plot for [i=1:10] file(i),\
     for [i=1:10] f(A[i], x)

尽管gnuplots数组是作为用户变量的链接列表实现的,但是不可能直接使用A[i]进行拟合,但是我不得不使用一个临时变量来使其正确.

Although gnuplots arrays are implemented as linked list of user variables, it is not possible to use A[i] directly for the fit, but I had to use a temporary variable to get it right.

一个完整的工作示例,包括使用gnuplot的python生成的随机数据,uargh;):

A full working example, including generation of random data, with python from gnuplot, uargh ;):

# generate some random data
system("python3 -c 'import random\nfor i in range(1, 11):\n\twith open(\"output_{0}.dat\".format(i), \"w\") as f:\n\t\tf.write(chr(10).join([str(i*100 + i* x * (1 + 0.1*(random.random()-0.5))) for x in range(0,100)]))'")


file(n) = sprintf('output_%d.dat', n)

f(a, b, x) = a*x + b
array A[10]
array B[10]
do for [i=1:10] {
    tmpA = 1
    tmpB = 1
    fit f(tmpA, tmpB, x) file(i) u 0:1 via tmpA, tmpB
    A[i] = tmpA
    B[i] = tmpB
}

plot for [i = 1:10] file(i) u 0:1 with points lt i notitle, \
     for [i=1:10] f(A[i], B[i], x) with lines lt i notitle

顺便说一句:没有fit for,因为它等效于do for { fit }.但是在绘制时,plot for会生成具有多个功能的单个图,而do for { plot }会绘制多个图,应与multiplot

BTW: There is no fit for, because that is equivalent to do for { fit }. But when plotting, plot for generates a single plot with multiple functions, whereas do for { plot } makes several plots and should be used with multiplot

这篇关于在gnuplot中拟合和绘制序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆