Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.graphics.apps.gnuplot > #1692 > unrolled thread

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

Started byThomas Grant <tomer485@gmail.com>
First post2013-03-14 10:34 -0700
Last post2013-05-23 15:34 -0700
Articles 8 — 4 participants

Back to article view | Back to comp.graphics.apps.gnuplot


Contents

  Defining multiple similar functions, and fitting them, using a for loop Thomas Grant <tomer485@gmail.com> - 2013-03-14 10:34 -0700
    Re: Defining multiple similar functions, and fitting them, using a for loop Karl <mail.kfr@gmx.net> - 2013-03-15 08:49 +0100
      Re: Defining multiple similar functions, and fitting them, using a for loop Karl <mail.kfr@gmx.net> - 2013-03-15 08:59 +0100
      Re: Defining multiple similar functions, and fitting them, using a for loop Karl <mail.kfr@gmx.net> - 2013-03-15 14:45 +0100
      Re: Defining multiple similar functions, and fitting them, using a for loop Karl <mail.kfr@gmx.net> - 2013-03-15 16:50 +0100
        Re: Defining multiple similar functions, and fitting them, using a for loop sfeam <sfeam@users.sourceforge.net> - 2013-03-15 11:52 -0700
    Re: Defining multiple similar functions, and fitting them, using a for loop kress.florian@gmail.com - 2013-05-23 13:03 -0700
      Re: Defining multiple similar functions, and fitting them, using a for loop sfeam <sfeam@users.sourceforge.net> - 2013-05-23 15:34 -0700

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

FromThomas Grant <tomer485@gmail.com>
Date2013-03-14 10:34 -0700
SubjectDefining multiple similar functions, and fitting them, using a for loop
Message-ID<52d92c72-a809-490f-86e9-c577132ffa44@googlegroups.com>
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

[toc] | [next] | [standalone]


#1693

FromKarl <mail.kfr@gmx.net>
Date2013-03-15 08:49 +0100
Message-ID<khujr6$1dr$1@news.rz.uni-karlsruhe.de>
In reply to#1692
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´d 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

[toc] | [prev] | [next] | [standalone]


#1694

FromKarl <mail.kfr@gmx.net>
Date2013-03-15 08:59 +0100
Message-ID<khukcn$1ke$1@news.rz.uni-karlsruhe.de>
In reply to#1693
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´d be very happy to see them implemented properly some day.

;-)

[toc] | [prev] | [next] | [standalone]


#1699

FromKarl <mail.kfr@gmx.net>
Date2013-03-15 14:45 +0100
Message-ID<khv8la$9ii$1@news.rz.uni-karlsruhe.de>
In reply to#1693
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

[toc] | [prev] | [next] | [standalone]


#1700

FromKarl <mail.kfr@gmx.net>
Date2013-03-15 16:50 +0100
Message-ID<khvfvb$ch6$1@news.rz.uni-karlsruhe.de>
In reply to#1693
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.

 K

[toc] | [prev] | [next] | [standalone]


#1701

Fromsfeam <sfeam@users.sourceforge.net>
Date2013-03-15 11:52 -0700
Message-ID<khvqi6$fd1$1@dont-email.me>
In reply to#1700
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

[toc] | [prev] | [next] | [standalone]


#1853

Fromkress.florian@gmail.com
Date2013-05-23 13:03 -0700
Message-ID<eee78761-c914-4658-86b1-c10ee0fa56e8@googlegroups.com>
In reply to#1692
On Thursday, March 14, 2013 6:34:10 PM UTC+1, Thomas Grant wrote:
> 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

I am trying to do the same, but can't get it.
could you please post your running code?

thanks florian

[toc] | [prev] | [next] | [standalone]


#1854

Fromsfeam <sfeam@users.sourceforge.net>
Date2013-05-23 15:34 -0700
Message-ID<knm5a5$oi0$1@dont-email.me>
In reply to#1853
kress.florian@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.graphics.apps.gnuplot


csiph-web