Re: iteration question
- To: mathgroup at smc.vnet.net
- Subject: [mg122601] Re: iteration question
- From: "Dr. Wolfgang Hintze" <weh at snafu.de>
- Date: Thu, 3 Nov 2011 03:44:20 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j8r9b8$3ie$1@smc.vnet.net>
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 at yahoo.com> schrieb im Newsbeitrag
news:j8r9b8$3ie$1 at 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