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.
0 个评论
采纳的回答
Star Strider
2022-4-20
% 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))
figure
plot(k1, omega_nf)
grid
.
0 个评论
更多回答(2 个)
Bruno Luong
2022-4-20
编辑:Bruno Luong
2022-4-20
% 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)')
0 个评论
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!