While loop doesn't stop
显示 更早的评论
Hey I've created the code described beneath but it doesn't run as planned. When I run it, the inner while loop 'while zonestop == 0' doesn't stop when it's condition isn't met anylonger? I can see that the condition is met in the command window.
Any help would be appreciated :)
%Debugging
clear clc %restarter alle kommandoer i Command window %alt køres ind med mm, N og MPa
% trinproces er 0,70 mm og stempeldiameteren er 110 mm
r = zeros(70,4); %preallocating = skrives for at lave hele matricen fra start, så det går hurtigere t = zeros(70,4); %; skrives for at matricen ikke skrives ud epsilontheta = zeros(70,4); epsilonn = zeros(70,4); epsilonr = zeros(70,4); den = zeros(70,4); det = zeros(70,4); der = zeros(70,4); epsilons = zeros(70,4); sigmas = zeros(70,4); sigman = 2*ones(70,4);%den første betingelse gør at sigman ikke må være 0 indledningsvis sigmar = zeros(70,4); k = zeros(70,4); l = zeros(70,4); A = zeros(70,1); F = zeros(29,1);
for i = 1:70 r0=110;%mm t0=1; %mm R=1.70; K=530; %N/mm^2 = MPa n=0.268; my=0.2;
for j =1:4
r(1,j)=r0-0.5*(j-1);
r(i,1)=r0-1*(i-1);
end
%j = 1
t(1,1)=t0;
t(i,1)=t(1,1);
epsilonn(i,1)=0;
epsilontheta(i,1)=0;
epsilonr(i,1)=0;
epsilons(i,1)=0;
sigmas(i,1)=0;
k(i,1)=0;
l(i,1)=0;
sigmar(i,1)=0;
sigman(i,1)=0;
F(i,1)=0;
end
j=2;
i = 1;
done = 0;
dt = 0;
zonestop = 0;
while j <= 4
while done == 0 %når denne løkke er færdig j=j+1
while zonestop == 0
if j ~= 1
t(i,j)=t(i,j-1)+dt; %startantagelse af tykkelsen, dt ændrer sig
if i ~=70 %tykkelsen i hvert procestrin er ens
t(i+1,j)=t(i,j-1)+dt;
tic; r(i+1,j)=sqrt((2*((r(i,j))^2*(t(i,j)+t(i+1,j))/(2)-((r(i,1))^2-(r(i+1,1))^2)*t(i,1)))/(t(i,j)+t(i+1,j))); toc;
end
tic; epsilonn(i,j)=log(t(i,j)/t0); toc;
tic; epsilontheta(i,j)=log(r(i,j)/r(i,1)); toc;
tic; epsilonr(i,j)= -(epsilonn(i,j) + epsilontheta(i,j)); toc;
tic; den(i,j)=epsilonn(i,j) - epsilonn(i,j-1); toc;
tic; det(i,j)=epsilontheta(i,j) - epsilontheta(i,j-1); toc;
tic; der(i,j)=-(den(i,j)+det(i,j)); toc;
tic; epsilons(i,j)=(sqrt(R+1))^2/(R+1)*sqrt(R*(der(i,j)-det(i,j))^2+(der(i,j)-R*den(i,j))^2+(det(i,j)-R*den(i,j))^2); toc;
tic; sigmas(i,j)= K*(epsilons(i,j))^n;toc;
tic; k(i,j)= sigmas(i,j)/epsilons(i,j)*((1+R)/(1+2*R))*(2*det(i,j)+den(i,j)); toc;
%k=(sigmatheta(i,j)-sigmar(i,j))
tic; l(i,j)= sigmas(i,j)/epsilons(i,j)*((1+R)/(1+2*R))*(den(i,j)*(1+R)+det(i,j)); toc; %den(i,j) = 0
%l=(sigman(i,j)-sigmar(i,j))
sigmar(1,j) = 0;
if i ~= 70 %for sigmar = 2:70
if j~=1 %er allerede defineret
tic; sigmar(i+1,j)=(sigmar(i,j)+(0.5*((k(i+1,j))/(r(i+1,j))+(30*k(i,j))/(r(i,j)))+(my)/(t(i,j))*((l(i+1,j))+sigman(i,j)))*(r(i+1,j)-r(i,j)))/(1-(my)/(t(i,j))*(r(i+1,j)-r(i,j))); toc;
end
end
end
if j == 1 % der er et loop inden i loopet
sigman(i,1) = 0;
else
if i==1
tic; sigman(1,j)= l(1,j); toc; %ved i=1 er sigmar = 0
else
tic; sigman(i,j)=l(i,j)+sigmar(i,j); toc;
end
end
if sigman >= 0
zonestop = 1;
else
i = i+1;
zonestop = 0;
end
end %while zonestop == 0
i
Fanvendt = 2.8*10^4; %N
if i ~= 70 %50 radier, men 49 ringelementer
F(i,1)= sigman(i,j)*((r(i,j))^2-(r(i+1,j))^2)*pi;
end
if sqrt((sum(F))^2)-Fanvendt > 0.01*Fanvendt %istedet for numerisk tegn
dt = dt + 0.01;
done = 0;
else
done = 1;
end
end
j=j+1;
end
t
1 个评论
Jan
2011-11-23
Please use a proper code formatting as explained in the "Markup help" link on this page.
It would be smarter, if you post only the part of the code, which reproduces the problem.
Especially "tic; ...; toc;" is 1. a waste of computing time, because it doesn't display the results, and 2. a waste of the time of all readers of your question.
采纳的回答
更多回答(1 个)
Jan
2011-11-23
It is not helpful to include all lines in tic and toc. You cannot get a reliable time measurement by this method. Remember, that the JIT acclereation requires, that each line contains a single command only.
This test:
if sigman >= 0
is equivalent to:
if all(sigman >= 0)
if sigman is not a scalar. Is this the wanted behaviour? If not, it does not trigger the setting of zonestop = 1; and this would lead to your problem.
类别
在 帮助中心 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!