Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Mission 2. Code.m function best = circpack(n,iter) f=@(x)-x(2*n+1); best = zeros

ID: 3583159 • Letter: M

Question

Mission 2.

Code.m

function best = circpack(n,iter)
f=@(x)-x(2*n+1);
best = zeros(2*n+1);
for i=1:iter
    ip=rand(2*n+1,1)';
    ip(2*n+1)=0;
    lb=zeros(2*n+1,1)';
    ub=ones(2*n+1,1)';
    mo=fmincon(f,ip,[],[],[],[],lb,ub,@constraint);
    if mo(2*n+1)>best(2*n+1)
        best = mo;
    end
end
t=0:0.01:2*pi;
x=best(2*n+1)*cos(t);
y=best(2*n+1)*sin(t);
hold on;
for i=1:n
    plot(x+best(2*i-1),y+best(2*i));
end
best(2*n+1);
  

Constraint.m

function [c ceq] = constraint(x)
k=1;
n=10;
ceq=0;
for i = 1:n-1
    for j=n-1:-1:i
        c(k) = -(x(2*i-1)-x(2*j+1))^2-(x(2*i)-x(2*j+2))^2 + 4*x(2*n+1)^2;
        k=k+1;
    end
end
for i=1:2*n
    c(i+n*(n-1)/2)=-x(i)+x(2*n+1);
end
for i=1:2*n
    c(i+n*(n+3)/2)=-1+x(2*n+1)+x(i);
end

Mission #2: Using MATLAB to solve packing problems Circle packing problems can be formulated as nonlinear programs with non-convex feasible regions. For this reason, CPLEX cannot be used to solve them. Instead, we will use MATLAB. are paper paper, near-optimal packings of eircles in the unit (ix1) square obtained using the MATLAB optimization toolbox Extend the code to answer any two of the following five questions: rcles of radius 2.3m ean be packed inside a rectangle of dimensions 20m x 50m2 (2) How many circles of radius 8m can be packed inside a triangle whose sides measure 20, 30, and 40 meters? (3) How many spheres of radius 2.1 ft can be packed inside a rectangular container of dimensions 8 ft x 8.5 ft x 40 ft? (4) How many circles of radius 2.3m can be packed inside a circle of radius 17m? (5) How many spheres of radius 5.8m can be packed inside a sphere of radius 25m? Describe your efforts and report your results in a 2-page report. For each of the two questions you consider, (l) state the coordinates of the circles/spheres in the best packing that you found and (2) print out a picture of the best packing that you found (two dimensional questions only) Also, email the instructor two MATLAR codes, one for each problem.

Explanation / Answer

Answer :

Code.m

function best = circpack(n,iter)
f=@(x)-x(2*n+1);
best = zeros(2*n+1);
for i=1:iter
    ip=rand(2*n+1,1)';
    ip(2*n+1)=0;
    lb=zeros(2*n+1,1)';
    ub=ones(2*n+1,1)';
    mo=fmincon(f,ip,[],[],[],[],lb,ub,@constraint);
    if mo(2*n+1)>best(2*n+1)
        best = mo;
    end
end
t=0:0.01:2*pi;
x=best(2*n+1)*cos(t);
y=best(2*n+1)*sin(t);
hold on;
for i=1:n
    plot(x+best(2*i-1),y+best(2*i));
end
best(2*n+1);
  

Constraint.m

function [c ceq] = constraint(x)
k=1;
n=10;
ceq=0;
for i = 1:n-1
    for j=n-1:-1:i
        c(k) = -(x(2*i-1)-x(2*j+1))^2-(x(2*i)-x(2*j+2))^2 + 4*x(2*n+1)^2;
        k=k+1;
    end
end
for i=1:2*n

    c(i+n*(n-1)/2)=-x(i)+x(2*n+1);
end
for i=1:2*n
    c(i+n*(n+3)/2)=-1+x(2*n+1)+x(i);
end