2
$\begingroup$

I want to have a function that gets generic domain and function and does maximum on this domain. For this I tried the following straight-forward code:

aa[xa_, ya_] := xa + ya
dom = ImplicitRegion[-3 <= a <= 5 && -10 <= b <= 10, {a, b}]
Module[{mm}, NMaximize[ aa @@ mm, mm \[Element]  dom]]

It doesn't work

NMaximize::objfs: The objective function {Subscript[mm$86344, 1],Subscript[mm$86344, 2]} should be scalar-valued.

If I do {x,y} \in dom etc then it does work. But I prefer to avoid it.

$\endgroup$

1 Answer 1

3
$\begingroup$
$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

aa[{xa_, ya_}] := xa + ya
dom = ImplicitRegion[-3 <= a <= 5 && -10 <= b <= 10, {a, b}];
Module[{mm}, NMaximize[aa@mm, mm ∈ dom]]

(* {15., {mm$4229 -> {5., 10.}}} *)

Or with Maximize

Module[{mm}, Maximize[aa@mm, mm ∈ dom]]

(* {15, {mm$4320 -> {5, 10}}} *)

EDIT: Re your comment below

aa2[x_List] := x[[1]] + x[[2]]

Module[{mm}, Maximize[aa2@mm, mm ∈ dom]]

(* {15, {mm$6507 -> {5, 10}}} *)
$\endgroup$
1
  • $\begingroup$ Thanks. It works! Why I couldn't access the members any other way. For example aa[x_]:=x[[1]]* x[[2]] or using subscripts (like it suggested) $\endgroup$ Commented Aug 18, 2023 at 18:14

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.