Matlab程序,为什么我的if条件下的for循环没能运行?
clc;clear;%函数作用显示满足条件的点%Alpha=linspace(-pi/6,pi/6,100);%创建一维数组Beta=linspace(-pi/6,pi/...
clc;clear;%函数作用显示满足条件的点%
Alpha=linspace(-pi/6,pi/6,100);%创建一维数组
Beta=linspace(-pi/6,pi/6,100);
ZP=linspace(0.10,0.40,100);
N=0;
for i=1:length(Alpha) %为何循环没有运行?
for j=1:length(Beta)
for k=1:length(ZP)
theta11=cos(Alpha(i))+sin(Beta(j))+1/2*ZP(k);
theta12=sin(Beta(j))+cos(Alpha(i))+ZP(k);
theta13=sin(Beta(j))*cos(Alpha(i))-ZP(k);
J=[cos(Alpha(i)) sin(Beta(j)) ZP(k)
sin(Beta(j)) cos(Alpha(i)) 1/2*ZP(k)
sin(Alpha(i))*cos(Beta(j)) cos(Alpha(i))*ZP(k)sin(Beta(j))*ZP(k)];
if (theta11>=pi/4)&&(theta11<=(3*pi)/4)&&(theta12>=pi/4)&&(theta12<=(3*pi)/4)&&(theta13>=pi/4)&&(theta13<=(3*pi)/4)&&(det(J)~=0);
disp(theta11);
disp(theta12);
disp(theta12);
disp(J);
N=N+1; %统计满足条件的点个数
figure(1);
plot3(Alpha(i),Beta(j),ZP(k));
hold on;
end
end
end
end
display(num2str(N));% num2str(N)将数值变量转化为字符串% 展开
Alpha=linspace(-pi/6,pi/6,100);%创建一维数组
Beta=linspace(-pi/6,pi/6,100);
ZP=linspace(0.10,0.40,100);
N=0;
for i=1:length(Alpha) %为何循环没有运行?
for j=1:length(Beta)
for k=1:length(ZP)
theta11=cos(Alpha(i))+sin(Beta(j))+1/2*ZP(k);
theta12=sin(Beta(j))+cos(Alpha(i))+ZP(k);
theta13=sin(Beta(j))*cos(Alpha(i))-ZP(k);
J=[cos(Alpha(i)) sin(Beta(j)) ZP(k)
sin(Beta(j)) cos(Alpha(i)) 1/2*ZP(k)
sin(Alpha(i))*cos(Beta(j)) cos(Alpha(i))*ZP(k)sin(Beta(j))*ZP(k)];
if (theta11>=pi/4)&&(theta11<=(3*pi)/4)&&(theta12>=pi/4)&&(theta12<=(3*pi)/4)&&(theta13>=pi/4)&&(theta13<=(3*pi)/4)&&(det(J)~=0);
disp(theta11);
disp(theta12);
disp(theta12);
disp(J);
N=N+1; %统计满足条件的点个数
figure(1);
plot3(Alpha(i),Beta(j),ZP(k));
hold on;
end
end
end
end
display(num2str(N));% num2str(N)将数值变量转化为字符串% 展开
1个回答
展开全部
首先,for循环一直在运行,只是你的if一直没有运行,所以最后N还是零,为了证明,我加了几句代码,来验证循环次数
clc;clear;%函数作用显示满足条件的点%
Alpha=linspace(-pi/6,pi/6,100);%创建一维数组
Beta=linspace(-pi/6,pi/6,100);
ZP=linspace(0.10,0.40,100);
N=0;
M=0;
P=0;
Q=0;
for i=1:length(Alpha) %为何循环没有运行?
for j=1:length(Beta)
for k=1:length(ZP)
theta11=cos(Alpha(i))+sin(Beta(j))+1/2*ZP(k);
theta12=sin(Beta(j))+cos(Alpha(i))+ZP(k);
theta13=sin(Beta(j))*cos(Alpha(i))-ZP(k);
J=[cos(Alpha(i)) sin(Beta(j)) ZP(k) sin(Beta(j)) cos(Alpha(i)) 1/2*ZP(k) sin(Alpha(i))*cos(Beta(j)) cos(Alpha(i))*ZP(k) sin(Beta(j))*ZP(k)];
if (theta11>=pi/4)&&(theta11<=(3*pi)/4)&&(theta12>=pi/4)&&(theta12<=(3*pi)/4)&&(theta13>=pi/4)&&(theta13<=(3*pi)/4)&&(det(J)~=0);
disp(theta11);
disp(theta12);
disp(theta12);
disp(J);
N=N+1; %统计满足条件的点个数
figure(1);
plot3(Alpha(i),Beta(j),ZP(k));
hold on;
end
M=M+1;
end
P=P+1;
end
Q=Q+1;
end
display(num2str(N));% num2str(N)将数值变量转化为字符串%
display(num2str(M));
display(num2str(P));
display(num2str(Q));
结果:
0
1000000
10000
100
更多追问追答
追问
恩,我也试过了,不要if条件的时候,前面for是能运行的。但是问题是,为什么我的if不能运行,请问应该怎样修改,才能使if运行,谢谢啊。
追答
你的if条件是所有条件都满足时才运行,只要有一个不满足就不运行。我不知道你的目的是什么,所以不知道怎么修改
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询