9
$\begingroup$

Why does the following not compute?

MaxValue[-y^2, y ∈ Interval[{-1, 1}]]

I expect it to compute the maximum value of the funtion over the interval from -1 to 1 but I instead get the error:

MaxValue::objfs: The objective function {-Subscript[y, 1]^2} should be scalar-valued.

What should I be using instead?

$\endgroup$
0

1 Answer 1

14
$\begingroup$

Update: Carl gave the best solution in a comment:

Could use {y} ∈ Interval[{-1, 1}] instead. – Carl Woll


If you use the region notation, the variable is considered to be a vector. 1D region implies a 1D vector, but still a vector, not a scalar.

The following all work:

Explicitly take the first component of the vector. Since this is symbolic computation, we must use Indexed:

MaxValue[-Indexed[y, 1]^2, y ∈ Interval[{-1, 1}]]
(* 0 *)

Use a vector operator (Dot) to compute the same thing:

MaxValue[-y.y, y ∈ Interval[{-1, 1}]]
(* 0 *)

Instead of regions, use constraints:

MaxValue[{-y^2, -1 <= y <= 1}, y]
(* 0 *)
$\endgroup$
1
  • 6
    $\begingroup$ Could use {y} ∈ Interval[{-1, 1}] instead. $\endgroup$
    – Carl Woll
    Commented Jan 13, 2020 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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