# Getting result from Maximize

I'm trying to get maximization result from Maximize function to use it in later calculations. At the moment I use the following code:

c := 4
p[q1_, q2_] := 12 - q1 - q2
pi[p_, q_] := p*q - c*q
q2optimal := Maximize[pi[p[q1, q2], q2], q2][[2]][[1]][[2]]
q2optimal
poptimal := Simplify[p[q1, q2optimal]]
poptimal


Is there any other way to get the same result without that ugly [[2]][[1]][[2]]?

Yes, for one, you can put all the accessors into one [[..]]:

Maximize[pi[p[q1, q2], q2], q2][[2, 1, 2]]


The second way is to use the rule -> in the last part the result directly

q2 /. Last@Maximize[pi[p[q1, q2], q2], q2]


The result from functions like Maximize, NMaximize, Minimize or even Solve might look strange for a beginner, but rules and replacement are one of the core concepts of Mathematica. Therefore, you should read this tutorial to get used to it.

Btw, is there a particular reason why you use := in your simple assignments?

• Thank for the explanation! I use := because it does what I need - it defines a function. – user4510038 Feb 23 '15 at 16:53
• And thanks for the link to tutorial. I definitely need it. :( – user4510038 Feb 23 '15 at 16:57
• @user4510038 Just make sure you are aware of the difference between = and :=. For functions like p it is (almost always) the right way, for variables like c you might experience unexpected behaviour. – halirutan Feb 23 '15 at 17:02