finding the distance between two points
Afficher commentaires plus anciens
prompt = "enter the number of elements to make";
x = input(prompt);
M = 2;
N = x/M;
r = 1;
R = 2;
W = linspace(r,R,M);
C = linspace(0,2*pi,N);
[R,T] = meshgrid(W,C);
X = R.*cos(T);
Y = R.*sin(T);
[m,n] = size(X)
figure
set(gcf, 'color', 'w');
axis equal;
axis on;
hold on;
for i = 1:m
plot(X(i,:), Y(i,:), '-o');
end
for j=1:n
G = plot(X(:,j),Y(:,j), 'b', 'linewidth', 1);
end
fprintf('the x coordinates of the nodes are')
X
fprintf('the y coordinates of the nodes are')
Y
%distance b/w two nodes

How do I find the distance between two nodes? I need to find the length of each side of the sements created (4 lengths in total).
Réponses (3)
You can try the function: pdist2().
help pdist2
I am not exactly certain what you want.
Calculating the perimeters of each segment appears to be straightforward —
% prompt = "enter the number of elements to make";
% x = input(prompt);
x = 16;
M = 2;
N = x/M;
r = 1;
R = 2;
W = linspace(r,R,M);
C = linspace(0,2*pi,N);
[R,T] = meshgrid(W,C);
X = R.*cos(T);
Y = R.*sin(T);
[m,n] = size(X)
figure
set(gcf, 'color', 'w');
axis equal;
axis on;
hold on;
for i = 1:m
plot(X(i,:), Y(i,:), '-o');
end
for j=1:n
G = plot(X(:,j),Y(:,j), 'b', 'linewidth', 1);
end
fprintf('the x coordinates of the nodes are')
X
fprintf('the y coordinates of the nodes are')
Y
%distance b/w two nodes
dX = hypot(diff(X,[],2), diff(Y,[],2)); % Radial Distances
dY = hypot(diff(X), diff(Y)); % Circumferential Distances
dY = [dY(1,:); dY]; % Duplicate First Row
Perimeters = sum([2*dX dY],2) % Perimeters Of Each Segment
.
To identify the distances between the nodes of a segment you first need to identify the nodes that make up that segment. Once you have those coordinates the distance can be determined. See below for an example. I adjusted the figure a bit to highlight the segment and the node numbers.
x = 16; % user input
M = 2; % number or radii divisions
N = x/M; % number of angluar divisions
r = 1; % bound inner radius
R = 2; % bound outer radius
W = linspace(r,R,M); % radii for the plot
C = linspace(0,2*pi,N); % angluar segments for the plot
[R,T] = meshgrid(W,C); % combined radii (R) and angle segment (T)
X = R.*cos(T); % x coordinates for the points
Y = R.*sin(T); % y coordinates for the points
[m,n] = size(X);
% reshape the points to ease the plotting
% note that we add 'nan' to disconnect the different lines
X_circ = reshape([X;NaN(1,n)],[],1);
Y_circ = reshape([Y;NaN(1,n)],[],1);
X_seg = reshape([X,NaN(m,1)]',[],1);
Y_seg = reshape([Y,NaN(m,1)]',[],1);
% create the grid for the segment highlight
SegGrid = [X(1,1) Y(1,1);
X(1,2) Y(1,2);
X(2,2) Y(2,2);
X(2,1) Y(2,1)];
% evaluate the distance
Edge_12 = sqrt( (SegGrid(1,1)-SegGrid(2,1))^2 + (SegGrid(1,2)-SegGrid(2,2))^2);
Edge_23 = sqrt( (SegGrid(2,1)-SegGrid(3,1))^2 + (SegGrid(2,2)-SegGrid(3,2))^2);
Edge_34 = sqrt( (SegGrid(3,1)-SegGrid(4,1))^2 + (SegGrid(3,2)-SegGrid(4,2))^2);
Edge_41 = sqrt( (SegGrid(4,1)-SegGrid(1,1))^2 + (SegGrid(4,2)-SegGrid(1,2))^2);
figure
hold on
% plot the circles
plot(X_circ,Y_circ,'b','linewidth',1);
% plot the inner edges
plot(X_seg ,Y_seg ,'r','Marker','o');
% highlight the first segment in yellow
patch('Faces',[1 2 3 4],'Vertices',SegGrid,'FaceColor','y','FaceAlpha',0.5,'EdgeColor','none')
% print text to indicate the node number
text(SegGrid(:,1),SegGrid(:,2)," "+num2str((1:4)'))
hold off
axis equal
grid on
fprintf('Edge between nodes %i and %i has length %.3f \n',1,2,Edge_12)
fprintf('Edge between nodes %i and %i has length %.3f \n',2,3,Edge_23)
fprintf('Edge between nodes %i and %i has length %.3f \n',3,4,Edge_34)
fprintf('Edge between nodes %i and %i has length %.3f \n',4,1,Edge_41)
% EDIT: added the perimeter
SegPer = Edge_12+Edge_23+Edge_34+Edge_41;
fprintf('Perimeter of the segment is %.3f \n',SegPer)
Catégories
En savoir plus sur Environment and Clutter dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

