Changing a value in a while loop

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
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.

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 24 Nov 2011

0 votes

sum(F,j) is the sum of the array F along the j'th dimension. This will result in a row vector when j is 1, a column vector when j is 2, and a 2D array when j is 3 or more.
You then compare that vector or array against a constant. The result will be a logical vector or array. You then have an "if" test against that logical vector or array. MATLAB defines "if" of a vector or array to be considered true only if all the entries in the vector or array are non-zero (true).
My guess is that you want
if sum(F(:,j)) - Fanvendt > 0.01*Fanvendt

1 commentaire

Linda
Linda le 24 Nov 2011
Good guess!
But do you then know why dt isn't added to all of the t's but only the two first? t for the same j should be the same? I have corrected the code above.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 24 Nov 2011

0 votes

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
Linda le 24 Nov 2011
I've cleaned the code and tried debugging. I placed a breakpoint at the 'if sum(F,j)-Fanvendt > 0.01*Fanvendt' and I triede going one step and the value of dt should change. And I wonder if it's because sum(F,j) isn't giving me what I want?
F is a matrix and I want Matlab to sum the j-array and subtract the value 28000 and then tell me if this is greater or smaller than 1% of 28000?

Connectez-vous pour commenter.

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!

Translated by