How to vary an element in a matrix?

2 次查看(过去 30 天)
I want to be able to vary a matrix element between a set of certain values. Below is my code:
% System parameters
k1=180; k2=50; k3=220;
m1=1.1; m2=3.4;
% Stiffness and mass matrices
K=[k1+k2,-k2;-k2,k2+k3];
M=[m1,0;0,m2];
% Eigenvalues and natural frequencies
lambda=eig(K,M);
omega_nf=sqrt(lambda); disp(sort(omega_nf))
However, I want k1 to vary between 0.5*180:1.5*180. I know this doesn't work due to the dimensions of the arrays not being consistent. How could I make this work? I want to plot the eigenvalues against the varying k1.
Thank you for any help, it is appreciated.

采纳的回答

Star Strider
Star Strider 2022-4-20
Use a for loop or arrayfun
% System parameters
k1=180 * linspace(0.5, 1.5, 9); % Define 'k1' As A Vector
k2=50;
k3=220;
m1=1.1; m2=3.4;
% Stiffness and mass matrices
K= @(k1) [k1+k2,-k2;-k2,k2+k3]; % Create Anonymoous Function
M=[m1,0;0,m2];
% Eigenvalues and natural frequencies
lambda=arrayfun(@(k1)eig(K(k1),M), k1, 'Unif',0); % Calculate 'lambda'
omega_nf=sqrt([lambda{:}]); disp(sort(omega_nf))
8.2530 8.4101 8.5106 8.5791 8.6283 8.6651 8.6936 8.7163 8.7348 11.7716 12.5063 13.2349 13.9444 14.6306 15.2929 15.9321 16.5497 17.1471
figure
plot(k1, omega_nf)
grid
.

更多回答(2 个)

Bruno Luong
Bruno Luong 2022-4-20
编辑:Bruno Luong 2022-4-20
If you have latest R2022a, use eig2 FEX (we are still waiting for TMW to implement pageeig)
% System parameters
k1=0.5*180:1.5*180;
k2=50; k3=220;
m1=1.1; m2=3.4;
% Stiffness and mass matrices
K1=reshape(k1,1,1,[]);
K=[K1+k2,0*K1-k2;0*K1-k2,0*K1+k2+k3];
M=[m1,0;0,m2];
% Eigenvalues and natural frequencies
lambda=eig2(pagemldivide(K,M));
omega_nf=sqrt(lambda);
plot(k1,sort(omega_nf)')
If not just do for-loop of k1
% System parameters
k1=0.5*180:1.5*180;
k2=50; k3=220;
m1=1.1; m2=3.4;
% Stiffness and mass matrices
omega_nf = zeros(2,length(k1));
for i=1:length(k1)
K=[k1(i)+k2,-k2;-k2,k2+k3];
M=[m1,0;0,m2];
% Eigenvalues and natural frequencies
lambda=eig(K,M);
omega_nf(:,i)=sqrt(lambda);
end
plot(k1,sort(omega_nf)')

Davide Masiello
Davide Masiello 2022-4-20
clear,clc
k1 = 0.5*180:1.5*180;
k2 = 50;
k3 = 220;
m1 = 1.1;
m2 = 3.4;
omega_nf = zeros(2,length(k1));
for idx = 1:length(k1)
K = [k1(idx)+k2,-k2;-k2,k2+k3];
M = [m1,0;0,m2];
lambda = eig(K,M);
omega_nf(:,idx) = sqrt(lambda);
end
plot(k1,omega_nf)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by