MATLAB: A wire of 3 m length is to be used to make a circile and a square. How should the wire be distributed between two shapes in order to minimize the sum of the enclosed areas

doit4mehomeworkproblem

Can anyone write matlab code of this question?
A wire of 3 m length is to be used to make a circile and a square. How should the wire be distributed between two shapes in order to minimize the sum of the enclosed areas?

Best Answer

Hi,
your code returns the correct result. Here is another way to write the same in symbolic form:
syms l1 l2 % declare symbolic variables
A_quad = (l1/4)^2; % area of square expressed with l1
A_circ = (l2/pi)^2*pi/4; % areas of circle expressed with l2
A = A_quad + A_circ; % sum of areas
A = subs(A,l2,l1-3); % using the constraint that l1 + l2 = 3 to eliminate l2
A_diff = diff(A,l1) == 0; % calculate first derivative and set it equal to zero
A_min = double(diff(lhs(A_diff),l1)) % check 2. derivative --> if positive it is a minimum
res = solve(A_diff,l1) % solve for l1 (analytic expression)
sol_l1 = double(res) % get a numeric value for the solution above
This returns:
A_min =
0.2842
res =
12/(pi + 4)
sol_l1 =
1.6803
Since A_min is positive you know it is a minimum. You can also check the result numeric within 2 lines of code:
% Check numeric
fun = @(l)((l(1)/4)^2 + (l(2)/pi)^2*pi/4);
num_sol_l1 = fmincon(fun,[3 0],[],[],[1 1],3)
The result is:
num_sol_l1 =
1.6803 1.3197
Best regards
Stephan