Fit many pieces of data in Gnuplot's for loop

Data

Model Decreasing-err Constant-err Increasing-err
2025 73-78 80-85 87-92 
2035 63-68 80-85 97-107 
2050 42-57 75-90 104.5-119.5 

which data-structure (use of -err) described here.

To plot the points, I run

set terminal qt size 560,270; 
set grid; set offset 1,1,0,0; 
set datafile separator " -"; 
set key autotitle columnhead;
plot for [i=2:6:2] "data.dat" using 1:(0.5*(column(i)+column(i+1))):(0.5*(column(i+1)-column(i))) with yerror; 

and get

enter image description here

However, I would like to add a line fits to these points which you cannot do just with with yerrorlines because of kinks.

My pseudocode for fitting the increasing and decreasing lines

inc(x) = k1*x + k2; 
con(x) = n1*x + n2;
dec(x) = m1*x + m2;
fit inc(x), con(x) dec(x) for [i=2:6:2] "data.dat" 
   using 1:(0.5*(column(i)+column(i+1))):(0.5*(column(i+1)-column(i))) 
   via k1,k2,n1,n2,m1,m2; 

where the problem is in using the function fit with for loop.

How can you use Gnuplot's fit in a for loop? I would like to fit many lines at the same time to the data.

Upvotes: 1

Views: 237

Answers (1)

Miguel
Miguel

Reputation: 7627

I would use do in conjunction with eval to do this:

# Define your functions (you can also use do + eval to do this)
f1(x) = a1*x+b1
f2(x) = a2*x+b2
f3(x) = a3*x+b3

# Loop
do for [i=1:3] {
eval sprintf("fit f%g(x) 'data.dat' u 0:%g via a%g, b%g", i, i, i, i)
}

You can adapt the above to your own purposes.

Upvotes: 2

Related Questions