1
$\begingroup$

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]]?

$\endgroup$

1 Answer 1

1
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ Thank for the explanation! I use := because it does what I need - it defines a function. $\endgroup$ Commented Feb 23, 2015 at 16:53
  • $\begingroup$ And thanks for the link to tutorial. I definitely need it. :( $\endgroup$ Commented Feb 23, 2015 at 16:57
  • 1
    $\begingroup$ @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. $\endgroup$
    – halirutan
    Commented Feb 23, 2015 at 17:02

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.