How to plot points on several concentric circles in sequence? - matlab

I want to plot several points on several concentric circles just like this:
The number of points on different circles are same and to be determined. The difference of radius are same.
But I face the problem that if I want to use the for function, I define i = 1:total number of points. I don't know how to select correct angle value. Can anyone help me?
Here is the code that I wrote:
R_steplength = 1; %difference of radius
angle_point = 20; %total number of points on one circle
max_R = 4; %outer radius of circle
central_x = 1; % origin of concentric circle
central_y = 1;
total_circle_points = (max_R/R_steplength) * angle_point; %calculate total
points
fin_x= zeros(1, total_circle_points); %define output points position
fin_y = zeros(1, total_circle_points);
for i = 1:total_circle_points
for j = 1:angle_point
if rem(i+1, 20)~= 1
k = floor(i/20);
angles = linspace(0,2*pi,angle_point);
fin_x(i) = R_steplength*(k+1)*cos(angles(j))+central_x;
fin_y(i)= R_steplength*(k+1)*sin(angles(j))+central_y;
else
fin_x(i) = central_x + R_steplength*(k+2);
fin_y(i) = central_y + R_steplength*(k+2);
end
plot(fin_x(:),fin_y(:),'ro')
end
end

You can use polarplot for that:
ax = polaraxes; % create polar axes
% calculate all points locations:
[angles,rad] = meshgrid(0:angle_point:360,1:R_steplength:max_R);
polarplot(ax,deg2rad(angles),rad,'r.') % plot all the points
ax.GridColor = 'k'; % set grid color to black
ax.GridAlpha = 1;
ax.ThetaAxis.TickValues = 10:20:360; % set radius grid between the points
ax.RAxis.TickValues = 1.5:R_steplength:(max_R+0.5); % set circles between the points
ax.RAxis.Limits = [0 max_R+0.5]; % show the outer circle
Here I use the axes grid to draw the circles. If this is not needed you can just write:
[angles,rad] = meshgrid(0:angle_point:360,1:R_steplength:max_R);
polarplot(ax,deg2rad(angles),rad,'r.') % plot all the points

Related

How do I label lines in a MatLab plot?

