2
votes

i want to use gnuplot to get fit parameters of a first degree polynomial equation (F(x)=a*x+b) for many curvers. Some of the curves are represented exactly straight lines.

For example if my data look like

1 1
2 2
3 3
4 4

which can be represented with the f(x)=x (param a=0, b=0).

But the following gnuplot code

# regression line
f(x) = a*x + b
fit f(x) './test.dat' u 1:2  via a, b

fails to compute fit params giving the message below

Singular matrix in Invert_RtR

update: It seems that gnuplot does not "crash" if i define the number of iterations for fit function

FIT_MAXITER = 1
# regression line
f(x) = a*x + b
fit f(x) './test.dat' u 1:2  via a, b

It should be a=1 and b=0. But gnuplot gives

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

a               = 0.989067         +/- 0.004339     (0.4387%)
b               = 0.0761393        +/- 0.02692      (35.36%)

How can i "force" gnuplot compute the correct values of a and b?

3

3 Answers

4
votes

I have found two solutions:

1) Add a tiny offset to your fit function:

f(x) = a*x + b + 1e-9

This prevents the singularity issue, and results in a perfectly correct fit (a = 1, b=-1e-9).

2) Eliminate the b parameter altogether

f(x) = a*x

This assumes that your fit lines will all go through 0, which may of course not be what you want.

3
votes

As I see the problem is probably that f(x) can be fitted exactly to the data. If you add any nonzero value to any of your data, you get no error. In real life, this exact fitting simply does not occur (you have noise). Anyway, regardless gnuplot says "error during fit", it seems that gnuplot fits the function correctly.

Your solution by adding FIT_MAXITER = 1 can be a workaround. The higher value you define for FIT_MAXITER, the better fitting you get. But if you give too high value for it, the fitting will be exact (the error will be less than the number precision).

Try to fit f(x) on your real data and tell us what you get!

0
votes

You have a few options:

  • replace your variable b by (b+1), and substract 1 from the result afterwards. That way your variable does not vanish, and the gnuplot algorithm will fit successfully & exit without errormessage, when it finds that the numeric error has reached zero.

  • give a very small starting value for b.

General rule for fitting: You parameters need to be of the same order of magnitude, and be initialised to be of the correct order of magnitude.