2
$\begingroup$

I'm new to the forum and I have problems when I try to run this code. The computation exceeds the limit of my plan.

This is my code. What can I do?

clear[f,g]
f[k_,l_,m_,n_,o_]:=(k^0.05)(l^0.09)(m^0.08)(n^0.43)(o^0.35);
g[k_,l_,m_,n_,o_]:=116.7k+216.7l+183m+31n+833.3o-71500;
Solve[
  D[f[k,l,m,n,o] - lambda g[k,l,m,n,o], {{k,l,m,n,o,lambda}}] == 0, 
  {k,l,m,n,o}, lambda]
$\endgroup$
3
  • $\begingroup$ (1) The logarithm being monotonic, optimizing f is equivalent to optimizing log(f). So use PowerExpand[Log[f[k, l, m, n, o]]]. (2) This might all work better with exact numbers. But if you take that log it is not likely to matter. $\endgroup$ Oct 15, 2020 at 23:18
  • 1
    $\begingroup$ clear should be Clear. This doesn't resolve the problem, though. $\endgroup$
    – xzczd
    Oct 16, 2020 at 6:55
  • $\begingroup$ The lambda in Solve[... , ... , lambda] specifies a domain like Reals or Complex. I think this hardly what you want. $\endgroup$ Oct 16, 2020 at 13:25

2 Answers 2

3
$\begingroup$
Clear["Global`*"]

Rationalize all of the constants

f[k_, l_, m_, n_, o_] = (k^0.05) (l^0.09) (m^0.08) (n^0.43) (o^0.35) // 
   Rationalize;
g[k_, l_, m_, n_, o_] = 
  116.7 k + 216.7 l + 183 m + 31 n + 833.3 o - 71500 // Rationalize;

eqns = Thread[
    D[f[k, l, m, n, o] - 
       lambda g[k, l, m, n, o], {{k, l, m, n, o, lambda}}] == 0] // Simplify;

Include lambda in the list of variables to be found and use the Solve option Method -> Reduce

(sol = Solve[eqns, {k, l, m, n, o, lambda}, Method -> Reduce] // 
    N) // AbsoluteTiming

(* {4.0572, {{k -> 30.6341, l -> 29.6954, m -> 31.2568, n -> 991.774, 
   o -> 30.0312, lambda -> 0.00189561}}} *)

Verifying the solution,

And @@ (eqns /. sol[[1]])

(* True *)
$\endgroup$
1
$\begingroup$

FindRoot works well on this problem:

Clear[f, g]
f[k_, l_, m_, n_, o_] := (k^0.05) (l^0.09) (m^0.08) (n^0.43) (o^0.35);
g[k_, l_, m_, n_, o_] := 116.7 k + 216.7 l + 183 m + 31 n + 833.3 o - 71500;
FindRoot[D[f[k, l, m, n, o] - 
    lambda g[k, l, m, n, o], {{k, l, m, n, o, lambda}}], {#, 1} & /@ {k, l, m, n, o, 
    lambda}] // AbsoluteTiming
(*
{0.0331255, {k -> 30.6341, l -> 29.6954, m -> 31.2568, n -> 991.774, o -> 30.0312, 
  lambda -> 0.00189561}}
 *)
$\endgroup$

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.