0

I have a file with multiple columns and I would like to make multiple linear regressions (to be all plotted in same graph after regression) over the file using a loop as follows:

do for [i=1:6] {
g(i,x)= 'm'.i*x + 'b'.i
fit g(i,x) 'Dens.dat' using 1:(column(i+1)) via 'm'.i,'b'.i    
}

But I get an error: could not read parameter-file "m1"

How can I fix this?

Pedro R.
  • 93
  • 2
  • 9

1 Answers1

1

If your version of Gnuplot doesn't support arrays, then you could adapt an alternative solution, for example:

vGet(name, i) = value(sprintf("%s%i", name, i)) 
vSet(name, i, val) = sprintf("%s%i = %.16e", name, i, val)

N = 2
#array m[N]
#array b[N]

do for [i=1:N] {
    g(x)= alpha*x + beta
    fit g(x) 'Dens.dat' using 1:(column(i+1)) via alpha,beta

    eval vSet('m', i, alpha);
    eval vSet('b', i, beta);

    #m[i-1] = alpha
    #b[i-1] = beta
}

set key top left
set terminal pngcairo
set output 'fig.png'

plot \
    for [i=1:N] vGet('m', i)*x + vGet('b',i) w l t '', \
    for [i=1:N] 'Dens.dat' u 1:(column(i+1)) w p t sprintf("column %d", i)

in combination with Dens.dat as:

1   4   5
2   8   10
3   12  15
4   16  20

produces: enter image description here

Community
  • 1
  • 1
ewcz
  • 12,819
  • 1
  • 25
  • 47