0

I would like to minimize a function, which is supplied as an argument to another function, and report back the estimates of the values of the variables that result in its minimum. In specific, I would like:

y = x^2 - w^2;
fTest[f_] := Module[{w, x}, NMinimize[{f, 1 >= x >= 0, 1 >= w >= 0}, {x, w}]]
fTest[y]

To work the same as:

y = x^2 - w^2;
NMaximize[{y, 1 >= x >= 0, 1 >= w >= 0}, {x, w}]

I think the issue is due to the difference between local and global variables, as I seem to sometimes get the function itself returned, but unevaluated, with the following:

NMinimize[{-w^2 + x^2, 1 >= x$140868 >= 0, 1 >= w$140868 >= 0}, {x$140868, w$140868}]

Suggesting some difference between $w$ and $w\$140868$ for example.

I have seen this answer: Pass function or formula as function parameter and have tried setting the 'HoldAll' attribute of the function itself, but to no avail. Not convinced this is the right approach either!

Does anyone know how I can get around this issue properly?

Note: I could not declare the local variables in my Module declaration, (referring hence to global variables in its body), but I find this a bit messy, and would like a cleaner way.

Best,

Ben

ben18785
  • 3,167
  • 15
  • 28

2 Answers2

1

Not sure if this help or not but you can try it:

fTest[f_, variables_] := 
 Module[variables, NMinimize[{f, 1 >= x >= 0, 1 >= w >= 0}, {x, w}]]

ans=fTest[y, {x, w}]

(*1., {x$8300 -> 0., w$8300 -> 1.}}*)

The easiest way I found is as follows:

ToExpression[StringReplace[ToString[ans], "$" :> "+0*"]]

(*{-1., {x -> 0., w -> 1.}}*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
0

Module is not necessary here, and is in fact the source of the problem:

fTest1[f_] := Module[{w, x}, NMinimize[{f, 1 >= x >= 0, 1 >= w >= 0}, {x, w}]]
fTest2[f_] := NMinimize[{f, 1 >= x >= 0, 1 >= w >= 0}, {x, w}]

fTest1[x^2 - w^2]
(* NMinimize::nnum errors and NMinimize[___] *)
fTest2[x^2 - w^2]
(* {-1., {x -> 0., w -> 1.}} *)

Basically, the Module localizes instances of w and x inside it: the ones in the constraints and the list of variables. However, the instances inside f are not localized, since they are defined outside of the Module! You can see this in the output, where the localized and non-localized forms are co-mingled.

The reason Module is not necessary here is that no definitions are ever associated to x and w. NMinimize essentially does it's own localization of the variables.

2012rcampion
  • 7,851
  • 25
  • 44