Why is an extra line being plotted?

38 vues (au cours des 30 derniers jours)
Joshua Tsui
Joshua Tsui le 12 Mar 2021
Commenté : Alan Stevens le 12 Mar 2021
Hello,
I have been trying to display the deflection and displacement of beams within a building. Currently, I am having an issue from within my loop which plots the displacement of each floor.
As you can see from the plot below, I have a red horizontal line from (0,0) to(2,0). Why is this? I think I have done something wrong in my "Displacement" loop but I am not sure
numFlr=2
Phi_EigVec=[1 3;2 -3;]
y1=zeros(numFlr,1);
y2=zeros(numFlr,1);
x1=zeros(numFlr,1);
x2=Phi_EigVec(:,1);
%Displacement
hold on
for i=1:numFlr
y1(i)=i
y2(i)=i
A = [x1(:) x2(:)]; B = [y1(:) y2(:)];
plot(A.',B.','r')
end
%Beam Deflection
AZero=[0]
BZero=[0]
AEven=A(:,2:2:end);
BEven=B(:,2:2:end);
AFull=[AZero,AEven']
BFull=[BZero,BEven']
plot(AFull.',BFull.','g')
%Vertical Line
y=numFlr
line([0,0],[0,y])
%Axis
axis([-5 5 -1 5])
  3 commentaires
Joshua Tsui
Joshua Tsui le 12 Mar 2021
Thanks Adam. I thought I was creating coordinates this way. Would you mind giving me some more insight as to how I can improve? I think my understanding is a bit flawed.
Adam Danz
Adam Danz le 12 Mar 2021
Modifié(e) : Adam Danz le 12 Mar 2021
You just need to move the two commented lines outside of the loop. Alan Stevens's answer does this (though, no need for the 2nd "hold on").
hold on
for i=1:numFlr
y1(i)=i
y2(i)=i
% A = [x1(:) x2(:)]; B = [y1(:) y2(:)];
% plot(A.',B.','r')
end
UPDATE
Actually, you don't need the loop at all,
y1 = 1:numFlr;
y2 = y1;
A = [x1(:) x2(:)]; B = [y1(:) y2(:)];
hold on
plot(A.',B.','r')

Connectez-vous pour commenter.

Réponse acceptée

Alan Stevens
Alan Stevens le 12 Mar 2021
More like this, I think:
numFlr=2;
Phi_EigVec=[1 3;2 -3;];
y1=zeros(numFlr,1);
y2=zeros(numFlr,1);
x1=zeros(numFlr,1);
x2=Phi_EigVec(:,1);
%Displacement
hold on
for i=1:numFlr
y1(i)=i;
y2(i)=i;
end
A = [x1(:) x2(:)]; B = [y1(:) y2(:)];
plot(A(1,:),B(1,:),'r',A(2,:),B(2,:),'r')
hold on
%Beam Deflection
AZero=[0];
BZero=[0];
AEven=A(:,2:2:end);
BEven=B(:,2:2:end);
AFull=[AZero,AEven'];
BFull=[BZero,BEven'];
plot(AFull.',BFull.','g')
%Vertical Line
y=numFlr;
line([0,0],[0,y])
%Axis
axis([-5 5 -1 5])
  3 commentaires
Alan Stevens
Alan Stevens le 12 Mar 2021
Extend the plot command
plot(A(1,:),B(1,:),'r',A(2,:),B(2,:),'r',A(3,:),B(3,:),'r')
Alan Stevens
Alan Stevens le 12 Mar 2021
More generally
for i = 1:numFlr
plot(A(i,:),B(i,:),'r')
end

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by