Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Defining multiple similar functions, and fitting them, using a for loop

1,416 views
Skip to first unread message

Thomas Grant

unread,
Mar 14, 2013, 1:34:10 PM3/14/13
to
Dear all,

I am trying to use gnuplot to iterate through a list of data files and fit the data in them with a simple linear regression. Then I would like to plot all the data points and the accompanying fits on the same graph. From the help it seems like the for loop iteration only works for plotting commands, and not for fit.

First, I would like to fit all the data files to a function:

f1(x)=m1*x+b1
f2(x)=m2*x+b2
etc...

then I'd like to fit them:

fit f1(x) 'datafile_1.dat' via m1,b1
etc....

then I'd like to plot the data files, which I can do with a for loop:

plot for [i=1:8] 'datafile_'.i.'.dat'

however, I'd also like to plot the fits, which doesn't seem to work with a for loop. I've tried the following (after individually defining f1(x), f2(x), etc...):

plot for [i=1:8] fi(x)
plot for [i=1:8] f.i.(x)
plot for [i=1:8] j=i fj(x)
plot for [i=1:8] j=i f.j.(x)
plot for [i=1:8] j=i "f".j."(x)"

and none of these seem to work. It occurs to me it may be that gnuplot doesn't seem to support the idea of defining functions with a variable, even though it would allow something such as:

plot for [i=1:8] j=i sin(i*x)

which would plot eight functions, sin(x), sin(2x), etc...

If anyone has any ideas on fitting multiple functions, I'd love to hear it. Or if anyone has any ideas on defining/plotting functions with variable names, I'd also love to hear it.

If possible I would like to avoid scripting, since I would like to be able to make edits interactively in gnuplot, rather than editing a script, running it, looking at the output, re-editing the script, running it, etc...

Thanks for your suggestions,
Tom

Karl

unread,
Mar 15, 2013, 3:49:58 AM3/15/13
to
Am 14.03.2013 18:34, schrieb Thomas Grant:
> First, I would like to fit all the data files to a function:
>
> f1(x)=m1*x+b1
> f2(x)=m2*x+b2
> etc...
>
> then I'd like to fit them:
>
> fit f1(x) 'datafile_1.dat' via m1,b1
> etc....
>
> then I'd like to plot the data files, which I can do with a for loop:
>
> plot for [i=1:8] 'datafile_'.i.'.dat'


I悲 say you define all your variables m1,m2,b1,b2, ...., and then fit
them on

m1=1;m2=1;m3=1;b1=1;b2=1;b3=1

f(x,i) = value("m".i) * x + value("b".i)
fname(i) = "datafilenr".i.".dat"

do for [i=1:3] {
fit f(x,i) fname(i) via value("m".i), value("b".i)
}

plot for [i=1:3] fname(i), f(x,i)


OK, i tried this myself, and it works for the plotting, but the "value"
statement is not evaluated correctly in "fit".

This might be a bug in gnuplot.

Best regards, Karl


Karl

unread,
Mar 15, 2013, 3:59:18 AM3/15/13
to
Am 15.03.2013 08:49, schrieb Karl:
>
> m1=1;m2=1;m3=1;b1=1;b2=1;b3=1
>
> f(x,i) = value("m".i) * x + value("b".i)

> plot for [i=1:3] f(x,i)

Ha, I finally have array variables in gnuplot!

Still, i悲 be very happy to see them implemented properly some day.

;-)

Karl

unread,
Mar 15, 2013, 9:45:15 AM3/15/13
to
Am 15.03.2013 08:49, schrieb Karl:

> This might be a bug in gnuplot.

I just filed it on sf.net

https://sourceforge.net/p/gnuplot/bugs/1222/


K

Karl

unread,
Mar 15, 2013, 11:50:05 AM3/15/13
to
Am 15.03.2013 08:49, schrieb Karl:
> fit f(x,i) fname(i) via value("m".i), value("b".i)

This of course cannot work. "value" returns the value stored in the
variable, and not it愀 name.
Should have noticed that myself.

K

sfeam

unread,
Mar 15, 2013, 2:52:29 PM3/15/13
to
Karl wrote:

> Am 15.03.2013 08:49, schrieb Karl:
>> fit f(x,i) fname(i) via value("m".i), value("b".i)
>
> This of course cannot work. "value" returns the value stored in the
> variable, and not it´s name.
> Should have noticed that myself.

You could use macros, however:

set macro
var1 = "m".i
var2 = "b".i

fit f(x,i) fname(i) via @var1, @var2


Ethan

kress....@gmail.com

unread,
May 23, 2013, 4:03:45 PM5/23/13
to
I am trying to do the same, but can't get it.
could you please post your running code?

thanks florian

sfeam

unread,
May 23, 2013, 6:34:33 PM5/23/13
to
kress....@gmail.com wrote:

> On Thursday, March 14, 2013 6:34:10 PM UTC+1, Thomas Grant wrote:
>> Dear all,
>>
>> First, I would like to fit all the data files to a function:
>>
>> f1(x)=m1*x+b1
>>
>> f2(x)=m2*x+b2
>>
>> etc...
>>
>>
>> then I'd like to fit them:
>>
>> fit f1(x) 'datafile_1.dat' via m1,b1
>>
>> etc....
>>
>>
>> then I'd like to plot the data files, which I can do with a for loop:
>>
>>
>> plot for [i=1:8] 'datafile_'.i.'.dat'
>>
>>
>> however, I'd also like to plot the fits, which doesn't seem to work with
>> a for loop. I've tried the following (after individually defining f1(x),
>> f2(x), etc...):
>>
>>
>>
>> plot for [i=1:8] fi(x)
>>
>> plot for [i=1:8] f.i.(x)
>>
>> plot for [i=1:8] j=i fj(x)
>>
>> plot for [i=1:8] j=i f.j.(x)
>>
>> plot for [i=1:8] j=i "f".j."(x)"
>>
>>
>>
>> and none of these seem to work.

I don't have an answer for handling an unbounded number of previously
unknown functions, but for some reasonable number, say 10 or so, you
can create a little pre-definition script that maps fi(x) onto g(i,x)

g(i,x) = \
i == 1 ? f1(x) : \
i == 2 ? f2(x) : \
i == 3 ? f3(x) : \
i == 4 ? f4(x) : \
i == 5 ? f5(x) : \
i == 6 ? f6(x) : \
i == 7 ? f7(x) : \
i == 8 ? f8(x) : \
i == 9 ? f9(x) : \
i == 10 ? f10(x) : \
NaN


Now your plot command is

plot for [i=1:8] g(i,x)


Ethan
0 new messages