Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.graphics.apps.gnuplot > #405
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | James Waldby <not@valid.invalid> |
| Newsgroups | comp.graphics.apps.gnuplot |
| Subject | Re: Function defined by a while loop |
| Date | Mon, 27 Jun 2011 16:21:18 +0000 (UTC) |
| Organization | A noiseless patient Spider |
| Lines | 35 |
| Message-ID | <iuaalu$14e$1@dont-email.me> (permalink) |
| References | <b935110d-6f8d-4b0d-919c-3151effdf59f@28g2000yqu.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| Injection-Date | Mon, 27 Jun 2011 16:21:18 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="OAvC6XS0NcIQcGJcjYhOHA"; logging-data="1166"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WRExf0cxNjG3JEXtbuAtQ" |
| User-Agent | Pan/0.133 (House of Butterflies) |
| Cancel-Lock | sha1:SSo+dNan6hEIIE8qRqYZ13nmozI= |
| Xref | x330-a1.tempe.blueboxinc.net comp.graphics.apps.gnuplot:405 |
Show key headers only | View raw
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
Back to comp.graphics.apps.gnuplot | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web