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?

1 次查看(过去 30 天)
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?
  2 个评论
Busra Tabak
Busra Tabak 2018-12-16
syms a r
f=pi*r^2+a^2;
g=2*pi*r+4*a-3;
r1=solve(g==0,r);
f=subs(f,r,r1);
as=double(solve(diff(f,a)==0,a));
as=as(as>0);
rs=(3-4*as)/(2*pi);
fdp=diff(f,a,as);
fdp=double(subs(fdp,a,as));
if fdp>0
disp('min')
end
l1=2*pi*rs
l2=4*as
I wrote this and got 2 distributed shapes. Correct?
min
l1 =
1.3197
l2 =
1.6803

请先登录,再进行评论。

采纳的回答

Stephan
Stephan 2018-12-16
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by