For loop not running...?
显示 更早的评论
I have a simple program with two for loops (not nested). The first for loop in the script runs fine, but the second doesn't run. If I remove the first for loop everything works fine. Any idea what might be causing this?
%%Constants
% E-glass/Epoxy
E1 = 41000; % MPa
E2 = 10400; % MPa
G12 = 4300; % MPa
nu12 = 0.28;
nu21 = 0.06;
Vf = 0.55;
n = 3; % Aumber of plys
th = [-45 30 0]; % Ply angles
t = 0.002; % Thickness, m
%%Stiffness Matrix Calcs (A, B, D)
% Q_11, Q_12... same for all
Q11 = E1/(1-nu12*nu21);
Q22 = E2/(1-nu12*nu21);
Q12 = (nu21*E1)/(1-nu12*nu21);
Q21 = Q12;
Q66 = G12;
% Lamina Qs
for i = 1:n
m(i) = cosd(th(i));
n(i) = sind(th(i));
Qxx(i) = m(i)^4*Q11+n(i)^4*Q22+2*m(i)^2*n(i)^2*Q12+4*m(i)^2*n(i)^2*Q66;
Qyy(i) = n(i)^4*Q11+m(i)^4*Q22+2*m(i)^2*n(i)^2*Q12+4*m(i)^2*n(i)^2*Q66;
Qxy(i) = m(i)^2*n(i)^2*Q11+m(i)^2*n(i)^2*Q22+(m(i)^4+n(i)^4)*Q12...
-4*m(i)^2*n(i)^2*Q66;
Qxs(i) = m(i)^3*n(i)*Q11-m(i)*n(i)^3*Q22-m(i)*n(i)*(m(i)^2-n(i)^2)...
*Q12-2*m(i)*n(i)*(m(i)^2-n(i)^2)*Q66;
Qys(i) = m(i)*n(i)^3*Q11-m(i)^3*n(i)*Q22+m(i)*n(i)*(m(i)^2-n(i)^2)...
*Q12+2*m(i)*n(i)*(m(i)^2-n(i)^2)*Q66;
Qss(i) = m(i)^2*n(i)^2*Q11+m(i)^2*n(i)^2*Q22-2*m(i)^2*n(i)^2*Q12...
+(m(i)^2+n(i)^2)^2*Q66;
end
% Group into matrix
Q = [Qxx; Qyy; Qxy; Qxs; Qys; Qss];
A = zeros(3,3);
B = zeros(3,3);
C = zeros(3,3);
z = t; % all layers have same thickness
% Q value summations
for j = 1:6
for k = 1:n
k
end
end
回答(1 个)
Star Strider
2017-11-29
The ‘j’ loop runs. The ‘k’ loop does not run because in the ‘i’ loop you define ‘n’ as:
n(i) = sind(th(i));
The sine is always going to be bounded by ±1, so the ‘k’ loop index is complete at the outset, and so does not iterate further (that here means does not iterate at all). The solution is to choose a different loop index limit (other than ‘n’ or any other variable you have previously defined or calculated, so that the variable does not interfere with anything you have previously calculated, overwriting it).
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!