ERROR-The statement cannot be reached

33 次查看(过去 30 天)
theta = linspace(0,360,361);
theta_rad = theta*pi/180;
is_crossply = 0;
animate = 1;
an_speed = .001;
l=[-30 4*2*.0052 1;
60 4*2*.0052 1];
E = [19.4000001e6 1.4e6 .30 .702e6 -.5e-6 15e-6;
5.84e6 .9e6 .2 .3e6 0.0e-6 0.0e-6];
T1_theta = @(ang) [(cos(ang))^2 (sin(ang))^2 2*sin(ang)*cos(ang);
(sin(ang))^2 (cos(ang))^2 -2*sin(ang)*cos(ang);
-sin(ang)*cos(ang) sin(ang)*cos(ang) (cos(ang))^2-(sin(ang))^2 ];
if is_crossply
M = cosd(2*theta);
else
n = size(l,1);
h = zeros(n+1,1);
A = zeros(3);
B = zeros(3);
D = zeros(3);
D_0 = zeros(3);
D_90 = zeros(3);
D_top = zeros(3);
D_bot = zeros(3);
M = zeros(3,3,length(theta));
R = [ 1 0 0 ;
0 1 0 ;
0 0 2 ];
total = sum(l,1);
thick = total(1,2);
h(1) = -thick/2;
imax = n + 1;
for i = 2 : imax
h(i) = h(i-1) + l(i-1,2);
end
for j = 1:length(theta)
for i = 1:n
mi=l(i,3);
v21 = E(mi,2)*E(mi,3)/E(mi,1);
d = 1 - E(mi,3)*v21;
Q = [E(mi,1)/d v21*E(mi,1)/d 0;
E(mi,3)*E(mi,2)/d E(mi,2)/d 0;
0 0 E(mi,4)];
a1=l(i,1)*pi/180;
T1 = [(cos(a1))^2 (sin(a1))^2 2*sin(a1)*cos(a1);
(sin(a1))^2 (cos(a1))^2 -2*sin(a1)*cos(a1);
-sin(a1)*cos(a1) sin(a1)*cos(a1) (cos(a1))^2-(sin(a1))^2 ];
Qxy = inv(T1)*Q*R*T1*inv(R);
A = A + Qxy*(h(i+1)-h(i));
B = B + Qxy*(h(i+1)^2 - h(i)^2);
D = D + Qxy*(h(i+1)^3 - h(i)^3);
if i <= n/2
disp('comp D_top')
T1_top = T1_theta(-a1+theta_rad(j));
Q_top = inv(T1_top)*Q*R*T1_top*inv(R);
D_top = D_top + Q_top*(h(i+1)^3 - h(i)^3);
T1_90 = T1_theta(pi/2);
Q_90 = inv(T1_90)*Q*R*T1_90*inv(R);
D_90 = D_90 + Q_90*(h(i+1)^3 - h(i)^3);
T1_0 = T1_theta(0);
Q_0 = inv(T1_0)*Q*R*T1_0*inv(R);
D_0 = D_0 + Q_0*(h(i+1)^3 - h(i)^3);
elseif i > n/2
disp('comp D_bot')
T1_bot = T1_theta(-a1+theta_rad(j));
Q_bot = inv(T1_bot)*Q*R*T1_bot*inv(R);
D_bot = D_bot + Q_bot*(h(i+1)^3 - h(i)^3);
else
disp('ERROR h(i)=0')
end
end
format short e
A = 1.0*A;
B = .5*B;
D = (1/3)*D;
D_top = (1/3)*D_top;
D_bot = (1/3)*D_bot;
D_0 = (1/3)*D_0;
D_90 = (1/3)*D_90;
M(:,:,j) = (D_bot-D_top)./(D_0-D_90);
end
end
if is_crossply
posMi = find(M>=0);
negMi = find(M<=0);
posM = polar(theta_rad(posMi),M(posMi));
hold on;
negM = polar(theta_rad(negMi),M(negMi));
set(posM , ...'Color','r',...'LineWidth',2)
set(negM , ...'Color','b',...'LineWidth',2,...'LineStyle','--')
title('Bending Stiffness Mismatch, M')
hold off;
if animate
for i = 1:2:length(M)
subplot(1,2,1)
t = 0 : .01 : 2 * pi;
P = polar(t, 1 * ones(size(t)));
hold on;
anim = polar(theta_rad(i),M(i));
hold on;
[X,Y] = pol2cart(theta_rad(i),M(i));
str = [' \leftarrow ',num2str(M(i))];
Mtext = text(X,Y,str);
posM = polar(theta_rad(posMi),M(posMi));
hold on;
negM = polar(theta_rad(negMi),M(negMi));
hold on;
set(P, 'Visible', 'off')
set(anim,...'Marker','o',...'MarkerSize',10)
set(Mtext,...'FontSize',16)
set(posM , ...'Color','r',...'LineWidth',2)
set(negM , ...'Color','b',...'LineWidth',2,...'LineStyle','--')
title('Bending Stiffness Mismatch, M')
hold off;
subplot(1,2,2)
xdir = [1;0];
ydir = [0;1];
R = [ cos(theta_rad(i)) -sin(theta_rad(i)) ;
sin(theta_rad(i)) cos(theta_rad(i))];
xp = R*xdir;
yp = R*ydir;
xpaxis = plot([0 xp(1)],[0 xp(2)]);
hold on;
ypaxis = plot([0 yp(1)],[0 yp(2)]);
hold on;
xfixed = plot([0 1],[0 0]);
hold on;
yfixed = plot([0 0],[0 1]);
hold on;
xpaxis_text = text(xp(1),xp(2),' x''');
ypaxis_text = text(yp(1),yp(2),' y''');
xfixed_text = text(1,0,' x');
yfixed_text = text(0,1,' y');
angle_text = text(1,1,['\theta = ',num2str(theta(i))]);
set(xpaxis,...'LineWidth',2,...'Color','green')
set(ypaxis,...'LineWidth',2,...'Color','magenta')
set(xfixed,...'LineWidth',2,...'Color','black')
set(yfixed,...'LineWidth',2,...'Color','black')
axis([-1.5 1.5 -1.5 1.5])
axis square
axis off
title('Current Coordinate System')
hold off;
pause(an_speed)
end
end
end
if is_crossply ~= 1
Mrs = reshape(M(1,1,:),[1,length(theta)]);
t = 0 : .01 : 2 * pi;
P = polar(t, 1 * ones(size(t)));
hold on;
posMi = find(Mrs>=0);
negMi = find(Mrs<=0);
posM = polar(theta_rad(posMi),Mrs(posMi));
hold on;
negM = polar(theta_rad(negMi),Mrs(negMi));
hold on;
set(P, 'Visible', 'off')
set(posM , ...'Color','r',...'LineWidth',2)
set(negM , ...'Color','b',...'LineWidth',2,...'LineStyle','--')
title('Bending Stiffness Mismatch, M') \
hold off;
end
line 14 and 85 shows that the statement cannot be reached. Kindly help.
  2 个评论
