Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gnuplot ignores second branch in multi-branch fitting

I want to do a multi-branch fit with gnuplot. I defined two functions z1(x) and z2(x) and I want that z1(x) is fitted with all data up to line 49 and starting from line 50 it should fit to z2(x). This is what I do in the fitting part:

z(x,y) = (y < 50) ? z1(x) : z2(x)

fit z(x,y) 'data.txt' using  1:-1:2  via a, b

But gnuplot completly ignores the z2(x) part. I can even comment out the defintion of z2(x), it gives not even an error nor a warning. It never reaches the case where y >= 50, but the data.txt file has 100 lines.

Also, if I plot z1(x) and z2(x) afterwards, they are just constant zero (z1 = z2 = 0), but the fit parameters a and b are roughly (not fully, because 2nd branch is ignored) in the right order.

Am I doing something wrong here? Is there a good example somehwere (the official gnuplot fit demo didn't help)?

like image 271
Foo Bar Avatar asked Mar 06 '26 09:03

Foo Bar


1 Answers

Provided that your line number in the 'data.txt' file is the x variable for your function z(x), you can make it simpler (this is necessary for this to work, actually!!!). Your function z(x,y) is in reality just single variable function z(x) defined separately on two intervals. I re-created your problem with a sample data file, where the ranges for z1(x) and z2(x) break at the 10th point of my data file (you want 49 or 50 there).

The following code does what you want; defines the function z(x) differently for the first 10 data points and the rest (this is where the x HAS to be equal to line number),

z1(x) = a*x + b
a = 1.2
b = 1.2
z2(x) = a + b
z(x) = x < 10 ? z1(x) : z2(x)

then fit the data file using the data point line number (column(0)) as x and the value on the respective line (1) as y, varying parameters a and b

 fit z(x) 'data.txt' using (column(0)):1 via a,b

and plot to see how we do

 plot 'data.txt' using (column(0)):1, z(x)

If this doesn't work, you may look into the index option for plotting data files. This would require separating the data ranges (first 50 points) in your data file by one blank line (or two, can't remember) and modify the script accordingly.

like image 183
MickLock Avatar answered Mar 08 '26 23:03

MickLock