Overwriting within an if/else

Hey!
I've this code but I don't quite understand why at the first iteration j = 2 and when i = 60 the for loop at line overwrites the last input of t(:,1) - I already defined t(:,1) before initializing the if loop.
Any help would be appreciated.
clear clc
r=zeros(60,20); t=zeros(60,20); epsilontheta=zeros(60,20); epsilonn=zeros(60,20); epsilonr=zeros(60,20); den=zeros(60,20); der=zeros(60,20); det=zeros(60,20); epsilons=zeros(60,20); sigmas=zeros(60,20); sigman=(-1)*ones(60,20); sigmar=zeros(60,20); k=zeros(60,20); l=zeros(60,20); A=zeros(60,20); F=zeros(60,20);
for i =1:60 r0=110; %mm t0=1; %mm R=1.15; 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;
det(i,1)=0;
den(i,1)=0;
der(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.0001; zonestop = 0; processkift = 0;
while processkift == 0 %næste procestrin while processtop == 0 %kraftligevægtskriterie while zonestop == 0 %antal ringelementer if j ~= 1 t(i,j)=t0+dt; %startantagelse af tykkelse, dt ændrer sig t(:,j)=t(i,j);
if i ~=60
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(i,j) = 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));
%l(i,j) = sigman(i,j) - sigmar(i,j)
sigmar(1,j)=0;
if i ~=60
if i ~=1
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 %et loop inden i et loop - kunne nok gøres smartere
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
if zonestop >= 1
Fanvendt = 3,9*10^4 %N
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); %i stedet for numerisk tegn
end
if sum(F(:,j))-Fanvendt > 0.01*Fanvendt
dt = dt + 0.01;
zonestop = 0; %geninitialiserer zonestop
processtop = 0;
i = 1; %starter forfra ved i = 1
else
processtop = 1;
end
end
end
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

댓글 수: 2

Walter Roberson
Walter Roberson 2011년 12월 5일
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Jan
Jan 2011년 12월 5일
The command "clear clc" removes the function of variable called "clc" from the memory. Are you sure you want to do this?!

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2011년 12월 5일

0 개 추천

I suggest to use the debugger to step through your code line by line.
This will most likely not produce the expected result:
Fanvendt = 3,9*10^4 %N
Now Fanvendt is 3 and "9e4" appears in the command window. You need a dot instead of the comma. Calculating 10^4 and the multiplication is much more expensive than using "3.9e4" directly.
Sahdowing the function "det" by a variable with the same name might have unexpected effects.

댓글 수: 2

Linda
Linda 2011년 12월 5일
Arhh of course! But I don't understand what's wrong with the det-function?
Jan
Jan 2011년 12월 5일
"det" is a builtin function. You *can* shadow it by redefining it as a local variable. But this leads to problems frequently, as you can check in this forum. Example:
max = 1:5;
... Some other code, until you forget, that "max" was used before...
disp(max(rand(1, 100))); % ERROR!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Software Development에 대해 자세히 알아보기

질문:

2011년 12월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by