MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: NMinimize problem: fct minimized uses FindRoot

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123585] Re: NMinimize problem: fct minimized uses FindRoot
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Mon, 12 Dec 2011 06:43:06 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201112101227.HAA19219@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

Andrzej answered far better than I would have, but in general...

I avoid Return, GoTo, Label, Continue, and Break because they encourage  
Fortran-like "spaghetti code". I suspect they ALL have scoping issues,  
too, as Andrzej pointed out for Return.

In tutorial/LoopsAndControlStructures, Help says, "In general, use of Goto  
reduces the degree of structure that can readily be perceived in a  
program, and therefore makes the operation of the program more difficult  
to understand."

That's putting it mildly,  think.

Bobby

On Sun, 11 Dec 2011 05:21:24 -0600, Andrzej Kozlowski <akoz at mimuw.edu.pl>  
wrote:

> It does not do in Mathematica what you think it does. In cases like  
> yours it is harmless enough for it does nothing at all! At the end of a  
> function call Return[x] is exactly the same as x, so you are only  
> deluding yourself by using it.
>
> The only real use Of Return is to exit control structures and loops but,  
> if not used properly it will cause problems. For example, this will not  
> work:
>
> memberQ[x_List, y_] := (Do[If[i == y, Return[True]], {i, x}]; False)
>
> memberQ[{a, b, c}, b]
>
> False
>
> What you actually need to do is:
>
> Clear[memberQ]
>
> memberQ[x_List, y_] := (Do[If[i == y, Return[Return[True]]], {i, x}];
> False)
>
> memberQ[{a, b, c}, b]
>
> True
>
> With a multiple Do loop (but not with, for example, a While loop) you  
> will need multiple returns to exit the entire looping structure. The  
> only use of Return I can imagine is precisely when you wish to exit a  
> single loop inside multiple looping structure (without exiting the  
> enclosing ones). In all other cases Throw and Catch are much preferable.
> Using Return as one does in C and Fortran in Mathematica is like using  
> French when speaking English - you may well be understood but you are  
> deluding yourself if you think you are speaking English.
>
> Andrzej Kozlowski
>
>
>
>
>
>
>
>
>
> On 11 Dec 2011, at 09:47, Doug Tinkham wrote:
>
>> Thanks Bobby.
>>
>> The function I showed with Sin and Cos is meaningless; it was just a
>> function that reproduced the recursion limit problem I was having. The
>> actual function would be several pages long, even after simplification.
>>
>> Why never use Return?  As a C++ programmer, it is a habit.  I can't find
>> information saying why one should not. Is there a performance penalty?
>>
>> Thanks.
>>
>> --------------------------------------------------
>> From: "DrMajorBob"
>>
>>> 1) Never, ever, EVER use Return. In this case, there wasn't even a  
>>> flimsy
>>> excuse for it.
>>>
>>> 2) To prevent computing a function with symbolic arguments, use a  
>>> pattern
>>> on the LHS such as _?NumericQ.
>>>
>>> 3) You used invalid syntax in the second argument of NMinimize.
>>>
>>> 4) Use Set, not SetDelayed, whenever possible.
>>>
>>> Clear[numFct]
>>> numFct[optvar_?NumericQ] :=
>>> Module[{inteq, n, x}, inteq[x_] = (Sin[x] + 1/2*Cos[x])/optvar;
>>> Sin[optvar] + n /. FindRoot[inteq[n], {n, 0.1}]]
>>>
>>> NMinimize[numFct[var], {var, 0, 6}]
>>>
>>> {-1.46365, {var -> 4.71239}}
>>>
>>> 5) Finding a root for (Sin[x] + 1/2*Cos[x])/optvar is the same as  
>>> finding
>>> a root for Sin[x] + 1/2*Cos[x]:
>>>
>>> Clear[numFct]
>>> numFct[optvar_?NumericQ] :=
>>> Module[{inteq, n, x}, inteq[x_] = Sin[x] + 1/2*Cos[x];
>>> Sin[optvar] + n /. FindRoot[inteq[n], {n, 0.1}]]
>>>
>>> NMinimize[numFct[var], {var, 0, 6}]
>>>
>>> {-1.46365, {var -> 4.71239}}
>>>
>>> 6) Don't define functions you don't need:
>>>
>>> Clear[numFct]
>>> numFct[optvar_?NumericQ] := Module[{x},
>>> Sin[optvar] + x /. FindRoot[Sin[x] + Cos[x]/2, {x, 0.1}]]
>>> NMinimize[numFct[var], {var, 0, 6}]
>>>
>>> {-1.46365, {var -> 4.71239}}
>>>
>>> 7) Optimization and root-finding are uncoupled in this case, so:
>>>
>>> Clear[numFct]
>>> numFct[optvar_?NumericQ] := Sin[optvar] + Module[{x},
>>> x /. FindRoot[Sin[x] + Cos[x]/2, {x, 0.1}]]
>>> NMinimize[numFct[var], {var, 0, 6}]
>>>
>>> {-1.46365, {var -> 4.71239}}
>>>
>>> or
>>>
>>> Clear[numFct, x, y]
>>> numFct[x_?NumericQ] = Sin[x] +
>>> y /. FindRoot[Sin[y] + Cos[y]/2, {y, 0.1}];
>>> NMinimize[numFct[x], {x, 0, 6}]
>>>
>>> {-1.46365, {x -> 4.71239}}
>>>
>>> or even simpler:
>>>
>>> Clear[x]
>>> NMinimize[Sin[x], {x, 0, 6}]
>>> First@% + x /. FindRoot[Sin[x] + Cos[x]/2, {x, 0.1}]
>>>
>>> {-1., {x -> 4.71239}}
>>>
>>> -1.46365
>>>
>>> Bobby
>>>
>>> On Sat, 10 Dec 2011 06:27:05 -0600, Doug Tinkham <dtinkham at live.ca>  
>>> wrote:
>>>
>>>> Hello
>>>>
>>>> I'm using NMinimize and FindMinimum to minimize a function that uses
>>>> FindRoot when calculating it's value. The problem is that the equation
>>>> that FindRoot is used on uses the variable that is being optimized,  
>>>> and
>>>> Mathematica appears to be forcing the variable that is being optimized
>>>> to remain symbolic in the FindRoot call, and this leads to recursion  
>>>> and
>>>> a recursion limit error.
>>>>
>>>> Rather than post my actual functions that are quite long, I've reduced
>>>> my problem to the code below that shows my issue. As you will see,
>>>> FindRoot keeps optvar in symbolic form when executing FindRoot. Is  
>>>> there
>>>> a way to force Mathematica to use all numerical calculations using
>>>> NMinimize or FindMinimum?  Is the issue with calculation of the
>>>> gradient, which Mathematica wants to do symbolically?
>>>>
>>>> Many thanks.
>>>>
>>>>
>>>>
>>>> MyNumFct[optvar_] := Module[{inteq, n},
>>>> inteq[x_] := (Sin[x] + 1/2*Cos[x])/optvar;
>>>> n = n /. FindRoot[inteq[n], {n, 0.1}];
>>>> Return[n + Sin[optvar]];
>>>> ]
>>>> NMinimize[{MyNumFct[var], 0 <= var <= 6}, {var, 4.1}]
>>>>
>>>>
>>>
>>>
>>> --
>>> DrMajorBob at yahoo.com
>>>
>>
>


-- 
DrMajorBob at yahoo.com



  • Prev by Date: Re: NMinimize problem: fct minimized uses FindRoot
  • Next by Date: Re: Change x-axis scale
  • Previous by thread: Re: NMinimize problem: fct minimized uses FindRoot
  • Next by thread: Re: NMinimize problem: fct minimized uses FindRoot