What my plot looks like
What the plot should look like
The code is working like it should but im trying to get the labels to show up on each line from (1-8). Just like the picture above.
I have read a bunch of posts and tried to search Matlab but i havent been able to figure it out.
clc;clear;close all;
V_inf = 20; % freestream velocity
R = 1; % cylinder radius
n = 8; % number of panels
d_theta = 2*pi/n; % resolution of angles
alpha = 0; % angle of attack
theta = pi+pi/n:-d_theta:-pi+pi/n; % angles of boundary points of panels
X = R*cos(theta); % X coordinates of boundary points of panels
Y = R*sin(theta); % Y coordinates of boundary points of panels
Phi = zeros(n,1); % angle from Vinf to bottom of panel
beta = zeros(n,1); % angle from Vinf to outward normal of panel
conX = zeros(n,1); % X coordinates of control points
conY = zeros(n,1); % Y coordinates of control points
S = zeros(n,1); % panel length
for i = 1:n
Phi(i) = -alpha + atan2((Y(i+1)-Y(i)),(X(i+1)-X(i)));
beta(i) = Phi(i)+pi/2;
if beta(i)>2*pi, beta(i)=beta(i)-2*pi;
elseif beta(i)<0, beta(i)=beta(i)+2*pi; end
conX(i) = (X(i+1)+X(i))/2;
conY(i) = (Y(i+1)+Y(i))/2;
S(i) = sqrt((X(i+1)-X(i))^2 + (Y(i+1)-Y(i))^2);
end
close all
plot(R*cos(0:0.01:2*pi),R*sin(0:0.01:2*pi),'b', X,Y,'r',conX,conY,'g^');
axis equal; legend('Exact shape','Panel approximation','Control points')
xlabel('x, m'); ylabel('y, m'); title('Fig. 1 Panel layout (n = 8, R = 1m)');
Possibly plotting the labels along the points of a circle using the text() function may suffice. There's some shifting of points and flipping that needs to be done to get the order you wish but otherwise it's just 8 points taken along a circle that is smaller in diameter in comparison to the octagon. An alternative would be using the green triangles as reference instead but that involves more math. As long as your octagon is expected to be symmetrical vertically and horizontally this should work alright.
clc;clear;close all;
V_inf = 20; % freestream velocity
R = 1; % cylinder radius
n = 8; % number of panels
d_theta = 2*pi/n; % resolution of angles
alpha = 0; % angle of attack
theta = pi+pi/n:-d_theta:-pi+pi/n; % angles of boundary points of panels
X = R*cos(theta); % X coordinates of boundary points of panels
Y = R*sin(theta); % Y coordinates of boundary points of panels
Phi = zeros(n,1); % angle from Vinf to bottom of panel
beta = zeros(n,1); % angle from Vinf to outward normal of panel
conX = zeros(n,1); % X coordinates of control points
conY = zeros(n,1); % Y coordinates of control points
S = zeros(n,1); % panel length
for i = 1:n
Phi(i) = -alpha + atan2((Y(i+1)-Y(i)),(X(i+1)-X(i)));
beta(i) = Phi(i)+pi/2;
if beta(i)>2*pi, beta(i)=beta(i)-2*pi;
elseif beta(i)<0, beta(i)=beta(i)+2*pi; end
conX(i) = (X(i+1)+X(i))/2;
conY(i) = (Y(i+1)+Y(i))/2;
S(i) = sqrt((X(i+1)-X(i))^2 + (Y(i+1)-Y(i))^2);
end
close all
plot(R*cos(0:0.01:2*pi),R*sin(0:0.01:2*pi),'b', X,Y,'r',conX,conY,'g^');
axis equal; legend('Exact shape','Panel approximation','Control points')
xlabel('x, m'); ylabel('y, m'); title('Fig. 1 Panel layout (n = 8, R = 1m)');
%*************************************************************************%
%ADDING LABELS BY PLOTTING LABELS ALONG CIRCLE%
%*************************************************************************%
Radius = 0.8;
Number_Of_Data_Points = 9;
theta = linspace(0,2*pi,Number_Of_Data_Points);
X_Circle = Radius*cos(theta);
X_Circle = X_Circle(1:end-1);
Y_Circle = Radius*sin(theta);
Y_Circle = Y_Circle(1:end-1);
X_Circle = flip(circshift(X_Circle,3));
Y_Circle = flip(circshift(Y_Circle,3));
for Point_Index = 1: numel(conX)
X_Displacement = X_Circle(Point_Index);
Y_Displacement = Y_Circle(Point_Index);
text(X_Displacement,Y_Displacement,num2str(Point_Index),'HorizontalAlignment','center','fontsize',20);
end
To Plot on Control Points:
%*************************************************************************%
%ADDING LABELS BY PLOTTING LABELS ALONG CONTROL POINTS%
%*************************************************************************%
for Point_Index = 1: numel(conX)
text(conX(Point_Index),conY(Point_Index),num2str(Point_Index),'HorizontalAlignment','center','fontsize',20);
end

How to plot a circle in Matlab? (least square)

