0
|
1905 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:
GROUPS:

# How to define piecewise functions of two variables for NMinimize?

Posted 1 year ago
 Hey, I'm new, so sorry if I somehow messed up by posting in the wrong place. Anyway, onto my question: I defined two functions \[Beta][w_, x_] := Piecewise[{{0, w < 150 && x < 150}, {1/ 3, (w < 150 && 150 <= x < 300) || (150 <= w < 300 && x < 150)}, {1/ 2, (w < 150 && x >= 300) || (x < 150 && w >= 300)}, {2/ 3, (150 <= w < 300 && 150 <= x < 300)}, {5/ 6, (150 <= w < 300 && x >= 300 ) || (150 <= x < 300 && w >= 300 )}, {1, w >= 300 && x >= 300}}]  and \[Gamma][w_, x_, y_] := Piecewise[{{0, w < 150 && x < 150 && y < 150}, {1/ 3, (150 <= w < 300 && x < 150 && y < 150) || (150 <= x < 300 && w < 150 && y < 150) || (150 <= y < 300 && w < 150 && x < 150)}, {1/ 2, (w >= 300 && x < 150 && y < 150 ) || (x >= 300 && w < 150 && y < 150 ) || (y >= 300 && x < 150 && w < 150 )}, {2/ 3, (150 <= w < 300 && 150 <= x < 300 && y < 150) || (150 <= w < 300 && 150 <= y < 300 && x < 150) || (150 <= y < 300 && 150 <= x < 300 && w < 150)}, {5/ 6, (w >= 300 && 150 <= x < 300 && y < 150) || (w >= 300 && 150 <= y < 300 && x < 150) || (x >= 300 && 150 <= w < 300 && y < 150) || (x >= 300 && 150 <= y < 300 && w < 150) || (y >= 300 && 150 <= w < 300 && x < 150) || (y >= 300 && 150 <= x < 300 && w < 150)}, {1, (w >= 150 && x >= 150 && y >= 150) || (w < 150 && x >= 300 && y >= 300) || (x < 150 && w >= 300 && y >= 300) || (y < 150 && x >= 300 && w >= 300)}}]  and then use them along with a third piecewise function in one variable \[Alpha][w_] := Piecewise[{{0, w < 150}, {1/3, 150 <= w < 300}, {1/2, w >= 300}}]  The piecewise functions themselves all work, but I get an error when evaluating: NMinimize[{w + x + y + z, w + \[Alpha][w] x + \[Beta][w, x] y + \[Gamma][w, x, y] z >= 1/3 && w \[Element] PositiveIntegers && x \[Element] PositiveIntegers && y \[Element] PositiveIntegers && z \[Element] PositiveIntegers }, {w, x, y, z}]  Can anyone tell me why this doesn't work and if there is some way to do this correctly? Thank you very much!
2 Replies
Sort By:
Posted 1 year ago
 Jesco,I do not really know why your approach using Piecewise is not working. But you could try with interpolated functions instead: alpha = Quiet@FunctionInterpolation[\[Alpha][w], {w, 0, 500}, InterpolationOrder -> 0]; beta = FunctionInterpolation[\[Beta][w, x], {w, 0, 500}, {x, 0, 500}, InterpolationOrder -> 0]; gamma = FunctionInterpolation[\[Gamma][w, x, y], {w, 0, 500}, {x, 0, 500}, {y, 0, 500}, InterpolationOrder -> 0]; And then: NMinimize[{w + x + y + z, w + alpha[w] x + beta[w, x] y + gamma[w, x, y] z >= 1/3 && w \[Element] PositiveIntegers && x \[Element] PositiveIntegers && y \[Element] PositiveIntegers && z \[Element] PositiveIntegers}, {w, x, y, z}] (* Out: {4.,{w\[Rule]1,x\[Rule]1,y\[Rule]1,z\[Rule]1}} *) `Given your definitions this result is not really surprising - well, at least it seems to be correct.Does that help? Regards -- Henrik
Posted 1 year ago
 Hey Henrik,that does help, thank you! I'll have to look into interpolated functions in Mathematica sometime.I actually got it to work by using Minimize instead of NMinimize and simplifying the definitions of beta and gamma by reusing alpha. I was actually interested in a slightly different problem (with another piecewise function on the RHS instead of just 1/3 and some conditions to remove the trivial w=x=y=z=1 solution), but that change didn't pose any problems.Kind regards,Jesco
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments