Eigenvalue output in different order to inputs

11 ビュー (過去 30 日間)
Joshua Tsui
Joshua Tsui 2021 年 4 月 11 日
コメント済み: Joshua Tsui 2021 年 4 月 11 日
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().

採用された回答

Matt J
Matt J 2021 年 4 月 11 日
Omega2=sort(eig(K,M));
...
[Vec,Val,Vec2]=eig(A);
[~,ind]=sort(diag(Val))
Vec=Vec(:,ind);
Val=Val(:,ind);
Vec2=Vec2(:,ind);
  3 件のコメント
Joshua Tsui
Joshua Tsui 2021 年 4 月 11 日
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 2021 年 4 月 11 日
Actually, I believe this may have solved the problem due to a previous line of code giving me access to values of Val.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by