Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: FindRoot

2 views
Skip to first unread message

Chris Chiasson

unread,
Oct 15, 2006, 12:32:21 AM10/15/06
to
Putting a Print statement there to print x at each evaluation is like
using something in System`Dump` or Experimental`. It might work in one
version, but change in another.

There are even some things that are documented to work in Mathematica
that don't (four argument form of Infix anyone?). Why expect the
undocumented things to work across versions? :-]

And yes, MathGroupers, I am going to keep mentioning the four argument
form of Infix until someone shows me how to use it or I get tired.

On 10/14/06, dimm...@yahoo.com <dimm...@yahoo.com> wrote:
> Hello.
>
> Let me deal with something elementary but yet confusing for me.
>
> Consider the equation cos(x)=x.
>
> FindRoot[Cos[x] == x, {x, 5}]
> {x -> 0.7390851332151607}
>
> This prints the value of x every time a step is taken.
>
> FindRoot[Cos[x] == x, {x, 5}, StepMonitor :> Print[x]]
>
> This gives a list of the steps taken.
>
> Reap[FindRoot[Cos[x] == x, {x, 5}, StepMonitor :> Sow[x]]]
> {{x -> 0.7390851332151607}, {{-1., -0.02837830412204312,
> 1.0296174587519653, 0.7525886779802748, 0.7391248287000711,
> 0.7390851335630761, 0.7390851332151607}}}
>
> This counts the steps.
>
> Block[{st = 0}, {FindRoot[Cos[x] == x, {x, 5}, StepMonitor :> st++],
> st}]
> {{x -> 0.7390851332151607}, 7}
>
> I want to know what exactly the following command gives.
>
> Reap[FindRoot[Cos[x] == x, {x, 5}, EvaluationMonitor :> Sow[x]]]
> {{x -> 0.7390851332151607}, {{5., -54.99999999999999, -1.,
> 8.716216958779569,
> -0.02837830412204312, 1.0296174587519653, 0.7525886779802748,
> 0.7391248287000711, 0.7390851335630761, 0.7390851332151607}}}
>
> In versions earlier than 5.0 you could use the following command
>
> FindRoot[Print[x]; Cos[x] == x, {x, 5}]
>
> Why it cannot be used now?
>
> Thanks in advance for any help.
>
>


--
http://chris.chiasson.name/

Andrzej Kozlowski

unread,
Oct 15, 2006, 12:51:37 AM10/15/06
to


If you want you can still do it in the old way:

First evaluate:

Developer`SetSystemOptions["EvaluateNumericalFunctionArgument" ->
False];

and then

FindRoot[(Print[x]; Cos[x]) == x, {x, 5}]
will work.
But note the parentheses! It won't work without them.
However, the approach using Sow and Reap is vastly more useful since
you can't manipulate data returned by Print statements (well, at
least not without changing the value of $Output and some extra
programming). I am not sure what kind of answer you expected to your
question " what exactly the following command gives". It gives
exactly the same list of values (of successive approximations to the
root of the equation tried by FindRoot) as your Print method gives,
but in a much more convenient form. Does this answer your question?

Andrzej Kozlowski

Andrzej Kozlowski

unread,
Oct 16, 2006, 2:59:40 AM10/16/06
to
O.K, it seems I missed your point, which I now think concerned the
difference between StepMonitor and EvaluationMonitor (whereas I
originally thought you were concerned with the difference between
using Sow-Reap and Print).
In general EvaluationMonitor will produce more values than
StepMonitor, because it records each time the specified numerical
function is evaluated. Sometimes to make one step in an iterative
procedure you need to evaluate the function several times at
different points (the standard example is when you need to compute a
numerical derivative using finite differences) and all these
computations will be part of one step (so will not be recorded by
StepMonitor) but will be recored by EvaluationMonitor.

Andrzej


On 15 Oct 2006, at 03:45, dimitris anagnostou wrote:

> Dear Andrzej,
>
> I really appreciate your response.
>
> By " what exactly the following command gives?" I mean
> from mathematical point of view what the command


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

> gives.
>
> For example this gives a list of the steps taken


>
> Reap[FindRoot[Cos[x] == x, {x, 5}, StepMonitor :> Sow[x]]]
>
>

> Andrzej Kozlowski <ak...@mimuw.edu.pl> wrote:
> (tm) Pro*

> All-new Yahoo! Mail - Fire up a more powerful email and get things
> done faster.

dimm...@yahoo.com

unread,
Oct 17, 2006, 3:14:17 AM10/17/06
to
Thanks to Chriss and Andrzej for their responses.
0 new messages