问题
I am trying to fit the power curve into my data with following gnuplot code.
set termoption enhanced
f(x) = a*x**b;
fit f(x) 'data.txt' via a,b
plot 'data.txt' with points title 'data points', \
f(x) with lines title sprintf('power fit curve f(x) = %.2f·x^{%.2f}', a, b)
It works fine for non-redundant data for x axis. (no repeats).
But for following type of data: It is fitting the curve only to points of first x value i.e. 1, (Starred). and not to the whole dataset.
Data:
1 2194*
1 2675*
1 1911*
2 966
2 1122
2 951
2 1356
3 935
3 934
4 851
4 886
4 849
4 597
回答1:
You must set appropriate starting values for a
and b
, because nonlinear fitting usually does not have one unique minimum. If you don't specify any starting values, 1
is assumed, which goes in the completely wrong direction in your case.
So, with the following script
set termoption enhanced
f(x) = a*x**b
b = -1
a = 2000
fit f(x) 'data.txt' via a,b
plot 'data.txt' with points title 'data points', \
f(x) with lines title sprintf('power fit curve f(x) = %.2f·x^{%.2f}', a, b)
you get a proper fit:

来源:https://stackoverflow.com/questions/18712839/power-curve-fitting-in-gnuplot-for-redundant-values