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.Fri, 04 Jul 2014 02:56:15 +0200function minimize cannot solve Lagrange Multipliers problem.https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/ I've tried all sorts of initial conditions but this just won't converge. It is an easy Lagrange multipliers problem.
var("y z L")
F(x, y, z, L) = (x-3)^2 + (y-1)^2 + (z + 1)^2 + L*(x^2 + y^2 + z^2 - 4)
minimize(F, [4, 3, 2, 1], algorithm='ncg' )
Any thoughts? Are there ways of forcing it to use more iterations, or something??
Thu, 03 Jul 2014 03:35:21 +0200https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/Comment by Gregory Bard for <p>I've tried all sorts of initial conditions but this just won't converge. It is an easy Lagrange multipliers problem. </p>
<pre><code>var("y z L")
F(x, y, z, L) = (x-3)^2 + (y-1)^2 + (z + 1)^2 + L*(x^2 + y^2 + z^2 - 4)
minimize(F, [4, 3, 2, 1], algorithm='ncg' )
</code></pre>
<p>Any thoughts? Are there ways of forcing it to use more iterations, or something??</p>
https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?comment=23191#post-id-23191rws, I'm not sure where the plus signs went. They definitely belong. It must have been an error during cut-and-paste. I was using the Sage Single-Cell Server.Thu, 03 Jul 2014 22:49:13 +0200https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?comment=23191#post-id-23191Comment by Gregory Bard for <p>I've tried all sorts of initial conditions but this just won't converge. It is an easy Lagrange multipliers problem. </p>
<pre><code>var("y z L")
F(x, y, z, L) = (x-3)^2 + (y-1)^2 + (z + 1)^2 + L*(x^2 + y^2 + z^2 - 4)
minimize(F, [4, 3, 2, 1], algorithm='ncg' )
</code></pre>
<p>Any thoughts? Are there ways of forcing it to use more iterations, or something??</p>
https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?comment=23192#post-id-23192calc314, I'm sorry, but that's not correct. See where it says "Current function value: 2.032982"? That should be zero. Also, it does not match the symbolic solution at all. Perhaps this is a bug, and minimize should give an error message, since the minimum wasn't reached?Thu, 03 Jul 2014 22:52:17 +0200https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?comment=23192#post-id-23192Comment by calc314 for <p>I've tried all sorts of initial conditions but this just won't converge. It is an easy Lagrange multipliers problem. </p>
<pre><code>var("y z L")
F(x, y, z, L) = (x-3)^2 + (y-1)^2 + (z + 1)^2 + L*(x^2 + y^2 + z^2 - 4)
minimize(F, [4, 3, 2, 1], algorithm='ncg' )
</code></pre>
<p>Any thoughts? Are there ways of forcing it to use more iterations, or something??</p>
https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?comment=23184#post-id-23184It appears to have worked for me in SMC. Here is the output:
Optimization terminated successfully.
Current function value: 2.032982
Iterations: 3
Function evaluations: 57
Gradient evaluations: 3
Hessian evaluations: 3
(1.48641364243, 0.860982992316, 0.536302359521, 0.251059261969)Thu, 03 Jul 2014 14:30:17 +0200https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?comment=23184#post-id-23184Comment by rws for <p>I've tried all sorts of initial conditions but this just won't converge. It is an easy Lagrange multipliers problem. </p>
<pre><code>var("y z L")
F(x, y, z, L) = (x-3)^2 + (y-1)^2 + (z + 1)^2 + L*(x^2 + y^2 + z^2 - 4)
minimize(F, [4, 3, 2, 1], algorithm='ncg' )
</code></pre>
<p>Any thoughts? Are there ways of forcing it to use more iterations, or something??</p>
https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?comment=23185#post-id-23185When I input the second line I get SyntaxError. With plus signs inserted I also get the succesful result. This is Sage-6.3beta5. SMC is Sage-6.2. Which version is your question about?Thu, 03 Jul 2014 14:58:22 +0200https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?comment=23185#post-id-23185Answer by Gregory Bard for <p>I've tried all sorts of initial conditions but this just won't converge. It is an easy Lagrange multipliers problem. </p>
<pre><code>var("y z L")
F(x, y, z, L) = (x-3)^2 + (y-1)^2 + (z + 1)^2 + L*(x^2 + y^2 + z^2 - 4)
minimize(F, [4, 3, 2, 1], algorithm='ncg' )
</code></pre>
<p>Any thoughts? Are there ways of forcing it to use more iterations, or something??</p>
https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?answer=23190#post-id-23190%% step one:
var("y z L")
F(x, y, z, L) = (x-3)^2 + (y-1)^2 + (z+1)^2 + L*(x^2 + y^2 + z^2 - 4)
derivative(F)
%% step two:
eqn1= 2*L*x + 2*x - 6 == 0
eqn2= 2*L*y + 2*y - 2 == 0
eqn3= 2*L*z + 2*z + 2 == 0
eqn4= x^2 + y^2 + z^2 - 4 == 0
solve( [eqn1, eqn2, eqn3, eqn4], [x, y, z, L] )
%% correct answer obtained:
[[x == -6/11*sqrt(11), y == -2/11*sqrt(11), z == 2/11*sqrt(11), L == -1/2*sqrt(11) - 1],
[x == 6/11*sqrt(11), y == 2/11*sqrt(11), z == -2/11*sqrt(11), L == 1/2*sqrt(11) - 1]]
%% moral of the story: if symbolic methods work fine, why bother with numerical methods?! ;-)Thu, 03 Jul 2014 22:48:04 +0200https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?answer=23190#post-id-23190Answer by calc314 for <p>I've tried all sorts of initial conditions but this just won't converge. It is an easy Lagrange multipliers problem. </p>
<pre><code>var("y z L")
F(x, y, z, L) = (x-3)^2 + (y-1)^2 + (z + 1)^2 + L*(x^2 + y^2 + z^2 - 4)
minimize(F, [4, 3, 2, 1], algorithm='ncg' )
</code></pre>
<p>Any thoughts? Are there ways of forcing it to use more iterations, or something??</p>
https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?answer=23195#post-id-23195I've done a bit of digging. You can set the max number of iterations and the tolerance.
minimize(F, [4,3,2,1], algorithm='ncg', avextol=10^(-30) , maxiter =10000 )
These get passed to scipy. However, this does not resolve your issue.
I agree with your symbolic approach! Here a different way to code it:
soln=solve(list(F.gradient()),[x,y,z,L])
Fri, 04 Jul 2014 02:56:15 +0200https://ask.sagemath.org/question/23173/function-minimize-cannot-solve-lagrange-multipliers-problem/?answer=23195#post-id-23195