Antonis P
Antonis P

Reputation: 79

fit straight lines with gnuplot

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?

Upvotes: 3

Views: 18175

Answers (3)

Karl
Karl

Reputation: 2187

You have a two options, I recommend the first:

  • replace your variable b by (b+n), and substract the fixed variable n (can be one, or any other arbitrary value that is not very small of very large) from the result afterwards. That way $b$ does not vanish, and the gnuplot algorithm will fit successfully & exit without errormessage. Instead it just shows a notice saying "Hmmmm.... Sum of squared residuals is zero. Can't compute errors."

  • give a very small starting value for b. Gnuplot prescales its internal representative of $b, that let's the fit converge before the error turns up.

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.

Upvotes: 0

DanielTuzes
DanielTuzes

Reputation: 2744

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!

Upvotes: 3

andyras
andyras

Reputation: 15910

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.

Upvotes: 4

Related Questions