在gnuplot中拟合一个数据集作为其他数据集的函数

问题描述 投票:0回答:2

我假设一个数据集包含其他两个数据集的混合,但不知道确切如何在gnuplot中进行计算。实际上,我想使用类似以下函数的数据集2和数据集1“拟合”数据集3:输出= a *(数据集1)+ b *(数据集2)得到a和b。

gnuplot data-fitting
2个回答
1
投票

假设您有一些这样的数据:

1 1 6.95322
1 2 10.9421
1 3 14.9826
1 4 19.4772
2 1 10.03
2 2 13.8502
2 3 17.9662
2 4 21.9018
3 1 12.7868
3 2 16.8742
3 3 21.0967
3 4 24.3392
4 1 16.4094
4 2 20.2189
4 3 24.5766
4 4 27.1483

您可以编写函数“ output = a *(dataset1)+ b *(dataset2)”并像这样拟合它:

f(x,y) = a*x + b*y
fit f(x,y) "a.dat" using 1:2:3 via a,b

结果:

...
Final set of parameters            Asymptotic Standard Error
=======================            ==========================
a               = 3.02235          +/- 0.06193      (2.049%)
b               = 3.95873          +/- 0.06193      (1.564%)
...

我已经用a=3b=4生成了示例数据。


0
投票

这基本上与maij的解决方案相同,但是具有复制,粘贴和运行代码,包括“ x”数据和插图。每次,它都会创建一些带有随机噪声的频谱。只需跳过这一部分,然后用文件替换$Data。如果您的数据集位于不同的文件中,则必须将其复制到一起或使用gnuplot玩一些技巧。确保所有数据集的“ x”坐标和步长都相同,否则必须进行插值以具有相同的x值。

代码:

### fit to find superposition factors
reset session

# generate some random test data
f1(x) = exp(-(x**2)/5) + rand(0)*0.05
f2(x) = exp(-((x-4)**2)/5) + rand(0)*0.05
a0 = int(rand(0)*10+5)
b0 = int(rand(0)*10+2)
f3(x) = a0*f1(x) + b0*f2(x) + rand(0)*0.05
set table $Data
    plot '+' u 1:(f1(x)):(f2(x)):(f3(x)) w table
unset table

set fit quiet results
f(x,y) = a*x + b*y
fit f(x,y) $Data using 2:3:4 via a,b

plot \
    $Data u 1:2 w lp pt 7 ps 0.4 ti "Dataset 1", \
    '' u 1:3 w lp pt 7 ps 0.4 ti "Dataset 2", \
    '' u 1:4 w lp pt 7 ps 0.4 ti "Dataset 3", \
    '' u 1:(a*$2 + b*$3) w l lw 2 \
    ti sprintf("\n\n\nSuperposition\n D3 = a*D1 + b*D2\na: %g\nb: %g", a,b)
### end of code

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.