How to vary an element in a matrix?

2 visualizaciones (últimos 30 días)
Reed Smith
Reed Smith el 20 de Abr. de 2022
Respondida: Star Strider el 20 de Abr. de 2022
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.

Respuesta aceptada

Star Strider
Star Strider el 20 de Abr. de 2022
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
.

Más respuestas (2)

Bruno Luong
Bruno Luong el 20 de Abr. de 2022
Editada: Bruno Luong el 20 de Abr. de 2022
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 el 20 de Abr. de 2022
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)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by