Matlab 圆内的填充圆

Matlab 圆内的填充圆,matlab,geometry,packing,nonlinear-optimization,Matlab,Geometry,Packing,Nonlinear Optimization,给定单位圆和半径为r的一组M小圆。找出较小圆的最大半径,使它们都能在单位圆内不重叠 在多边形示例中,我有以下圆形填充 我想改变所有圆都在多边形内的方程 theta = 2*pi/N; % Angle covered by each side of the polygon phi = theta*(0:N-1)'; % Direction of the normal to the side of the polygon polyEq = ( [cos(phi) sin(phi)]*

给定单位圆和半径为r的一组M小圆。找出较小圆的最大半径,使它们都能在单位圆内不重叠

在多边形示例中,我有以下圆形填充

我想改变所有圆都在多边形内的方程

theta = 2*pi/N;       % Angle covered by each side of the polygon

phi = theta*(0:N-1)'; % Direction of the normal to the side of the polygon

polyEq = ( [cos(phi) sin(phi)]*x <= cdist-r );
theta=2*pi/N;%多边形每边覆盖的角度
φ=θ*(0:N-1);%多边形侧面的法线方向

polyq=([cos(phi)sin(phi)]*x在您的例子中,没有“多边形的边”,因此没有与
θ
的类似物,您需要更改引用的每个位置
θ
,以及引用
θ
的所有变量(如
φ

类似下面的内容应该可以使用。我刚刚从你的链接复制粘贴了代码,去掉了
theta
phi
,重新定义了
cdist
polyq
,并让它在答案中绘制一个单位圆,而不是多边形。请询问这些选择是否不清楚

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)]);
M=19;%要拟合的圆数
toms r%圆半径
x=tom('x',2,M);%圆心坐标
清除pi%3.1415。。。
cdist=1;%单位圆半径
%%%%%%表示所有圆都在单位圆内的方程式
聚醚=(sqrt(x(1,:).^2+x(2,:).^2)+r=2*r);
结束
%开始猜测
x0={r==0.5*sqrt(1/M),x==0.3*randn(尺寸(x))};
%解决问题,最大化r
选项=结构;
%尝试多次开始猜测,并选择最佳结果
options.solver='multimin';
options.xInit=30;%不同的起始猜测数
解决方案=ezsolve(-r,{polyq,circEq},x0,选项);
%绘图结果
x_vals=(0:100)。/100;
单位圆顶部的绘图(sqrt(1-x_vals.^2),“-”)%
单位圆底部的绘图(-1.*sqrt(1-x_vals.^2),'-')%
轴图像
等等
alpha=linspace(0,2*pi,100);
cx=溶液.r*cos(α);
cy=溶液r*sin(α);
对于i=1:M
图(解x(1,i)+cx,解x(2,i)+cy)%圈数i
结束
拖延
标题(['Maximum radius='num2str(solution.r)]);

这不是一个编程问题,因此在本网站上是离题的。现在我有错误,我已更正了答案中的语法,因此您不应该出现错误。但是,您应该尝试理解我为什么编写此答案以及错误消息的含义,以便您可以自己修复它,并能够在未来。