在您的情况下,没有“多边形的边”,因此没有类似物theta
,您需要更改引用的每个位置theta
,以及引用的所有变量theta
(如phi
)。
像下面这样的东西应该可以工作。我刚刚从您的链接中复制粘贴了代码,摆脱了theta
and phi
,重新定义了cdist
and polyEq
,并使它在答案中绘制了一个单位圆而不是多边形。请询问这些选择是否不清楚。
M = 19; % number of circles to fit
toms r % radius of circles
x = tom('x', 2, M); % coordinates of circle centers
clear pi % 3.1415...
cdist = 1; % radius of unit circle
%%%%%% equations saying all circles are inside of unit circle
polyEq = (sqrt(x(1,:).^2 + x(2,:).^2) + r <= cdist);
% create a set of equations that say that no circles overlap
circEq = cell(M-1,1);
for i=1:M-1
circEq{i} = ( sqrt(sum((x(:,i+1:end)-repmat(x(:,i),1,M-i)).^2)) >= 2*r );
end
% starting guess
x0 = { r == 0.5*sqrt(1/M), x == 0.3*randn(size(x)) };
% solve the problem, maximizing r
options = struct;
% try multiple starting guesses and choose best result
options.solver = 'multimin';
options.xInit = 30; % number of different starting guesses
solution = ezsolve(-r,{polyEq,circEq},x0,options);
% plot result
x_vals = (0:100)./100;
plot(sqrt(1 - x_vals.^2),'-') % top of unit circle
plot(-1.*sqrt(1 - x_vals.^2),'-') % bottom of unit circle
axis image
hold on
alpha = linspace(0,2*pi,100);
cx = solution.r*cos(alpha);
cy = solution.r*sin(alpha);
for i=1:M
plot(solution.x(1,i)+cx,solution.x(2,i)+cy) % circle number i
end
hold off
title(['Maximum radius = ' num2str(solution.r)]);