1
$\begingroup$

I am unable to maximize a function containing an If statement and a vector variable. To give an MWE (not my original problem), consider the following:

x = {x1, x2};
Maximize[If[x[[2]] == 1, 1, -x[[1]]^2], x]

This returns no error, but returns

Maximize[If[x2 == 1, 1, -x[[1]]^2], {x1, x2}]

As you can see, it refuses to convert the second vector element to x1. If I do that manually by entering

x = {x1, x2};
Maximize[If[x[[2]] == 1, 1, -x1^2], x]

then it easily spits out

{1, {x1 -> 33/10, x2 -> 1}}

Can anybody offer me some insight into what's going on here? Is there a way to tweak it that doesn't involve manual rewriting? (If I have to type out all vector operations, it's going to be a long ugly formula...)

$\endgroup$
1
$\begingroup$

You'll want to use Piecewise[] to represent your function, Indexed[] to extract coordinates, and FullRegion[2] as the search domain. Thus:

Maximize[Piecewise[{{1, Indexed[x, 2] == 1}}, -Indexed[x, 1]^2], x ∈ FullRegion[2]]
   {1, {x -> {33/10, 1}}}
$\endgroup$
  • $\begingroup$ Thanks! I can't pretend I know what's going on here, but this solves my current problem, and now I can look more into these functions to learn more. :) $\endgroup$ – mimuller Apr 1 '17 at 2:57
  • $\begingroup$ "...now I can look more into these functions to learn more." - yes, that was the intention. :) $\endgroup$ – J. M.'s technical difficulties Apr 1 '17 at 2:58
  • $\begingroup$ (I'm just waiting for the site to allow me to accept your answer.. one more minute :)) $\endgroup$ – mimuller Apr 1 '17 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.