I am trying to plot a circle's equation-regression on x and y, but I do not know how to proceed. Any suggestions? (I want a circle to connect the points thru least square solution)
x = [5; 4; -1; 1];
y = [3; 5; 2; 1];
% circle's equation: x^2+y^2 = 2xc1+2yc2+c3
a = [2.*x,2.*y,ones(n,3)]
b = [x.^2 + y.^2];
c = a\b;
How do I plot the circle after this
There are a couple of ways how to plot a circle in matlab:
plot a line where the data points form a circle
use the 'o' marker in plot and the 'MarkerSize' name-value pair to set the radius of the circle
you can plot a circle image using the vscircle function
In your case, I would go with the first option, since you maintain in control of the circle size.
use the rectangle(...,'Curvature',[1 1]) function [EDITED: thx to #Cris Luengo]
So here is a plotting function
function circle(x,y,r)
%x and y are the coordinates of the center of the circle
%r is the radius of the circle
%0.01 is the angle step, bigger values will draw the circle faster but
%you might notice imperfections (not very smooth)
ang=0:0.01:2*pi+.01;
xp=r*cos(ang);
yp=r*sin(ang);
plot(x+xp,y+yp);
end
So with your (corrected) code, it looks like this
x = [5; 4; -1; 1];
y = [3; 5; 2; 1];
% circle's equation: x^2+y^2 = 2xc1+2yc2+c3
a = [2.*x,2.*y,ones(length(x),1)];
b = [x.^2 + y.^2];
c = a\b;
x_m = c(1)/2;
y_m = c(2)/2;
r = sqrt(x_m^2 + y_m^2 -c(3));
% plot data points
plot(x,y,'o')
hold on
% plot center
plot(x_m,y_m,'+')
% plot circle
circle(x_m,y_m,r)
hold off

Area between circular curves

I'm trying to calculate the surface between two circular curves (yellow surface in this picture as simplification) but I'm somehow stuck since I don't have datapoints at the same angular values of the two curves. Any ideas?
Thanks for your help!
Picture:
I assume you have the x,y coordinates which you used to the plot. I obtained them here using imfreehand. I used inpolygon to generate a binary mask for each curve and then apply xor on them to get a mask of the desired area:
% x,y were obtained using imfreehand on 100x100 image and getPosition()
x = [21;22;22;22;22;22;22;23;23;23;23;23;23;24;25;25;26;26;27;28;29;30;30;31;32;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71;72;73;74;75;76;77;78;79;79;80;80;81;81;81;82;82;82;82;83;83;83;84;84;85;85;86;86;86;86;86;86;85;84;84;83;82;81;80;79;78;77;76;75;74;73;72;71;70;69;68;67;66;65;64;63;62;61;60;59;58;57;56;55;54;53;52;51;50;49;48;47;46;45;44;43;42;41;40;39;38;37;36;35;34;33;32;31;30;29;28;27;26;25;25;24;24;23;22;21;21;21;21;21;21;21;21;21;21;21;21;21];
y = [44;43;42;41;40;39;38;37;36;35;34;33;32;31;30;29;28;27;26;25;24;23;22;21;20;19;18;18;17;17;17;17;17;16;16;16;16;16;16;15;15;14;14;14;14;14;14;15;15;15;16;16;17;17;17;17;18;18;18;19;20;20;21;22;23;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;56;57;57;58;59;59;60;61;61;61;61;61;60;60;60;59;58;57;56;56;55;55;54;54;54;54;54;54;54;54;54;55;55;55;55;56;57;58;59;60;61;61;62;63;63;64;64;65;65;66;66;66;66;66;66;65;64;63;62;61;60;59;58;57;56;55;54;53;52;51;50;49;48;47;46;45;44];
% generate arbitrary xy
x1 = (x - 50)./10; y1 = (y - 50)./10;
x2 = (x - 50)./10; y2 = (y - 40)./10;
% generate binary masks using poly2mask
pixelSize = 0.01; % resolution
xx = min([x1(:);x2(:)]):pixelSize:max([x1(:);x2(:)]);
yy = min([y1(:);y2(:)]):pixelSize:max([y1(:);y2(:)]);
[xg,yg] = meshgrid(xx,yy);
mask1 = inpolygon(xg,yg,x1,y1);
mask2 = inpolygon(xg,yg,x2,y2);
% add both masks (now their common area pixels equal 2)
combinedMask = mask1 + mask2;
% XOR on both of them
xorMask = xor(mask1,mask2);
% compute mask area in units (rather than pixels)
Area = bwarea(xorMask)*pixelSize^2;
% plot
subplot(131);
plot(x1,y1,x2,y2,'LineWidth',2);
title('Curves');
axis square
set(gca,'YDir','reverse');
subplot(132);
imshow(combinedMask,[]);
title('Combined Mask');
subplot(133);
imshow(xorMask,[]);
title(['XNOR Mask, Area = ' num2str(Area)]);
function area = area_between_curves(initial,corrected)
interval = 0.1;
x = -80:interval:80;
y = -80:interval:80;
[X,Y] = meshgrid(x,y);
in_initial = inpolygon(X,Y,initial(:,1),initial(:,2));
in_corrected = inpolygon(X,Y,corrected(:,1),corrected(:,2));
in_area = xor(in_initial,in_corrected);
area = interval^2*nnz(in_area);
% visualization
figure
hold on
plot(X(in_area),Y(in_area),'r.')
plot(X(~in_area),Y(~in_area),'b.')
end
If I use the lines of the question, this is the result:
area = 1.989710000000001e+03

Matlab join 2 arcs

I am trying to draw a shape using matlab. Here I need to join arcs. I created arc using following code
circr = #(radius,rad_ang) [radius*cos(rad_ang); radius*sin(rad_ang)]; % Circle Function For Angles In Radians
N = 25; % Number Of Points In Complete Circle
r_angl = linspace(pi/2, 5*pi/4, N); % Angle Defining Arc Segment (radians)
radius = 0.5; % Arc Radius
xy_r = circr(radius,r_angl); % Matrix (2xN) Of (x,y) Coordinates
figure(1)
plot(xy_r(1,:), xy_r(2,:))
axis([-1.25*radius 1.25*radius 0 1.25*radius])
Code generates following output
Now I modified code to draw other arc
circr = #(radius,rad_ang) [radius*cos(rad_ang); radius*sin(rad_ang)]; % Circle Function For Angles In Radians
N = 25; % Number Of Points In Complete Circle
r_angl = linspace(pi/2, 5*pi/4, N); % Angle Defining Arc Segment (radians)
radius = 0.5; % Arc Radius
xy_r = circr(radius,r_angl); % Matrix (2xN) Of (x,y) Coordinates
r_angl1 = linspace(4*pi/4,pi/4); % Angle Defining Arc Segment (radians)
radius1 = 0.1; % Arc Radius
xy_r1 = circr(radius1,r_angl1); % Matrix (2xN) Of (x,y) Coordinates
figure(1)
plot(xy_r(1,:), xy_r(2,:),xy_r1(2,:),xy_r1(1,:))
axis([-1.25*radius 1.25*radius 0 1.25*radius]) % Set Axis Limits
axis equal
This code generates
How can I join both arcs? I need to join them using their end points
Inserting the following lines will joint the end points with lines
hold on;
line([xy_r(1,1) xy_r1(1,end)], [xy_r(2,1) xy_r1(2,end)]);
line([xy_r(1,end) xy_r1(2,1)], [xy_r(2,end) xy_r1(1,1)]);
Together:
figure(1)
plot(xy_r(1,:), xy_r(2,:),xy_r1(2,:),xy_r1(1,:))
hold on;
line([xy_r(1,1) xy_r1(1,end)], [xy_r(2,1) xy_r1(2,end)]);
line([xy_r(1,end) xy_r1(2,1)], [xy_r(2,end) xy_r1(1,1)]);
axis([-1.25*radius 1.25*radius 0 1.25*radius]) % Set Axis Limits
axis equal

Packing Circles inside a Circle

Given unit circle, and a set of M smaller circles of radius r. Find the maximum radius of the smaller circles that allows them all to fit inside the unit circle without overlap.
I have the following circles packing in polygon example link
I want to change equations that say that all circles are inside the polygon
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 );
to equations that say that all circles are inside the circle, but i don't know how. Can somebody help me?
Kind regards.
In your case, there are no "sides of a polygon" so there is no analogue to theta and you'll need to change every place theta is referenced, and all variables that reference theta (like phi).
Something like the following should work. I've just copy-pasted the code from your link, gotten rid of theta and phi, redefined cdist and polyEq, and made it plot a unit circle in the answer instead of a polygon. Please ask if any of these choices are unclear.
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)]);