8 views (last 30 days)

Show older comments

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

grad_Cx=diff(C,x);

grad_Cy=diff(C,y);

grad_Cz=diff(C,z);

%this is the g (constraint)-volume

grad_Vx=diff(V,x);

grad_Vy=diff(V,y);

grad_Vz=diff(V,z);

%equations to be solved

eqns=[grad_Cx==lambda*grad_Vx,grad_Cy==lambda*grad_Vy,grad_Cz==lambda*grad_Vz,x*y*z==V];

%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

grad_Cx=diff(C,x);

grad_Cy=diff(C,y);

grad_Cz=diff(C,z);

%this is the g (constraint)-volume

grad_V1x=diff(V1,x);

grad_V1y=diff(V1,y);

grad_V1z=diff(V1,z);

%equations to be solved

eqns=[grad_Cx==lambda*grad_V1x,grad_Cy==lambda*grad_V1y, ...

grad_Cz==lambda*grad_V1z, V1==V];

%solve

P=solve(eqns,[x y z lambda])

x0 = simplify(P.x)

y0 = simplify(P.y)

z0 = simplify(P.z)

lambda0 = simplify(P.lambda)

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!