1

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?

halirutan
  • 112,764
  • 7
  • 263
  • 474

1 Answers1

2

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]
halirutan
  • 112,764
  • 7
  • 263
  • 474