MATLAB: For loops for matrices

for loopMATLABmatricesmatrix

I am trying to link two equations, where I use a for loop calculate the value of k (wavenumber) from the range of frequencies (eg. 1-5 Hz) then substitute the values of k into a 6×6 matrix. Can anyone help show me how to create a matrix for each value of k in Matlab?
1st Equation
for f = 1:5; % Range of Frequencies (Hz)
f;
w = 2.*pi.*f; % Angular Frequency (Hz)
p = 8050;% Density of Mild Steel(kg/m^3)
v = 0.30; % Poissons Ratio of Mild Steel
R = 0.02; % Radius of Pipe (m)
E = 210*10^9; % Youngs Modulus of Mild Steel (pa)
a = (w.^2).*p;
b = (p.*(1-(v.^2)).*(R.^2).*(w.^2)-E);
c = (p.*(R.^2).*(w.^2)-E).*E;
k(f) = sqrt((a.*b)/c); % k = Wave Number
end
An example solution when f=1:5
f=1, k=0.0012
f=2, k=0.0025
f=3, k=0.0037
f=4, k=0.0049
f=5, k=0.0062
2nd Equation (6×6 Matrix)
k = **value from equation 1**
L1=0.1;
L2=0.6;
L3=0.6;
D= [0,0,exp(-k*L1),exp(-k*L2),0,0; exp(-k*L1),1,exp(-k*L1),exp(-k*L2),0,0; -k*exp(-k*L1),k,k*exp(-k*L1),-k*exp(-k*L2),0,0;0,0,exp(-k*(L1+L2)),k,-exp(-k*(L1+L2)),-exp(-k*L3);0,0,-k*exp(-k*(L1+L2)),1,k*exp(-k*(L1+L2)),k*exp(-k*L3);0,0,exp(-k*(L1+L2)),1,0,0]
An example of how the matrix looks using the 2nd equation is in the picture below:

Best Answer

It’s probably easiest to just set up a second loop (since ( don’t want to go through and subscript all the ‘k’-values in ‘D’:
for f = 1:5; % Range of Frequencies (Hz)
f;
w = 2.*pi.*f; % Angular Frequency (Hz)
p = 8050;% Density of Mild Steel(kg/m^3)
v = 0.30; % Poissons Ratio of Mild Steel
R = 0.02; % Radius of Pipe (m)
E = 210*10^9; % Youngs Modulus of Mild Steel (pa)
a = (w.^2).*p;
b = (p.*(1-(v.^2)).*(R.^2).*(w.^2)-E);
c = (p.*(R.^2).*(w.^2)-E).*E;
kv(f) = sqrt((a.*b)/c); % k = Wave Number
end
for f = 1:length(kv)
L1=0.1;
L2=0.6;
L3=0.6;
k = kv(f);
D(:,:,f) = [0,0,exp(-k*L1),exp(-k*L2),0,0; exp(-k*L1),1,exp(-k*L1),exp(-k*L2),0,0; -k*exp(-k*L1),k,k*exp(-k*L1),-k*exp(-k*L2),0,0;0,0,exp(-k*(L1+L2)),k,-exp(-k*(L1+L2)),-exp(-k*L3);0,0,-k*exp(-k*(L1+L2)),1,k*exp(-k*(L1+L2)),k*exp(-k*L3);0,0,exp(-k*(L1+L2)),1,0,0]
end
NOTE: I renamed ‘k’ in the first loop as ‘kv’, and used the renamed vector in the second loop.