Re: goto & Findmininimum
- To: mathgroup at smc.vnet.net
- Subject: [mg20922] Re: [mg20914] goto & Findmininimum
- From: Hartmut Wolf <hwolf at debis.com>
- Date: Wed, 1 Dec 1999 01:49:53 -0500 (EST)
- Organization: debis Systemhaus
- References: <199911212347.SAA08984@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Albert Maydeu-Olivares schrieb:
>
> Greetings,
>
> I have a Do loop of the type
>
> Do[
> Label[begin]
> Generate Data
> FindMinimum
> Do something to the result, {times}]
>
> When FindMinimum does not converge in x iterations it gives the message
>
> FindMinimum::"fmlim": "The minimum could not be bracketed in x iterations."
>
> How do I get Mathematica to transfer control to "begin" using Goto when
> FindMininumn does not converge?
>
Dear Albert,
you may proceed along these lines
Do[
Generate Data;
Check[FindMinimum, Continue[]];
Do something to the result,
{times}]
(no Label), so e.g. try
Do[x = i^2; y = Sin[i Pi/2];
Print[Check[x/y, Continue[]]],
{i, 1, 5}]
you will get reasonable answers for i=1,3,5 and error massage for i=2,4.
In case you don't want to have your output cluttered with the error
messages (and you know very well, when your calculations won't succeed),
you may use Carl K.Woll's <mailto:carlw at fermi.phys.washington.edu>
SilentCheck, which he published recently. For your convenience I copy it
here:
---------------------------
SetAttributes[SilentCheck, {HoldAll}]
SilentCheck::usage =
"SilentCheck[expr,failexpr] evaluates expr, and returns the result,
\
unless messages were generated, in which case it evaluates and returns \
failexpr. SilentCheck suppresses the output of the messages generated in
\
evaluating expr.";
SilentCheck[expr_, err_] := Module[{ans, flag}, Unprotect[Message];
_Message := Throw[flag, SilentCheck];
ans = Catch[expr, SilentCheck];
Clear[Message];
Protect[Message];
If[ans === flag, err, ans]]
---------------------------
So then
In[15]:=
Do[x = i^2; y = Sin[i Pi/2];
x=SilentCheck[x/y, Continue[]];
Print[x],
{i, 1, 5}]
Kind regards, Hartmut