How to vary an element in a matrix?
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 20 Apr 2022
% 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 Comments
More Answers (2)
Bruno Luong
on 20 Apr 2022
Edited: Bruno Luong
on 20 Apr 2022
% 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 Comments
Davide Masiello
on 20 Apr 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)
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!