Eigenvalue output in different order to inputs

3 views (last 30 days)
Hi, This code is meant to plot the mode shapes of buildings. Is it possible to have the eigenvalues outputted in ascending order and have the eigen vectors follow this order too?
For example, if eigenvalues were: 2,3,1 ; I would like to be in the order 1,2,3. But also have the eigenvectors arranged in a manner that I know what eigenvector corresponds to each eigenvalue.
Here is my code below:
clear all
numFlr=input('Number of floors: ');
MVal=zeros(numFlr,1);
EVal=zeros(numFlr,1);
IVal=zeros(numFlr,1);
LVal=zeros(numFlr,1);
KVal=zeros(numFlr,1);
K=zeros(numFlr,1);
for i=1:numFlr
MVal(i)=input('Mass:');
EVal(i)=input('Elastic Modulus:');
IVal(i)=input('Second Moment of Area:');
LVal(i)=input('Length:');
end
for i=1:numFlr
KVal(i)=(24*EVal(i)*IVal(i))/(LVal(i)^3);
end
M=diag(MVal);
%stiffness matrix
for i=1:numFlr
for j=1:numFlr
try
if i==j
K(i,j)=(KVal(i)+KVal(i+1));
elseif j==i+1
K(i,j)=-KVal(j);
elseif i==j+1
K(i,j)=-KVal(i);
end
end
end
end
K(end,end)=KVal(end,:);
K=K
%eigenvalues and eigenvectors
Omega2=eig(K,M);
Omega=sqrt(Omega2);
A=inv(M)*K;
[Vec,Val,Vec2]=eig(A);
Omega2_EigVal=diag(Val)
EigVec=Vec
OmegaSpecial=sqrt(Omega2_EigVal)
%plotting arrays
Choose=input('Mode: ');
y1=zeros(numFlr,1);
y2=zeros(numFlr,1);
x1=zeros(numFlr,1);
x2=EigVec(:,Choose);
%Displacement
hold on
for i=1:numFlr
y1(i)=i;
y2(i)=i;
end
A = [x1(:) x2(:)]; B = [y1(:) y2(:)];
for i = 1:numFlr
plot(A(i,:),B(i,:),'r')
end
%Vertical Line
y=numFlr
line([0,0],[0,y])
%Axis
axis([-5 5 -1 5])
Examples of input values would be:
Number of floors: 5
Mass:2
Elastic Modulus:1
Second Moment of Area:1
Length:1
Mass:1
Elastic Modulus:1
Second Moment of Area:1
Length:1
Mass:2
Elastic Modulus:1
Second Moment of Area:1
Length:1
Mass:2
Elastic Modulus:1
Second Moment of Area:1
Length:1
Mass:1
Elastic Modulus:1
Second Moment of Area:1
Length:1
I was thinking of using sort but I am unsure how I could implement this when using eig().

Accepted Answer

Matt J
Matt J on 11 Apr 2021
Omega2=sort(eig(K,M));
...
[Vec,Val,Vec2]=eig(A);
[~,ind]=sort(diag(Val))
Vec=Vec(:,ind);
Val=Val(:,ind);
Vec2=Vec2(:,ind);
  3 Comments
Joshua Tsui
Joshua Tsui on 11 Apr 2021
Hi Matt. I have tried out your solution. For some reason, I am no longer getting a diagonal matrix for Val when applying your solution.
Joshua Tsui
Joshua Tsui on 11 Apr 2021
Actually, I believe this may have solved the problem due to a previous line of code giving me access to values of Val.

Sign in to comment.

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!