MATLAB: Is there anyone to help me for Lagrange multipliers method

lagrange

A container with an open top is to have 10 m^3 capacity and be made of thin sheet metal. Calculate the dimensions of the box if it is to use the minimum possible amount of metal. Use Lagrange multipliers method.
f=x*y+2*x*z+2*y*z
s.t g=x*y*z-10=0
I wrote these codes and found this answer. Is it correct solution?
syms x y z r ll
f=x*y+2*x*z+2*y*z;
g=x*y*z-10;
L=f-r*g;
gradL=gradient(L);
[xs ys zs rs]=solve(gradL==0,[x y z r],'Real',true);
h=.01;
k=.01;
for i=1:numel(xs)
fopt=double(subs(f,[x y z],[xs(i) ys(i) zs(i)]));
gc=subs(g,[x y z],[xs(i)+h ys(i)+k zs(i)+ll]);
l=double(solve(gc==0,ll));
[a j]=min(abs(l));
l=l(j);
fnear=double(subs(f,[x y z],[xs(i)+h ys(i)+k zs(i)+l]));
[xs(i) ys(i) zs(i)]
fopt
if fopt<fnear
disp('min')
elseif fopt>fnear
disp('max')
end
end
ans =
[ (5^(1/3)*16^(2/3))/4, (5^(1/3)*16^(2/3))/4, (5^(1/3)*16^(2/3))/8]
fopt =
22.1042
min

Best Answer

Hi,
your result is correct. Here is an alternative solution using lagrange multiplier:
syms l b h lambda
A = l * b + 2 * b * h + 2 * l * h; % area of needed sheet metal (thickness = 1, because we are lazy guys)
V = l * b * h - 10 == 0; % volume constraint 10 m³
L = A + lambda * lhs(V); % Langrange function
dL_dl = diff(L,l) == 0; % derivative of L with respect to l
dL_db = diff(L,b) == 0; % derivative of L with respect to b
dL_dh = diff(L,h) == 0; % derivative of L with respect to h
dL_dlambda = diff(L,lambda) == 0; % derivative of L with respect to lambda
system = [dL_dl; dL_db; dL_dh; dL_dlambda]; % build the system of equations
[l_val, b_val, h_val,lambda_val] = solve(system, [l b h lambda], 'Real', true) % solve the system of equations and display the results
results_numeric = double([l_val, b_val, h_val,lambda_val]) % show results in a vector of data type double
V_res = l_val * b_val * h_val % calculate the resulting volume (should be 10, if correct)
A_res = l_val * b_val + 2 * b_val * h_val + 2 * l_val * h_val % calculate the needed amount (area) of sheet metal
I get the same results as you get:
l_val =
(5^(1/3)*16^(2/3))/4
b_val =
(5^(1/3)*16^(2/3))/4
h_val =
(5^(1/3)*16^(2/3))/8
lambda_val =
-(5^(2/3)*16^(1/3))/5
V_res =
10
A_res =
3*5^(2/3)*16^(1/3)
>> double(A_res)
ans =
22.1042
The single results for l, b, h and lambda converted to double:
results_numeric =
2.7144 2.7144 1.3572 -1.4736
As already shown in another question from you, you can check the results using fmincon:
% Check numeric
fun = @(x)(x(1) * x(2) + 2 * x(2) * x(3) + 2 * x(1) * x(3));
numeric_solution = fmincon(fun,[2 2.5 2],[],[],[],[],[0 0 0],[10 10 10],@nonlcon)
function [c, ceq] = nonlcon(x)
c = [];
ceq = x(1) * x(2) * x(3) - 10;
end
which gives us the same results:
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
numeric_solution =
2.7144 2.7144 1.3572
Best regards
Stephan