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.Wed, 21 Oct 2020 21:14:45 +0200find maximum value of Fhttps://ask.sagemath.org/question/53907/find-maximum-value-of-f/ F = (4*x+4*y+9*z-2*w),
G = (2*x+2*y +z+ w)==0,
H = (x^2+y^2+z-4)==0,
* x ,y, z, w is symbolic expressions in 4 variables.
* The symbolic expressions G and H represent constraints.
* find the maximum value of F , subject to the constraints G=0 and H=0 ?????????
* please help meWed, 14 Oct 2020 11:45:51 +0200https://ask.sagemath.org/question/53907/find-maximum-value-of-f/Comment by slelievre for <p>F = (4<em>x+4</em>y+9<em>z-2</em>w),
G = (2<em>x+2</em>y +z+ w)==0,
H = (x^2+y^2+z-4)==0,</p>
<ul>
<li>x ,y, z, w is symbolic expressions in 4 variables.</li>
<li>The symbolic expressions G and H represent constraints.</li>
<li><p>find the maximum value of F , subject to the constraints G=0 and H=0 ?????????</p></li>
<li><p>please help me</p></li>
</ul>
https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53919#post-id-53919Use Lagrange multipliers?Thu, 15 Oct 2020 00:17:18 +0200https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53919#post-id-53919Comment by Emmanuel Charpentier for <p>F = (4<em>x+4</em>y+9<em>z-2</em>w),
G = (2<em>x+2</em>y +z+ w)==0,
H = (x^2+y^2+z-4)==0,</p>
<ul>
<li>x ,y, z, w is symbolic expressions in 4 variables.</li>
<li>The symbolic expressions G and H represent constraints.</li>
<li><p>find the maximum value of F , subject to the constraints G=0 and H=0 ?????????</p></li>
<li><p>please help me</p></li>
</ul>
https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53939#post-id-53939Still homework or not ?
Hint : the maximum you seek is $\displaystyle\frac{516}{11}$...Fri, 16 Oct 2020 15:52:14 +0200https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53939#post-id-53939Comment by Emmanuel Charpentier for <p>F = (4<em>x+4</em>y+9<em>z-2</em>w),
G = (2<em>x+2</em>y +z+ w)==0,
H = (x^2+y^2+z-4)==0,</p>
<ul>
<li>x ,y, z, w is symbolic expressions in 4 variables.</li>
<li>The symbolic expressions G and H represent constraints.</li>
<li><p>find the maximum value of F , subject to the constraints G=0 and H=0 ?????????</p></li>
<li><p>please help me</p></li>
</ul>
https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53940#post-id-53940Another hint : in your specific case, Lagrange multipliers are not strictly necessary, and a high school-level method is sufficient...
Nevertheless, looking up "Lagrange multipliers" in Wikipedia and understanding it is *strongly* advisable, event if the rigorous proofs underlying it require a bit more analysis than high school-level... (Anyway, high school-level analysis postulates some properties of the real numbers, whose proof requires way more than high school-level algebra and topology (the latter being null IIRC...)).Fri, 16 Oct 2020 16:18:28 +0200https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53940#post-id-53940Comment by slelievre for <p>F = (4<em>x+4</em>y+9<em>z-2</em>w),
G = (2<em>x+2</em>y +z+ w)==0,
H = (x^2+y^2+z-4)==0,</p>
<ul>
<li>x ,y, z, w is symbolic expressions in 4 variables.</li>
<li>The symbolic expressions G and H represent constraints.</li>
<li><p>find the maximum value of F , subject to the constraints G=0 and H=0 ?????????</p></li>
<li><p>please help me</p></li>
</ul>
https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53931#post-id-53931Where are you stuck?Thu, 15 Oct 2020 13:50:04 +0200https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53931#post-id-53931Comment by Emmanuel Charpentier for <p>F = (4<em>x+4</em>y+9<em>z-2</em>w),
G = (2<em>x+2</em>y +z+ w)==0,
H = (x^2+y^2+z-4)==0,</p>
<ul>
<li>x ,y, z, w is symbolic expressions in 4 variables.</li>
<li>The symbolic expressions G and H represent constraints.</li>
<li><p>find the maximum value of F , subject to the constraints G=0 and H=0 ?????????</p></li>
<li><p>please help me</p></li>
</ul>
https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53908#post-id-53908Homework ?Wed, 14 Oct 2020 13:14:09 +0200https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?comment=53908#post-id-53908Answer by Emmanuel Charpentier for <p>F = (4<em>x+4</em>y+9<em>z-2</em>w),
G = (2<em>x+2</em>y +z+ w)==0,
H = (x^2+y^2+z-4)==0,</p>
<ul>
<li>x ,y, z, w is symbolic expressions in 4 variables.</li>
<li>The symbolic expressions G and H represent constraints.</li>
<li><p>find the maximum value of F , subject to the constraints G=0 and H=0 ?????????</p></li>
<li><p>please help me</p></li>
</ul>
https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?answer=54000#post-id-54000One week after, we still don't know if this is homework or not.
Well... one week after, it might be stale. So here are a couple of
answers to a question interesting beyond the OP's potential homework.
The problem data can be set up in Sage as follows:
# Problem data
# Arguments of the function to optimize and nullity constraints to satisfy
Args = var("w, x, y, z")
# Function to optimize (here a symbolic expression)
F = (4*x + 4*y + 9*z - 2*w)
# Constraints (here, symbolic expressions to nullify)
G = (2*x + 2*y + z + w)
H = (x^2 + y^2 + z - 4)
# Problem setup: tuple of constraints -- expressed in
# a way that does not depend on the particular problem
C = (G, H)
### General solution
The general solution states that if (the $n$-dimensional vector) $\mathbf{x_0}$
maximizes (the scalar) $\phi(\mathbf{x})$ under the ($m$-vector) equality constraint
$\boldsymbol{\psi}\mathbf{(x}) = \mathbf{0}$, then there exist an ($m$-dimensional) vector
$\mathbf{\lambda}$ (the Lagrange multipliers) such as all the components of the
differential of the (scalar) function (callad Lagrangian)
$\phi(\mathbf{x_0})+ \mathbf{\lambda} \cdot \mathbf{\boldsymbol{\psi}(x_0)}$ are all zero;
in other words, the maximization of a scalar function of $n$ variables
under $m$ constraints is replaced by the maximization
of a scalar function of $n + m$ variables.
This translates in Sage as:
# Solution
# (Vector) function of constraints
psi = vector(C).function(*Args)
# Vector of Lagrange multipliers
Lambda = vector([var("lambda_{}".format(u)) for u in range(len(C))])
# Function to optimize
phi = F.function(*Args)
# Lagrangian arguments
ArgsL = Args + tuple(Lambda)
# Lagrangian function
L = (phi + Lambda.dot_product(psi)).function(*ArgsL)
# Its differential (vector)
dL = L.diff()(*ArgsL)
# Solution(s) nullifying this differential :
Sol = solve(list(dL), ArgsL, solution_dict=True)
# Extremal value
Max = F.subs(Sol)
which gives:
sage: Sol
[{w: -628/121, x: 4/11, y: 4/11, z: 452/121, lambda_0: 2, lambda_1: -11}]
sage: Max
516/11
**Nota bene:** the Lagrange multipliers solutions *can* be maxima or minima,
but may also be saddle points; this should be checked.
### Specialized solution
The idea is to use the $m$ constraints to express $m$ variables as functions of
the other $n - m$ variables. (Here, we can express $w$ and $z$ as functions
of $x$ and $y$). The problem is then to find the (unconstrained) maximum
of a function of $n - m$ variables, which is done by solving the components
of its differential for those variables.
This is called parameterization.
# Eliminate two variables from the two constraints. Here, we choose w and z,
# since one constraint uses x^2 and y^2, opening the door to multiple solutions
S1 = solve([G, H], [w, z], solution_dict=True)
# Substitute this in F
F1 = F.subs(S1)
# Optimize F1, i. e. find the point(s) where both derivatives are zero
Sol1 = solve([F1.diff(u) for u in (x, y)], [x, y], solution_dict=True)
# Substitute in the [G, H] system, and solve for (numerical) w, z
Sol2 = solve([u.subs(Sol1[0]) for u in (G, H)], [w, z], solution_dict=True)
# Cleanup: One dictionary solution
HSSol = copy(Sol1[0])
HSSol.update(Sol2[0])
# Get the (numerical) extremum
HSMax = F.subs(HSSol)
which gives:
sage: HSSol
{x: 4/11, y: 4/11, w: -628/121, z: 452/121}
sage: HSMax
516/11
Again, the nature (maximum, minimum or saddle-point)
of the extrema thus obtained must be checked.
### Numerical solutions
One can note that Sage implements numerical methods of minimization
of a real function subject to (more general inequality) constraints,
accessible via the `minimize_constrained` function. Other methods
implemented in SciPy (which Sage depends on) can also be used.Wed, 21 Oct 2020 21:14:45 +0200https://ask.sagemath.org/question/53907/find-maximum-value-of-f/?answer=54000#post-id-54000