ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 12 Sep 2019 21:48:22 +0200Optimization under constrainthttps://ask.sagemath.org/question/47851/optimization-under-constraint/Here is an incredible powerfull solution of a constrained optimization that Mathematica cannot solve without help
var('A, x, y, l, alpha, beta, R, p_x, p_y');
U= A*x^(alpha)*y^(beta);
show(U)
D = p_x*x + p_y*y;
show(D)
show(U)
solve(D==R, y)
L = U-l*(D-R)
show(L)
L_x= L.diff(x)
show(L_x)
L_y= L.diff(y)
show(L_y)
L_lambda= L.diff(l)
show(L_l)
z=solve([L_x==0, L_y==0, L_l==0,], x, y, l)
Now I have two questions in one :
1. I would use greek lambda in place of l ?
2. I would retreive the values of x and y to put it in U ? I have tried some solutions as z[0 : 1 :...] but it doesnt work ? How can I do it ?Thu, 12 Sep 2019 19:05:30 +0200https://ask.sagemath.org/question/47851/optimization-under-constraint/Comment by tmonteil for <p>Here is an incredible powerfull solution of a constrained optimization that Mathematica cannot solve without help</p>
<pre><code>var('A, x, y, l, alpha, beta, R, p_x, p_y');
U= A*x^(alpha)*y^(beta);
show(U)
D = p_x*x + p_y*y;
show(D)
show(U)
solve(D==R, y)
L = U-l*(D-R)
show(L)
L_x= L.diff(x)
show(L_x)
L_y= L.diff(y)
show(L_y)
L_lambda= L.diff(l)
show(L_l)
z=solve([L_x==0, L_y==0, L_l==0,], x, y, l)
</code></pre>
<p>Now I have two questions in one :</p>
<ol>
<li><p>I would use greek lambda in place of l ?</p></li>
<li><p>I would retreive the values of x and y to put it in U ? I have tried some solutions as z[0 : 1 :...] but it doesnt work ? How can I do it ?</p></li>
</ol>
https://ask.sagemath.org/question/47851/optimization-under-constraint/?comment=47859#post-id-47859I bet `L_l` (which is not defined) stands for `L_lambda`.Thu, 12 Sep 2019 21:48:22 +0200https://ask.sagemath.org/question/47851/optimization-under-constraint/?comment=47859#post-id-47859Answer by tmonteil for <p>Here is an incredible powerfull solution of a constrained optimization that Mathematica cannot solve without help</p>
<pre><code>var('A, x, y, l, alpha, beta, R, p_x, p_y');
U= A*x^(alpha)*y^(beta);
show(U)
D = p_x*x + p_y*y;
show(D)
show(U)
solve(D==R, y)
L = U-l*(D-R)
show(L)
L_x= L.diff(x)
show(L_x)
L_y= L.diff(y)
show(L_y)
L_lambda= L.diff(l)
show(L_l)
z=solve([L_x==0, L_y==0, L_l==0,], x, y, l)
</code></pre>
<p>Now I have two questions in one :</p>
<ol>
<li><p>I would use greek lambda in place of l ?</p></li>
<li><p>I would retreive the values of x and y to put it in U ? I have tried some solutions as z[0 : 1 :...] but it doesnt work ? How can I do it ?</p></li>
</ol>
https://ask.sagemath.org/question/47851/optimization-under-constraint/?answer=47858#post-id-47858Regarding 1., it is not possible to use unicode λ in variables names, since Sage relies on Python 2, which does not allow it, so you have to wait the migration of Sage to Python 3 for that.
Regarding 2, you can use the `solution_dict=True` of solve :
sage: z=solve([L_x==0, L_y==0, L_l==0,], x, y, l, solution_dict=True)
sage: z
[{l: (A*alpha + A*beta)*(R*alpha/((alpha + beta)*p_x))^alpha*(R*beta/((alpha + beta)*p_y))^beta/R,
y: R*beta/((alpha + beta)*p_y),
x: R*alpha/((alpha + beta)*p_x)}]
sage: z[0]
{l: (A*alpha + A*beta)*(R*alpha/((alpha + beta)*p_x))^alpha*(R*beta/((alpha + beta)*p_y))^beta/R,
y: R*beta/((alpha + beta)*p_y),
x: R*alpha/((alpha + beta)*p_x)}
sage: z[0][x]
R*alpha/((alpha + beta)*p_x)
sage: z[0][y]
R*beta/((alpha + beta)*p_y)
You can pass a dictionary for substituting:
sage: U.subs(z[0])
A*(R*alpha/((alpha + beta)*p_x))^alpha*(R*beta/((alpha + beta)*p_y))^betaThu, 12 Sep 2019 21:47:25 +0200https://ask.sagemath.org/question/47851/optimization-under-constraint/?answer=47858#post-id-47858