如何解决如何在 gnuplot 中移动原点?
B x(cm)
24.5 4.2
25.5 4.5
26.5 5.0
27.5 5.4
28.5 5.9
29.5 6.6
30.5 7.2
31.5 7.9
32.5 8.6
33.5 9.3
34.5 10.0
35.5 10.5
36.5 10.9
37.5 11.1
38.5 11.1
39.5 10.8
40.5 10.3
41.5 9.8
42.5 9.2
43.5 8.4
44.5 7.7
45.5 7.1
46.5 6.4
47.5 5.9
48.5 5.4
49.5 5.0
50.5 4.6
51.5 4.2
这是我的数据。
y(x) = a/(b**2 + x**2)**3/2
是我想要拟合上述数据的等式,但我面临的问题是 b 的值变为负值。所以我想知道我将如何更改图形的原点以获得正确的结果
解决方法
一些事情:
- 您确定函数是
f(x) = a/(b**2 + x**2)**3/2
而不是f(x) = a/(b**2 + x**2)**(3/2)
,请注意(3/2)
周围的括号。 - gnuplot 有整数除法(意外结果的常见陷阱),因此,
(3/2)
将被评估为1
而不是预期的1.5
。 - 为什么不让 gnuplot 找到偏移量?只需引入一个变量
c
,它将考虑 x 偏移并让它适合。 - 根据您的模型,即如果指数是可变的,您还可以为指数添加一个变量
d
并让它通过 gnuplot 拟合算法找到。 - 有时,如果您使用良好的起始值帮助拟合会更好。
然后您必须判断拟合值是否有意义,例如b<0
或 d=0.794
...
代码:
### fitting with finding x-offset automatically
reset session
$Data <<EOD
B x(cm)
24.5 4.2
25.5 4.5
26.5 5.0
27.5 5.4
28.5 5.9
29.5 6.6
30.5 7.2
31.5 7.9
32.5 8.6
33.5 9.3
34.5 10.0
35.5 10.5
36.5 10.9
37.5 11.1
38.5 11.1
39.5 10.8
40.5 10.3
41.5 9.8
42.5 9.2
43.5 8.4
44.5 7.7
45.5 7.1
46.5 6.4
47.5 5.9
48.5 5.4
49.5 5.0
50.5 4.6
51.5 4.2
EOD
f1(x) = a1/(b1**2 + (x-c1)**2)**(3/2)
f2(x) = a2/(b2**2 + (x-c2)**2)**(3./2)
f3(x) = a3/(b3**2 + (x-c3)**2)**d3
set fit quiet nolog
fit f1(x) $Data u 1:2 via a1,b1,c1
fit f2(x) $Data u 1:2 via a2,b2,c2
a3=11; b3=1; c3=40; d3=1.5 # sometimes it's better to help the fitting with some good starting values
fit f3(x) $Data u 1:2 via a3,b3,c3,d3
print sprintf("% 9s% 9s% 9s% 9s","a","b","c","d")
print sprintf("%9.3g %9.3g %9.3g",a1,c1)
print sprintf("%9.3g %9.3g %9.3g",a2,c2)
print sprintf("%9.3g %9.3g %9.3g %9.3g",a3,d3)
plot $Data u 1:2 w p pt 7,\
f1(x) w l lc "red",\
f2(x) w l lc "web-green",\
f3(x) w l lc "web-blue"
### end of code
结果:
a b c d
1.17e+03 10.3 37.9
2.73e+04 -13.6 37.9
343 8.66 37.9 0.794
,
我不确定,但我认为您尝试拟合的方程可能不适合数据。也许你可以重写你的方程,使其更清晰。
以下是使用二次方程 y(x) = a*x**2 + b*x + c
拟合的示例:
test.dat
24.5 4.2
25.5 4.5
26.5 5.0
27.5 5.4
28.5 5.9
29.5 6.6
30.5 7.2
31.5 7.9
32.5 8.6
33.5 9.3
34.5 10.0
35.5 10.5
36.5 10.9
37.5 11.1
38.5 11.1
39.5 10.8
40.5 10.3
41.5 9.8
42.5 9.2
43.5 8.4
44.5 7.7
45.5 7.1
46.5 6.4
47.5 5.9
48.5 5.4
49.5 5.0
50.5 4.6
51.5 4.2
quad_fit.gp
set term pos col
set out 'xy_fit.ps'
set title 'Quadratic Regression Example Scatterplot'
set ylabel 'Y'
set xlabel 'X'
set style line 1 ps 1.5 pt 7 lc 'red'
set style line 2 lw 1.5 lc 'blue'
set grid
f(x) = a*(x**2) + b*x + c
fit f(x) 'test.dat' using 1:2 via a,b,c
p 'test.dat' ls 1 t 'Datapoints',f(x) ls 2 t 'Quadratic Regression'
set out
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。