How to vary an element in a matrix?

조회 수: 2 (최근 30일)
Reed Smith
Reed Smith 2022년 4월 20일
답변: Star Strider 2022년 4월 20일
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