Plotting a circle and finding intersections

조회 수: 3 (최근 30일)
Atiqah Zakirah
Atiqah Zakirah 2017년 5월 23일
댓글: KSSV 2017년 5월 24일
I am trying to plot a circle using existing data I have and find the points where the circle intersect grid lines. I randomly picked a set of coordinates from the data to be used as the center point of the circle. However, the code I have isn't displaying the circle. When I run the code, it says there is an error in the line "plot(Q(1,:),Q(2,:),'.b')". Can anyone solve this problem? Thanks in advance!
nc = 1; % number of circle
r = 10; % radius
th = linspace(0,2*pi);
iwant = cell(nc,1);
for i = i:nc
% get center of circle
x = center(:,2); % x coordinate
y = center(:,3); % y coordinate
% circle coordinates
cx = x+r*cos(th);
cy = y+r*sin(th);
plot(cx,cy,'k');
% get intersection
Q = cell(1,[]) ;
count = 0 ;
% loop along lines parallel to x-axes
for j = 1:N
P = InterX([cx ;cy],[X(j,:) ; Y(j,:)]) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
% loop along lines parallel to y-axes
for k = 1:N
P = InterX([cx ;cy],[X(:,k)' ; Y(:,k)']) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
Q = cell2mat(Q) ;
plot(Q(1,:),Q(2,:),'.b')
iwant{i} = Q ;
end
  댓글 수: 3
Atiqah Zakirah
Atiqah Zakirah 2017년 5월 24일
Thank you for the help! I made some changes to the code. I have troubling integrating my code with the code you provided. Instead of getting random center of the circle, I'm getting random coordinates from my data set to be the center of my circle this time. When I make this change to the code, it presents me with an error.
Index exceeds matrix dimensions error: P = InterX([cx ;cy],[X(:,k)' ; Y(:,k)']) ; Error: plot(Q(1,:),Q(2,:),'g');
%%create grid
N = 30;
xgrid = linspace(103.6,104,N);
ygrid = linspace(1.25,1.5,N);
[X,Y] = meshgrid(xgrid,ygrid);
figure
hold on
plot(X,Y,'r');
plot(X',Y','r');
xlim([103.6 104])
ylim([1.25 1.5])
%%plot points
plot(coordinates(:,2),coordinates(:,3),'.');
plot (train(:,1),train(:,2),'x');
%%obtaining random coordinates
row = randi(size(coordinates,1),1); % generates random row number
% obtain x and y coordinates from selected row
center = coordinates(row,:);
%%create a circle using center
nc = 1; % number of circle
R = 30; % radius
th = linspace(0,2*pi);
iwant = cell(nc,1);
for i = i:nc
% get center of circle
x = center(:,2); % x coordinate
y = center(:,3); % y coordinate
% circle coordinates
cx = x+R*cos(th);
cy = y+R*sin(th);
plot(cx,cy,'k');
% get intersection
Q = cell(1,[]) ;
count = 0 ;
% loop along lines parallel to x-axes
for j = 1:N
P = InterX([cx ;cy],[X(j,:) ; Y(j,:)]) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
% loop along lines parallel to y-axes
for k = 1:N
P = InterX([cx ;cy],[X(:,k)' ; Y(:,k)']) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
Q = cell2mat(Q) ;
plot(Q(1,:),Q(2,:),'g');
iwant{i} = Q ;
end
KSSV
KSSV 2017년 5월 24일
coordinates and train are missing..

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by