0

I try to solve the following 2D elliptic PDE electrostatic problem by fixing the Parallel plate Capacitors code. But I have problem to plot the circle region. How can I plot a circle region rather than the square?

Fig.1

% 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')

Fig.2

enter image description here

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Y Yen
  • 13
  • 3
  • Hi and welcome to Stack Overflow! Please read [ask] and then [edit] your question to limit the amount of questions per post to 1. This rule exists because it makes Stack Overflow searchable as a knowledge database, rather than only serve the purpose of help desk. So please remove your second question and can you please reduce your code to a [mcve]? The minimal part is relevant, please highlight where in the code you plot the square and remove all unrelated code. That makes it easier for us to help you. – Adriaan Dec 29 '21 at 08:35
  • Upon rereading your second question: that's off-topic for Stack Overflow. It is not a programming problem, but rather an electrical engineering problem (or one in your interpretation of your results). Try any of our sister sites on the network dedicated to electrical engineering to find an answer to that question. The first question though, is valid, just please make it clear where in the code you plot the square and how you'd like to change it to a circle. – Adriaan Dec 29 '21 at 08:43
  • OK! Sorry for that. I'll edit it right away. – Y Yen Dec 29 '21 at 08:44
  • Right, so you're basically indexing your `V` with square indices, that's going to give you a square. Instead, you'll need to define what points are inside your circle (hint: you've got the centre and the radius, a simple `sqrt((x-x0)^2+(y-y0)^2)` on any gridpoint will tell you whether it's inside or out) and only set those values to 50 or 100. The rest of the code you can leave be, as far as I can see at a quick glance – Adriaan Dec 29 '21 at 08:47

1 Answers1

0

You're creating a square due to the way you're indexing (see this post on indexing). You've specified the rows to run from pp1-r_circle_small to pp1+r_circle_small and similar for the columns. Given that Swiss cheese is not an option, you're creating a complete square.

From geometry we know that all points within distance sqrt((X-X0)^2 - (Y-Y0)^2) < R from the centre of the circle at (X0,Y0) with radius R are within the circle, and the rest outside. This means that you can simply build a mask:

% 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 is a logical matrix containing 1 where points are within your circle and 0 where points are outside. You can then use this mask to logically index your potential grid V to set it to the desired value.

Circle

Note: This will, obviously, not create a perfect circle, given you cannot plot a perfect circle on a square grid. The finer the grid, the more circle-like your "circle" will be. This shows the result with step = 0.01

enter image description here

Note 2: You'll need to tweek your definition of X, Y, X0, Y0 and R to match your values.

Adriaan
  • 17,741
  • 7
  • 42
  • 75