# optimization using lagrange multipliers

25 views (last 30 days)
Lal Deger on 3 Apr 2021
Commented: Lal Deger on 4 Apr 2021
I have a problem where I have to minimize cost of a container given by C = Sxy + 2Wz(x+y) and Volume=xyz where x,y are bottom dimensions and z the height of the box. S is the cost of material for the bottom per m^2 and W for the sides. For this I wrote the following code:
syms x y z S W lambda V
C=S*x*y+2*W*z*(x+y);
V=x*y*z;
%this is the f (function to be optimized)-cost
%this is the g (constraint)-volume
%equations to be solved
%solve
P=solve(eqns,[x y z lambda])
however in the answers I get (x,y,z,lambda)=(4W,4W,2S,1) and (0,0,0,0). The answer I should be getting is: could anyone tell me what I am doing wrong?

David Goodmanson on 4 Apr 2021
Hi Lal,
The problem is that you have V(x,y,z) = x*y*z as a function, but you do not define a fixed value for the volume. The code below uses V1 = x*y*z and later sets that to V.
% make variables positive to cut down to one solution
syms x y z S W lambda V1 V positive
C=S*x*y+2*W*z*(x+y);
V1=x*y*z;
%this is the f (function to be optimized)-cost
%this is the g (constraint)-volume
%equations to be solved
%solve
P=solve(eqns,[x y z lambda])
x0 = simplify(P.x)
y0 = simplify(P.y)
z0 = simplify(P.z)
lambda0 = simplify(P.lambda)
Lal Deger on 4 Apr 2021
Ohhh, I see thank you very much!!