【问题标题】:Creating a circle in a square grid在方形网格中创建一个圆
【发布时间】:2021-12-29 08:30:17
【问题描述】:

我尝试通过修复平行板电容器代码来解决以下二维椭圆 PDE 静电问题。但是我在绘制圆形区域时遇到问题。如何绘制圆形区域而不是正方形?

% I use following two lines to label the 50V and 100V squares
% (but it should be two circles)
                
     V(pp1-r_circle_small:pp1+r_circle_small,pp1-r_circle_small:pp1+r_circle_small) = 50;
     V(pp2-r_circle_big:pp2+r_circle_big,pp2-r_circle_big:pp2+r_circle_big) = 100;



    % Contour Display for electric potential
    figure(1)
    contour_range_V = -101:0.5:101;
    contour(x,y,V,contour_range_V,'linewidth',0.5);
    axis([min(x) max(x) min(y) max(y)]);
    colorbar('location','eastoutside','fontsize',10);
    xlabel('x-axis in meters','fontsize',10);
    ylabel('y-axis in meters','fontsize',10);
    title('Electric Potential distribution, V(x,y) in volts','fontsize',14);
    h1=gca;
    set(h1,'fontsize',10);
    fh1 = figure(1); 
    set(fh1, 'color', 'white')

    % Contour Display for electric field
    figure(2)
    contour_range_E = -20:0.05:20;
    contour(x,y,E,contour_range_E,'linewidth',0.5);
    axis([min(x) max(x) min(y) max(y)]);
    colorbar('location','eastoutside','fontsize',10);
    xlabel('x-axis in meters','fontsize',10);
    ylabel('y-axis in meters','fontsize',10);
    title('Electric field distribution, E (x,y) in V/m','fontsize',14);
    h2=gca;
    set(h2,'fontsize',10);
    fh2 = figure(2); 
    set(fh2, 'color', 'white')

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow!请阅读How to Ask,然后阅读edit 您的问题,以将每个帖子的问题数量限制为 1。此规则的存在是因为它使 Stack Overflow 可作为知识数据库进行搜索,而不仅仅是服务台的目的。所以请删除你的第二个问题,你能把你的代码减少到minimal reproducible example吗?最小的部分是相关的,请突出显示您在代码中绘制正方形的位置并删除所有不相关的代码。这让我们更容易为您提供帮助。
  • 重读您的第二个问题后:这与 Stack Overflow 无关。这不是编程问题,而是电气工程问题(或您对结果的解释中的问题)。尝试我们网络上专门用于电气工程的任何姊妹站点,以找到该问题的答案。虽然第一个问题是有效的,但请说明您在代码中绘制正方形的位置以及您希望如何将其更改为圆形。
  • 好的!对此感到抱歉。我马上修改。
  • 对,所以你基本上是用方形索引索引你的V,这会给你一个正方形。相反,您需要定义圆内的点(提示:您有中心和半径,任何网格点上的简单sqrt((x-x0)^2+(y-y0)^2) 都会告诉您它是在里面还是在外面)并且只将这些值设置为50 或 100。剩下的代码你可以留下来,我一眼就能看出

标签: matlab matrix


【解决方案1】:

由于您的索引方式,您正在创建一个正方形(请参阅this post on indexing)。您已指定要从 pp1-r_circle_smallpp1+r_circle_small 运行的行,列的类似内容。鉴于Swiss cheese is not an option,您正在创建一个完整的正方形。

从几何我们知道,距离圆心(X0,Y0) 的所有点在sqrt((X-X0)^2 - (Y-Y0)^2) < R 范围内,半径为R 都在圆内,其余的在圆外。这意味着您可以简单地构建一个掩码:

% Set up your grid
Xsize = 30;  % Your case: 1
Ysize = 30;  % Your case: 1
step = 1;  % Amount of gridpoints; use 0.001 or something
% Build indexing grid for circle search, adapt as necessary
X = 0:step:Xsize;
Y = 0:step:Ysize;
[XX,YY] = meshgrid(X, Y);
V = zeros(numel(X), numel(Y));

% Repeat the below for both circles
R = 10;  % Radius of your circle; your case 0.1 and 0.15
X0 = 11;  % X coordinate of the circle's origin; your case 0.3 and 0.7
Y0 = 15;  % Y coordinate of the circle's origin; your case 0.3 and 0.7

% Logical check to see whether a point is inside or outside
mask = sqrt( (XX - X0).^2 + (YY - Y0).^2) < R;

V(mask) = 50;  % Give your circle the desired value

imagesc(V)  % Just to show you the result
axis equal % Use equal axis to have no image distortion

mask 是一个逻辑矩阵,其中包含1,其中点在您的圈内,0,其中点在圈外。然后,您可以使用此掩码对您的潜在网格 V 进行逻辑索引,以将其设置为所需的值。

注意:显然,这不会创建一个完美的圆圈,因为您无法在方形网格上绘制一个完美的圆圈。网格越细,你的“圆圈”就越像圆圈。这显示了step = 0.01的结果

注意 2:您需要调整 XYX0Y0R 的定义以匹配您的值。

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多