# 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??

edit retag close merge delete

1

It 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)

( 2014-07-03 07:30:17 -0600 )edit
1

When 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?

( 2014-07-03 07:58:22 -0600 )edit

rws, 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.

( 2014-07-03 15:49:13 -0600 )edit

calc314, 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?

( 2014-07-03 15:52:17 -0600 )edit

Sort by ยป oldest newest most voted

I'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])

more

%% 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= 2Lx + 2*x - 6 == 0

eqn2= 2Ly + 2*y - 2 == 0

eqn3= 2Lz + 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/11sqrt(11), y == -2/11sqrt(11), z == 2/11sqrt(11), L == -1/2sqrt(11) - 1],

[x == 6/11sqrt(11), y == 2/11sqrt(11), z == -2/11sqrt(11), L == 1/2sqrt(11) - 1]]

%% moral of the story: if symbolic methods work fine, why bother with numerical methods?! ;-)

more