Changing a value in a while loop
Afficher commentaires plus anciens
Hey
I got great useful answers last time I asked so here I go again: As I run the code my value 'dt' doesn't change eventhough the condition 'if sum(F,j)-Fanvendt > 0.01*Fanvendt' is fulfilled - I've checked in the command window.
Thanks in advance.
%Debugging
clear clc %restarter alle kommandoer i Command window %alt køres ind med mm, N og MPa
% trinproces er 0,150 mm og stempeldiameteren er 110 mm
r = zeros(150,20); %preallocating = skrives for at lave hele matricen fra start, så det går hurtigere t = zeros(150,20); %; skrives for at matricen ikke skrives ud epsilontheta = zeros(150,20); epsilonn = zeros(150,20); epsilonr = zeros(150,20); den = zeros(150,20); det = zeros(150,20); der = zeros(150,20); epsilons = zeros(150,20); sigmas = zeros(150,20); sigman = 2*ones(150,20);%den første betingelse gør at sigman ikke må være 0 indledningsvis sigmar = zeros(150,20); k = zeros(150,20); l = zeros(150,20); A = zeros(150,20); F = zeros(150,20);
for i = 1:150 r0=110;%mm t0=1; %mm R=1.150; K=530; %N/mm^2 = MPa n=0.268; my=0.2;
for j =1:20
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;
processtop = 0;
dt = 0;
zonestop = 0;
processkift = 0;
while processkift == 0 %næste procestrin while processtop == 0 %kraftligevægtskriterie while zonestop == 0 %antal ringelementer i if j ~= 1 t(i,j)=t0+dt; %startantagelse af tykkelsen, dt ændrer sig if i ~=150 %tykkelsen i hvert procestrin er ens t(i+1,j)=t(i,j)+dt;
r(i+1, j) = sqrt((2*(r(i, j)^2*((t(i, j)+t(i+1, j))*(1/2))-(r(i, 1)^2-r(i+1, 1)^2)*t(i, 1)))/(t(i, j)+t(i+1, j)));
end
epsilonn(i,j)=log(t(i,j)/t0);
epsilontheta(i,j)=log(r(i,j)/r(i,1));
epsilonr(i,j)= -(epsilonn(i,j) + epsilontheta(i,j));
den(i,j)=epsilonn(i,j) - epsilonn(i,j-1);
det(i,j)=epsilontheta(i,j) - epsilontheta(i,j-1);
der(i,j)=-(den(i,j)+det(i,j));
epsilons(i, j) = sqrt(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)/(2*R+1);
sigmas(i,j)= K*(epsilons(i,j))^n;
k(i,j)= (sigmas(i,j)/epsilons(i,j))*((1+R)/(1+2*R))*(2*det(i,j)+den(i,j));
%k=(sigmatheta(i,j)-sigmar(i,j))
l(i,j)= (sigmas(i,j)/epsilons(i,j))*((1+R)/(1+2*R))*(den(i,j)*(1+R)+det(i,j)); %den(i,j) = 0
%l=(sigman(i,j)-sigmar(i,j))
sigmar(1,j) = 0;
if i ~= 150 %for sigmar = 2:150
if j~=1 %er allerede defineret
sigmar(i+1, j) = (sigmar(i, j)+(1/2*(k(i+1, j)/r(i+1, j)+k(i, j)/r(i, j))+my*(l(i+1, j)+sigman(i, j))/t(i, j))*(r(i+1, j)-r(i, j)))/(1-my*(r(i+1, j)-r(i, j))/t(i, j));
end
end
end
if j == 1 % der er et loop inden i loopet
sigman(i,1) = 0;
else
if i==1
sigman(1,j)= l(1,j); %ved i=1 er sigmar = 0
else
sigman(i,j)=l(i,j)+sigmar(i,j);
end
end
if j ~= 1
if sigman(i,j) >= 0
zonestop = 1
else
i = i + 1;
zonestop = 0;
end
end
end %while zonestop == 0
if zonestop >= 1
Fanvendt = 2.8*10^4; %N
%if i ~= 150 %50 radier, men 49 ringelementer
for i=1:i-1 %alle ringelementer frem til radius r(i,j)
A(i,j)=((r(i,j))^2-(r(i+1,j))^2)*pi;
F(i,j)= sqrt((sigman(i,j))^2)*A(i,j);
end
%end
if sum(F(:,j)) - Fanvendt > 0.01*Fanvendt %istedet for numerisk tegn
dt = dt + 0.01
zonestop = 0; %geninitialiserer zonestop
processtop = 0;
i = 1; %starter forfra ved i = 1
else
processtop = 1
end
end % if zonestop >= 1
end %while processtop == 0
if processtop >= 1
j = j+1
%restarter alt undt. j
zonestop = 0;
processtop = 0;
i = 1;
dt = 0;
end
if j > 20
processkift = 1 %terminerer alt
end
end %while processkift = 0
1 commentaire
Jan
le 24 Nov 2011
And again I ask you for a proper code formatting. Please take the time to read the "Markup help" link on this page. Currently your code has a low readability only.
The TIC/TOC around single lines are still useless and increase the confusion level only.
We cannot run this code, because lines like "*dt = 0;*" are not valid. Such methods increase the difficulties to answer your question.
Réponse acceptée
Plus de réponses (1)
Jan
le 24 Nov 2011
In the current form your code is very hard to debug. There are a lot of useless commands (TIC/TOC), the indentation is not clean, the comments are not in English, and including lines in stars prevents us from running your code.
Therefore I suggest to cleanup your code at first. An then set a breakpoint in the the line
if sum(F,j)-Fanvendt > 0.01*Fanvendt
and use the debugger to find the problem by your own. This is much more efficient than asking this forum.
1 commentaire
Linda
le 24 Nov 2011
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!