Why is an extra line being plotted?

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 Comments

Look at your A/B variables and recall that a line will be drawn for each column of inputs.
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.
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')

Sign in to comment.

 Accepted Answer

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 Comments

Joshua Tsui
Joshua Tsui on 12 Mar 2021
Edited: Joshua Tsui on 12 Mar 2021
Thanks. This answer works for 2 floors but not 3 or more.
For example:
numFlr=3
Phi_EigVec=[1 3;2 -3;3 4;];
A red line at the top is missing.
Is there a way to put this into a loop rather than mentioning the specific lines?
Extend the plot command
plot(A(1,:),B(1,:),'r',A(2,:),B(2,:),'r',A(3,:),B(3,:),'r')
More generally
for i = 1:numFlr
plot(A(i,:),B(i,:),'r')
end

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!