# How can I create a loop and plot a graph after each loop?

5 views (last 30 days)
Zeyad Elsayed on 14 Jun 2019
Commented: Zeyad Elsayed on 14 Jun 2019
Hello everyone,
I have two questions to do with the same code. So I am going to breakdown the questions below:
1. How can I create a for loop that uses a value of beta in each loop?
2. As q is a matrix (2 by 1), how can I plot the values of q1 and q2 separately, each against omega_n? (Also note that omega_n changes after each loop as beta changes)
NB: Just assume any values for the matrices below.
beta = 1:1:4; % Array of beta values
w = 0:50E3:300E3;
omega = w.*2.*pi./60; % Array of omega values
omega_n = 3*beta; % Omega_n is dependant on beta
M = [M_11 0; 0 M_22]; % Matrix M
C = [C_11 C_12; C_21 C_22]; % Matrix C
K = [K_11 K_12; K_21 K_22]; % Matrix K
F = [F_1; F_2]; % Matrix F
Z = -(omega.^2).*M + omega.*1i.*C + K; % Matrix Z
Z_inverse = inv(Z); % Inverse of Matrix Z
q = Z\F; % Matrix q

KALYAN ACHARJYA on 14 Jun 2019
Edited: KALYAN ACHARJYA on 14 Jun 2019
w = 0:50E3:300E3;
omega = w.*2.*pi./60; % Array of omega values
for beta=1:1:4
omega_n = 3*beta; % Omega_n is dependant on beta
M = [M_11 0; 0 M_22]; % Matrix M
C = [C_11 C_12; C_21 C_22]; % Matrix C
K = [K_11 K_12; K_21 K_22]; % Matrix K
F = [F_1; F_2]; % Matrix F
Z = -(omega.^2).*M + omega.*1i.*C + K; % Matrix Z
Z_inverse = inv(Z); % Inverse of Matrix Z
q= Z\F; % Matrix q
end
For second question clarification needed?
M_11..? M_22...
In each iteration omega_n is scalar (having single value),
Let suppose for first iteration beta=1, therefore omega_n=3
Lets consider q=[2
1] as you mentioned q is 2x1
Now can you elaborate how to do plot between omega_n vs q, I am asking Maths?
Zeyad Elsayed on 14 Jun 2019
w = 0:50E3:300E3;
omega = w.*2.*pi./60; % Array of omega values
M_22 = omega./4
C_22 = omega./3
for beta = (2 15 6 8)
omega_n = 3*beta; % Omega_n is dependant on beta
M = [1 0; 0 M_22]; % Matrix M
C = [1 2; -2 C_22]; % Matrix C
K = [100 0; 0 100]; % Matrix K
F = [200; 36]; % Matrix F
Z = -(omega.^2).*M + omega.*1i.*C + K; % Matrix Z
Z_inverse = inv(Z); % Inverse of Matrix Z
q= Z\F; % Matrix q
end
I just realized something else, that some of the values in the Matrix (e.g M_22 and C_22) entries are dependant on omega, meaning that another loop probably needs to be created. So for beta = 1st number, each omega needs to be considered at a time in the matrix itself, then the values of q get stored, then moves to beta = 2nd number.
Clarifying:
So yes for each iteration lets say omega_n=3 and also
lets say q = [2
1], then I want the value to get stored as q1 = 2 and q2 = 1, and two graphs would be created, q1 against omega_n and also q2 against omega_n.
Also note one more thing, treat beta as an array that was solved earlier, eg. beta = (2 15 6 8) - I changed it in the code above.
Thanks Kalyan!

R2018b

### Community Treasure Hunt

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

Start Hunting!