Re: NMinimize problem: fct minimized uses FindRoot
- To: mathgroup at smc.vnet.net
- Subject: [mg123703] Re: NMinimize problem: fct minimized uses FindRoot
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Fri, 16 Dec 2011 05:45:41 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201112101227.HAA19219@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
> This returning Return[something] rather than just something is not the > result of some sort of confusion in the design Eh. A sow's ear might turn out to be a purse in disguise, but I wouldn't count on it. Bobby On Thu, 15 Dec 2011 03:51:26 -0600, Andrzej Kozlowski <akoz at mimuw.edu.pl> wrote: > > On 14 Dec 2011, at 12:02, Oleksandr Rasputinov wrote: > >> On Tue, 13 Dec 2011 10:41:11 -0000, Andrzej Kozlowski >> <akoz at mimuw.edu.pl> >> wrote: >> >>> >>> On 12 Dec 2011, at 12:43, Oleksandr Rasputinov wrote: >>> >>>> For example, the erroneous usage: >>>> >>>> Block[{a = 1, b = 2}, Return[a]; Return[b]] >>>> >>>> gives: >>>> >>>> Return[1] >>>> >>>> but a correct usage: >>>> >>>> Do[Block[{a = 1, b = 2}, Return[a]; Return[b]], {1}] >>>> >>>> gives (because of the enclosing Do): >>>> >>>> 1 >>> >>> This is not quite the whole story. For example, while the above use of >>> Return in Block does not work, this one (in a function call) does: >>> >>> f[] := Block[{a = 1, b = 2}, Return[a]; Return[b]] >>> >>> f[] >>> >>> 1 >>> >>> Moreover, if you replace the Do loop in your example with a While loop, >>> you will get: >>> >>> n = 1; While[n < 10, Block[{a = 1, b = 2}, Return[a]; Return[b]]] >>> >>> Return[1] >>> >>> All of this is purposeful design and can be explained and justified but >>> I don't think it's worth the bother since Mathematica has far superior >>> means of flow control than this clumsy and arcane construct. >>> >>> Andrzej >>> >> >> Thanks for the additional examples. Yes, there are limitations to what >> Return (or Break, or Continue) will accept as a control flow construct; >> I >> imagine most people would have suspected that Block does not count but >> it >> is a little surprising to find that While doesn't either. > > My view of this is a little different. I think there is a good reason > why the While example returns Return[1] rather than just 1. To see my > point, look at the example on memberQ I posted earlier. With a Do loop > this does not work > > memberQ[x_List, y_] := (Do[If[i === y, Return[True]], {i, x}]; False) > > Now use the While loop: > > Clear[memberQ] > > memberQ[x_List, y_] := (i = 1; > While[i <= Length[x], If[x[[i]] === y, Return[True], i++]]; False) > > memberQ[{a, b, c}, b] > > True > > The While loop works fine. Why is that? Well, I think it is exactly for > the same reason why my earlier While example (when While was not in the > body of a function) "did not work" - i.e. when we got Return[1] instead > of 1. This returning Return[something] rather than just something is not > the result of some sort of confusion in the design (although it is, no > doubt, confusing) but it actually the means by which Return in a While > loop gets through all the control flow structures while in a Do loop it > only exits the innermost control structure. As I pointed out, to achieve > the same result in a Do loop, you will need a double Return: > > Clear[memberQ] > > memberQ[x_List, > y_] := (Do[If[i == y, Return[Return[True]]], {i, x}]; False) > > memberQ[{a, b, c}, b] > > True > > > Andrzej Kozlowski > > -- DrMajorBob at yahoo.com
- References:
- NMinimize problem: fct minimized uses FindRoot
- From: "Doug Tinkham" <dtinkham@live.ca>
- NMinimize problem: fct minimized uses FindRoot