3
$\begingroup$

I have a problem on my homework with Lagrangian multipliers that Mathematica has now been bashing at fruitlessly for 10 minutes. I'm not sure whether I did something horribly wrong codewise, but it's definitely a big ugly function that it is trying to optimize with constraints.

Here's the problem: I need to maximize f with regards to c and n if v is held constant at 20000.

p = 15 n (10 - c) - c^2;
v = 0.5 p + n*Sqrt[p];
f = c*p - v;

As you can probably tell, f, when properly expanded, is huge. Its derivatives are even uglier, making maximization absolutely nasty. I tried having Mathematica solve Lagrangian Multipliers on its own, so that with values of c and n I could just plug in to see what f's max was:

g1 = D[v, c]
g2 = D[v, n]
f1 = D[f, c]
f2 = D[f, n]
Solve[f1 == lam*g1 && f2 == lam*g2 && v == 20000, {c, n}]

It's been sitting for 25 minutes. 5 minutes later, I ssh'd to another machine and started this:

Maximize[{f, v == 20000}, {c, n}]

And that one has been going for 15 now. Is there something I can do to make this go faster? I'm running on a machine with 4 cores, 8 gigs of ram and an i7, it should be able to handle this. I have a lot of problems like this, and don't have time to let it go overnight :(

$\endgroup$

1 Answer 1

4
$\begingroup$

Update: I forgot to mention that first I changed the coefficients to exact numbers:

p = 15 n (10 - c) - c^2;
v = (1/2) p + n*Sqrt[p];
f = c*p - v;

Then I did what I posted originally:

The following returns an answer in less than 0.1 sec.:

Solve[f1 == lam*g1 && f2 == lam*g2 && v == 20000, {c, n, lam}, Method -> Reduce]

If I add lam to Maximize, I get this disappointing result:

Maximize[{f, v == 20000}, {c, n, lam}]

Maximize::natt: The maximum is not attained at any point satisfying the given constraints. >>

{∞, {c -> Indeterminate, n -> Indeterminate, lam -> Indeterminate}}
$\endgroup$
4
  • $\begingroup$ Huh. Looks like I forgot to solve for lam, but what does Method->Reduce do? $\endgroup$
    – laudiacay
    Oct 28, 2015 at 4:20
  • $\begingroup$ ClearAll[p, f, v, lam, g1, g2, f1, f2, c, n, "Global*`"] p = 15 n (10 - c) - c^2; v = 0.5 p + n*Sqrt[p]; f = c*p - v; g1 = D[v, c] g2 = D[v, n] f1 = D[f, c] f2 = D[f, n] Solve[f1 == lam*g1 && f2 == lam*g2 && v == 20000, {c, n, lam}, Method -> Reduce] This is my exact, complete code now, it's still just sitting there chillin' again. What could be up? @michael-e2 $\endgroup$
    – laudiacay
    Oct 28, 2015 at 4:23
  • 1
    $\begingroup$ @laudiacay Try it now. I forgot something. -- Method -> Reduce is in the docs. It makes Solve use Reduce to reduce the system. Reduce is more picky and produces a system equivalent to the given one; Solve by default looks for solutions that are generically valid. Often it takes longer and produces more complete results. Here "picky" turns out to be faster. $\endgroup$
    – Michael E2
    Oct 28, 2015 at 9:28
  • $\begingroup$ Wow, you learn something new every day. Thank god I only have to find real numerical solutions for now. $\endgroup$
    – laudiacay
    Oct 28, 2015 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.