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


Groups > comp.soft-sys.math.mathematica > #6112 > unrolled thread

iteration question

Started byFrancisco Gutierrez <fgutiers2002@yahoo.com>
First post2011-11-02 11:25 +0000
Last post2011-11-05 09:47 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  iteration question Francisco Gutierrez <fgutiers2002@yahoo.com> - 2011-11-02 11:25 +0000
    Re: iteration question "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-11-03 08:46 +0000
    Re: iteration question "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-11-03 08:47 +0000
    Re: iteration question Szabolcs Horvát <szhorvat@gmail.com> - 2011-11-03 08:57 +0000
    Re: iteration question Francisco Gutierrez <fgutiers2002@yahoo.com> - 2011-11-05 09:47 +0000

#6112 — iteration question

FromFrancisco Gutierrez <fgutiers2002@yahoo.com>
Date2011-11-02 11:25 +0000
Subjectiteration question
Message-ID<j8r9b8$3ie$1@smc.vnet.net>
Dear Group:

I have a function, and I iterate it using fixedpoint. Something of this sort:

FixedPointList[
 function[arg1,arg2,arg3, #] &, arg4, 
 SameTest -> (Max[Abs[Flatten[#1] - Flatten[#2]]] < 0.01 &)]


I would like to know how many steps it takes this function to converge. I have tried with EvaluationMonitor to no avail:

Block[{veamos, c = 0}, 
  veamos = FixedPoint[
    function[arg1,arg2,arg3, #] &, arg4, 
    SameTest -> (Max[Abs[Flatten[#1] - Flatten[#2]]]< 0.01 &)]; 
  EvaluationMonitor :> c++; {veamos, c}] 


The iterator c simply does not move, and the previous function works ok but returns the value of c as 0.

I tried if this was true with Length[FixedPointList[...]] and the answer was 20 (which should be the value of c in the immediately previous function). In principle, this would solve my problem, but it seems
rather inefficient, especially when the convergence criterion is severe (not 0.01, but say 10^-4).

Is there an efficient and nice way to solve this?

Thanks,

Francisco

[toc] | [next] | [standalone]


#6132

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-11-03 08:46 +0000
Message-ID<j8tkcj$es3$1@smc.vnet.net>
In reply to#6112
On Wed, 02 Nov 2011 11:25:28 -0000, Francisco Gutierrez  
<fgutiers2002@yahoo.com> wrote:

> Dear Group:
>
> I have a function, and I iterate it using fixedpoint. Something of this  
> sort:
>
> FixedPointList[
>  function[arg1,arg2,arg3, #] &, arg4,
>  SameTest -> (Max[Abs[Flatten[#1] - Flatten[#2]]] < 0.01 &)]
>
>
> I would like to know how many steps it takes this function to converge.  
> I have tried with EvaluationMonitor to no avail:
>
> Block[{veamos, c = 0},
>   veamos = FixedPoint[
>     function[arg1,arg2,arg3, #] &, arg4,
>     SameTest -> (Max[Abs[Flatten[#1] - Flatten[#2]]]< 0.01 &)];
>   EvaluationMonitor :> c++; {veamos, c}]
>
>
> The iterator c simply does not move, and the previous function works ok  
> but returns the value of c as 0.
>
> I tried if this was true with Length[FixedPointList[...]] and the answer  
> was 20 (which should be the value of c in the immediately previous  
> function). In principle, this would solve my problem, but it seems
> rather inefficient, especially when the convergence criterion is severe  
> (not 0.01, but say 10^-4).
>
> Is there an efficient and nice way to solve this?
>
> Thanks,
>
> Francisco

FixedPoint doesn't accept the option EvaluationMonitor, so this approach  
is a dead end. You could insert your counter into the SameTest; the number  
of comparisons will be one more than the number required to achieve  
convergence. However, I don't think this is likely to gain you very much;  
Length@FixedPointList[...] will not incur a noticeable efficiency penalty  
in most cases as building up a list of values is a fairly lightweight  
operation. Another approach, of course, would be to use a While loop.

In regard to your SameTest: you may need to be careful with comparisons of  
this sort where the absolute function values are large and the tolerance  
is small since, due to the properties of floating-point numbers, the  
relative error in this case may become arbitrarily small without the  
absolute error necessarily falling below the value required for  
convergence. Assuming that you're working in machine precision, you may be  
better off using something along the lines of

SameTest -> (val = Max[Abs[Flatten[#1] - Flatten[#2]]]; val < tol +  
val*Sqrt[$MachineEpsilon] &)

where tol is your chosen tolerance.

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


#6134

From"Dr. Wolfgang Hintze" <weh@snafu.de>
Date2011-11-03 08:47 +0000
Message-ID<j8tkeh$eta$1@smc.vnet.net>
In reply to#6112
Why not use the Reap, Sow mechanism described in the Help file?

(* 1 *)

Reap[FindRoot[Cos[x] == x, {x, 0},
EvaluationMonitor :> Sow[x]]]

{{x -> 0.7390851332151607},
{{0., 1., 0.7503638678402439, 0.7391128909113617,
0.739085133385284, 0.7390851332151607}}}


You can also combine your idea with this

(* 2 *)

c = 0;
Reap[FindRoot[Cos[x] == x, {x, 0},
StepMonitor :> Sow[x, c++]]]
c

{{x -> 0.7390851332151607},
{{1.}, {0.7503638678402439}, {0.7391128909113617},
{0.739085133385284}, {0.7390851332151607}}}
Out[35]=
5

Or, using only c

(* 3 *)

c = 0;
Reap[FindRoot[Cos[x] == x, {x, 0},
StepMonitor :> Sow[c++]]]
c
Out[36]=
{{x -> 0.7390851332151607}, {{0, 1, 2, 3, 4}}}
Out[37]=
5

Regards,
Wolfgang


"Francisco Gutierrez" <fgutiers2002@yahoo.com> schrieb im Newsbeitrag 
news:j8r9b8$3ie$1@smc.vnet.net...
> Dear Group:
>
> I have a function, and I iterate it using fixedpoint. Something of 
> this sort:
>
> FixedPointList[
> function[arg1,arg2,arg3, #] &, arg4,
> SameTest -> (Max[Abs[Flatten[#1] - Flatten[#2]]] < 0.01 &)]
>
>
> I would like to know how many steps it takes this function to 
> converge. I have tried with EvaluationMonitor to no avail:
>
> Block[{veamos, c = 0},
>  veamos = FixedPoint[
>    function[arg1,arg2,arg3, #] &, arg4,
>    SameTest -> (Max[Abs[Flatten[#1] - Flatten[#2]]]< 0.01 &)];
>  EvaluationMonitor :> c++; {veamos, c}]
>
>
> The iterator c simply does not move, and the previous function works 
> ok but returns the value of c as 0.
>
> I tried if this was true with Length[FixedPointList[...]] and the 
> answer was 20 (which should be the value of c in the immediately 
> previous function). In principle, this would solve my problem, but it 
> seems
> rather inefficient, especially when the convergence criterion is 
> severe (not 0.01, but say 10^-4).
>
> Is there an efficient and nice way to solve this?
>
> Thanks,
>
> Francisco 

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


#6153

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2011-11-03 08:57 +0000
Message-ID<j8tl13$f34$1@smc.vnet.net>
In reply to#6112
On 2011.11.02. 12:25, Francisco Gutierrez wrote:
> Dear Group:
>
> I have a function, and I iterate it using fixedpoint. Something of this sort:
>
> FixedPointList[
>   function[arg1,arg2,arg3, #]&, arg4,
>   SameTest ->  (Max[Abs[Flatten[#1] - Flatten[#2]]]<  0.01&)]
>
>
> I would like to know how many steps it takes this function to converge. I have tried with EvaluationMonitor to no avail:
>
> Block[{veamos, c = 0},
>    veamos = FixedPoint[
>      function[arg1,arg2,arg3, #]&, arg4,
>      SameTest ->  (Max[Abs[Flatten[#1] - Flatten[#2]]]<  0.01&)];
>    EvaluationMonitor :>  c++; {veamos, c}]
>
>
> The iterator c simply does not move, and the previous function works ok but returns the value of c as 0.
>
> I tried if this was true with Length[FixedPointList[...]] and the answer was 20 (which should be the value of c in the immediately previous function). In principle, this would solve my problem, but it seems
> rather inefficient, especially when the convergence criterion is severe (not 0.01, but say 10^-4).
>
> Is there an efficient and nice way to solve this?
>

Neither FixedPoint, not Block take the option EvaluationMonitor.  Also, 
the option was included incorrectly in Block.  Options must be separated 
by a colon, not semicolon.

I seriously doubt that FixedPointList would be measurably slower than 
FixedPoint (have you measured it?), but if you really want to avoid 
FixedPointList, you can use

Module[{result, c = 0},
   result = FixedPoint[(c++; f[#])&, value];
   {result, c}
]

I'd recommend {Last[#], Length[#]}& @ FixedPointList[ ... ] though.

I hope this helps.

References:

http://reference.wolfram.com/mathematica/ref/CompoundExpression.html
http://reference.wolfram.com/mathematica/ref/Block.html

--
Szabolcs Horvát
Mathematica on SO: http://stackoverflow.com/questions/tagged/mathematica

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


#6209

FromFrancisco Gutierrez <fgutiers2002@yahoo.com>
Date2011-11-05 09:47 +0000
Message-ID<j930o7$a5u$1@smc.vnet.net>
In reply to#6112
Many thanks to Szabolcs, Wolfgang, and Oleksandr for their valuable answers to my query. All very clear, and worked well.
Best,
Francisco


________________________________
From: Oleksandr Rasputinov <oleksandr_rasputinov@hmamail.com>
Sent: Thursday, November 3, 2011 3:43 AM
Subject: Re: iteration question

On Wed, 02 Nov 2011 11:25:28 -0000, Francisco Gutierrez  
<fgutiers2002@yahoo.com> wrote:

> Dear Group:
>
> I have a function, and I iterate it using fixedpoint. Something of this sort:
>
> FixedPointList[
>  function[arg1,arg2,arg3, #] &, arg4,
>  SameTest -> (Max[Abs[Flatten[#1] - Flatten[#2]]] < 0.01 &)]
>
>
> I would like to know how many steps it takes this function to converge. 
> I have tried with EvaluationMonitor to no avail:
>
> Block[{veamos, c = 0},
>   veamos = FixedPoint[
>     function[arg1,arg2,arg3, #] &, arg4,
>     SameTest -> (Max[Abs[Flatten[#1] - Flatten[#2]]]< 0.01 &)];
>   EvaluationMonitor :> c++; {veamos, c}]
>
>
> The iterator c simply does not move, and the previous function works ok 
> but returns the value of c as 0.
>
> I tried if this was true with Length[FixedPointList[...]] and the answer 
> was 20 (which should be the value of c in the immediately previous 
> function). In principle, this would solve my problem, but it seems
> rather inefficient, especially when the convergence criterion is severe 
> (not 0.01, but say 10^-4).
>
> Is there an efficient and nice way to solve this?
>
> Thanks,
>
> Francisco

FixedPoint doesn't accept the option EvaluationMonitor, so this approach 
is a dead end. You could insert your counter into the SameTest; the number 
of comparisons will be one more than the number required to achieve 
convergence. However, I don't think this is likely to gain you very much; 
Length@FixedPointList[...] will not incur a noticeable efficiency penalty 
in most cases as building up a list of values is a fairly lightweight 
operation. Another approach, of course, would be to use a While loop.

In regard to your SameTest: you may need to be careful with comparisons of 
this sort where the absolute function values are large and the tolerance 
is small since, due to the properties of floating-point numbers, the 
relative error in this case may become arbitrarily small without the 
absolute error necessarily falling below the value required for 
convergence. Assuming that you're working in machine precision, you may be 
better off using something along the lines of

SameTest -> (val = Max[Abs[Flatten[#1] - Flatten[#2]]]; val < tol + 
val*Sqrt[$MachineEpsilon] &)

where tol is your chosen tolerance.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web