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


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

Function defined by a while loop

Started byAntonio Lo Turco <antonio.lo.turco@gmail.com>
First post2011-06-27 02:50 -0700
Last post2011-06-28 11:56 +0200
Articles 5 — 4 participants

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


Contents

  Function defined by a while loop Antonio Lo Turco <antonio.lo.turco@gmail.com> - 2011-06-27 02:50 -0700
    Re: Function defined by a while loop James Waldby <not@valid.invalid> - 2011-06-27 16:21 +0000
      Re: Function defined by a while loop Mauro <pippo@hotmail.com> - 2011-06-28 11:48 +0200
    Re: Function defined by a while loop Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2011-06-27 22:00 +0200
      Re: Function defined by a while loop Mauro <pippo@hotmail.com> - 2011-06-28 11:56 +0200

#402 — Function defined by a while loop

FromAntonio Lo Turco <antonio.lo.turco@gmail.com>
Date2011-06-27 02:50 -0700
SubjectFunction defined by a while loop
Message-ID<b935110d-6f8d-4b0d-919c-3151effdf59f@28g2000yqu.googlegroups.com>
Hello everybody!
I would like to take advantage of the new construct while (<expr>)
{<commands>} in gnuplot to define a function something like that
f(x,<parameters>,x0 (initial guess of x), eps (maximum error), niter
(maximum no of iterations) =
x=x0;i=1; while (abs(x-x0)>eps && i<niter) {x=some function of x and
parameters;i=i+1}

Obviously I tried and it doesn't work!

The only way I managed to obtained some sensible result is to use the
instruction "call dummy.plt <parameters> x0 eps niter" where
"dummy.plt" script contains the while loop.

However I would like (and I'm not able) to implement such solution as
a user defined function.

I look forward to having your always valued suggestions

Antonio

[toc] | [next] | [standalone]


#405

FromJames Waldby <not@valid.invalid>
Date2011-06-27 16:21 +0000
Message-ID<iuaalu$14e$1@dont-email.me>
In reply to#402
On Mon, 27 Jun 2011 02:50:05 -0700, Antonio Lo Turco wrote:
> I would like to take advantage of the new construct while (<expr>)
> {<commands>} in gnuplot to define a function something like that
> f(x,<parameters>,x0 (initial guess of x), eps (maximum error), niter
> (maximum no of iterations) =
> x=x0;i=1; while (abs(x-x0)>eps && i<niter) {x=some function of x and
> parameters;i=i+1}
> 
> Obviously I tried and it doesn't work!
> 
> The only way I managed to obtained some sensible result is to use the
> instruction "call dummy.plt <parameters> x0 eps niter" where "dummy.plt"
> script contains the while loop.
> 
> However I would like (and I'm not able) to implement such solution as a
> user defined function.
...

Because you set x=x0 at start of f(), if eps is positive the condition 
abs(x-x0)>eps is always false and the while loop won't do anything.

I have gnuplot 4.4.0 installed, which apparently doesn't have 'while'
but a workaround like the following is possible.  In following, 
f(x,x0,eps,iter) is analogous to your f(...), and g(x,y) stands for
your "some function of x and parameters".  (I left out x=x0 statement.)

gnuplot> g(x,y)=(x+y)/2.0;
gnuplot> f(x,x0,eps,iter) = abs(x-x0)>eps ? (iter? f(g(x,x0),x0,eps,iter-1) : x) : x;
gnuplot> print f(15,0,.5,9),f(16,0,.5,9),f(17,0,.5,9)
0.46875 0.5 0.265625
gnuplot> print f(15,0,.5,3),f(16,0,.5,3),f(17,0,.5,3)
1.875 2.0 2.125

-- 
jiw

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


#412

FromMauro <pippo@hotmail.com>
Date2011-06-28 11:48 +0200
Message-ID<iuc7v1$i7p$1@tdi.cu.mi.it>
In reply to#405
Il 27/06/2011 18:21, James Waldby ha scritto:
> On Mon, 27 Jun 2011 02:50:05 -0700, Antonio Lo Turco wrote:
>> I would like to take advantage of the new construct while (<expr>)
>> {<commands>} in gnuplot to define a function something like that
>> f(x,<parameters>,x0 (initial guess of x), eps (maximum error), niter
>> (maximum no of iterations) =
>> x=x0;i=1; while (abs(x-x0)>eps&&  i<niter) {x=some function of x and
>> parameters;i=i+1}
>>
>> Obviously I tried and it doesn't work!
>>
>> The only way I managed to obtained some sensible result is to use the
>> instruction "call dummy.plt<parameters>  x0 eps niter" where "dummy.plt"
>> script contains the while loop.
>>
>> However I would like (and I'm not able) to implement such solution as a
>> user defined function.
> ...
>
> Because you set x=x0 at start of f(), if eps is positive the condition
> abs(x-x0)>eps is always false and the while loop won't do anything.

Oh, I actually introduced a conceptual mistake that would give a wrong 
result, but it wasn't the matter, because gnuplot merely refuses to give 
any result...

>
> I have gnuplot 4.4.0 installed, which apparently doesn't have 'while'
> but a workaround like the following is possible.  In following,
> f(x,x0,eps,iter) is analogous to your f(...), and g(x,y) stands for
> your "some function of x and parameters".  (I left out x=x0 statement.)
>
> gnuplot>  g(x,y)=(x+y)/2.0;
> gnuplot>  f(x,x0,eps,iter) = abs(x-x0)>eps ? (iter? f(g(x,x0),x0,eps,iter-1) : x) : x;
> gnuplot>  print f(15,0,.5,9),f(16,0,.5,9),f(17,0,.5,9)
> 0.46875 0.5 0.265625
> gnuplot>  print f(15,0,.5,3),f(16,0,.5,3),f(17,0,.5,3)
> 1.875 2.0 2.125
>

A further demonstration that a good use of human brain generally 
overcomes the need of new software functions!!!

Thank you indeed

Mauro

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


#410

FromHans-Bernhard Bröker <HBBroeker@t-online.de>
Date2011-06-27 22:00 +0200
Message-ID<96s5qpFmfqU1@mid.dfncis.de>
In reply to#402
On 27.06.2011 11:50, Antonio Lo Turco wrote:

> Obviously I tried and it doesn't work!

Of course it doesn't.  while is a _command_, not an expression.  You 
can't use it in the middle of an expression, like the one you use to 
define a function.

> However I would like (and I'm not able) to implement such solution as
> a user defined function.

The only way there is recursion.  But that will only work reasonably 
well for a _one_parameter_ loop.  You loop over 'x' and 'i' 
simultaneously.  That doesn't map cleanly to recursion over a single 
variable.

Well, unless you make them the real and imaginary part of a complex 
number that is:

iterate_f(x_i, x0, eps, maxiter) = \
     ((abs(real(x_i) - x0) < eps) || (imag(x_i) > maxiter)) \
     ? x_i \
     : iterate(f(real(x_i)) + (imag(x_i)+1)*{0,1}, x0, eps, maxiter)

print iterate(x0,x0,eps,maxiter)

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


#413

FromMauro <pippo@hotmail.com>
Date2011-06-28 11:56 +0200
Message-ID<iuc8dd$ir1$1@tdi.cu.mi.it>
In reply to#410
Il 27/06/2011 22:00, Hans-Bernhard Bröker ha scritto:
> On 27.06.2011 11:50, Antonio Lo Turco wrote:
>
>> Obviously I tried and it doesn't work!
>
> Of course it doesn't. while is a _command_, not an expression. You can't
> use it in the middle of an expression, like the one you use to define a
> function.
>
>> However I would like (and I'm not able) to implement such solution as
>> a user defined function.
>
> The only way there is recursion. But that will only work reasonably well
> for a _one_parameter_ loop. You loop over 'x' and 'i' simultaneously.
> That doesn't map cleanly to recursion over a single variable.
>
> Well, unless you make them the real and imaginary part of a complex
> number that is:
>
> iterate_f(x_i, x0, eps, maxiter) = \
> ((abs(real(x_i) - x0) < eps) || (imag(x_i) > maxiter)) \
> ? x_i \
> : iterate(f(real(x_i)) + (imag(x_i)+1)*{0,1}, x0, eps, maxiter)
>
> print iterate(x0,x0,eps,maxiter)

Thank you indeed!!!
Furthermore, it doesn't need while construct.
Nevertheless,I must congratulate all the Gnuplot development team for 
improving this unvaluable tool. The introduction of if, while and do for 
iterative constructs gives a further cut above to its practicality 
(maybe not for the functions ;-)).

Best regards

Mauro

[toc] | [prev] | [standalone]


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


csiph-web