Alex Mcaulley
Alex Mcaulley 2019-5-9
I guess it is just a warning, because they are inside an if 0:
if is_crossply %This variable is 0
%Some code that cannot be reached
end
Stephen23
Stephen23 2019-5-9
编辑:Stephen23 2019-5-9
@Pritam Ghosh: your code is very badly aligned. Badly aligned code is one way that beginners hide bugs in their code. You should align your code consistently, e.g. using the default setting of the MATLAB editor.
Note that your code has many syntax warnings which you should pay attention to. Do NOT ignore them!

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2019-5-9
编辑:Stephen23 2019-5-9
Those warnings are very simple to understand by aligning the code and looking at the values.
MATLAB warns you that lines 14 and 85 will never run, the reason is because you define the if condition to be zero:
is_crossply = 0;
...
if is_crossply
M = cosd(2*theta); % line 14
else
...
if is_crossply
posMi = find(M>=0); % line 85
....
If the condition is zero, how do you expect those lines to run?
  1 个评论
Steven Lord
Steven Lord 2019-5-9
FYI if you're writing this in the MATLAB Editor, those lines have small orange underlines (and there are orange lines in the right gutter) indicating there is an issue that may need to be corrected. Your code also has some red lines in the right gutter, indicating there is an issue that will need to be corrected for your code to run.
If you click on the line in the right gutter, the Editor will scroll to show the line where the issue is detected. Hover the mouse over the underlined code and a short description of the problem Code Analyzer has detected will appear. You can click the Details button in that message for more information, and the fifth bullet point in that additional information for this message describes why you receive that warning for this line.
"Controlled by a conditional expression that is coded such that it always returns a value of true or always returns a value of false."
See this documentation page for more information about Code Analyzer in the MATLAB Editor.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by