1
$\begingroup$

Suppose I have the following maximization problem:

Maximize[{x^2-a,0<=x<=a},{x}]

The result is a function of $a$:

Mathematica graphics

I want to extract this function of $a$. I tried:

mm[a_] := Out[34][[1]]

but this did not work. For example, mm[1] returns:

Mathematica graphics

What is the correct way of defining this function?

$\endgroup$
2
$\begingroup$

There are some things you need to be aware of and I advise to read the most common pitfalls post. Learn the difference between := and = and don't use things like Out in a definition. That definitely bites you at some point.

That being said, please look at the following solution:

{res, val} = Maximize[{x^2 - a, 0 <= x <= a}, {x}];
mm[a_] = res;
mm[4]
$\endgroup$
  • $\begingroup$ I see. So the problem was just that I used := instead of =. $\endgroup$ – Erel Segal-Halevi Jun 21 '18 at 6:48

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.