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
Module[{w,x}, ...]
scoping construct .... $\endgroup$y[x_, w_] := x^2 - w^2; fTest[f_] := NMinimize[{f[x, w], 1 >= x >= 0, 1 >= w >= 0}, {x, w}];fTest[y]
$\endgroup${x,w}
vars from theModule[{xxx} ,]
part. That is the easiest solution $\endgroup$