# Condition if on the elements of matrix in two For loop doesn't work

3 views (last 30 days)
Dear all, I posted my problem there is some days ago but I'didn't recieve a right answer. In my code, I use two for loops with some conditions on the elements of the matrix. thre is no error using two loops and the size of different parametrs and that of the matrix is right. But when I compare the rsults to that obtained using one for loop, I realised that the condition if doesn't work.
Any one has an idea!!!!
Thanks!!!

Azzi Abdelmalek on 21 Aug 2012
Edited: Azzi Abdelmalek on 21 Aug 2012
if i have understood whaat is your problem
1. you are using if elseif elseif ....
2. if the condition 1 is satisfied the other conditions will be skiped even they are true
3. instead using if elseif elseif ... use
if exp1
%do
end
if exp2
%do
end
Aziz, you are right !!! I tried the For loop on xxb for small range, I realized that if Loop doesn't work correctly and therfor the values of inter are wrong.

Jan on 21 Aug 2012
Edited: Jan on 21 Aug 2012
r=0:dr:5;
rr=r';
...
for l=1:length(xxb);
...
for m=1:length(rr)
if R<rr
Now rr is a vector, but R is a scalar. Then if R < rr is executed implicitly as
if all(R < rr) && ~isempty( R) && ~isempty(rr)
I guess you want something like if R < rr(m).
Jan on 21 Aug 2012
I repeat my suggestion to use the debugger by your own to find the cause of the differences. I still do not see a chance for us to distinguish correct from incorrect values, but you obviously have a method to do so.
And I recommend again to omit the useless but time consuming "clear all".

Edited: adam on 23 Aug 2012
Hello all, Jan, Aziz, Andrei,
Thank you very much for your help and suggestions. My code is all right now!! I could debug it.
I have Just one conditions couldn't make them work correctly. For abs(xxb(l))=R[xxb=R and xxb=-R] and rr(m)=0 , for which inter(m,l)=3/4pi. Where can I put this condition? Any idea!!!
Other way: inter(1,100)=inter(1,102)=3/4*pi.
Here below please you find my code:
{
xxb=-5:0.05:5;
dr=0.01;
r=0:dr:5;
rr=r';
ABDN1=ABDN';
w2=2.25;
R= 0.05;
for l=1:length(xxb);
xb=xxb(l);
for m=1:length(rr)
a(l)=2.*abs(xxb(l));
c(m,l)=(rr(m).^2)+xxb(l)^2-R^2;
xp(m,l)=c(m,l)./a(l);
yp(m,l)=R.^2-((((2.*c(m,l))-((a(l)).^2))./(2.*a(l))).^2);
if (a(l)==0);
xp(m,l)=0;
yp(m,l)=0;
end
if R<rr(m)
inter(m,l)=0;
intod(m,l)=ABDN1(m).*G(m)'.*rr(m).* inter(m,l);
elseif xb==0
inter(m,l)=2.*pi;
inter1(m,l)=inter(m,l)./(2.*pi);
% elseif(xb==R)&(rr(m)=0)
% inter(m,l)=(3/4).*pi;
% inter1(m,l)=inter(m,l)./(2.*pi);
%
% elseif(xb==-R)&(rr(m)=0)
% inter(m,l)=(3/4).*pi;
% inter1(m,l)=inter(m,l)./(2.*pi);
%
end
if(yp(m,l)>0);
inter(m,l)=2.*(atan((sqrt(yp(m,l)))./xp(m,l)));
inter1(m,l)=inter(m,l)./(2.*pi);
end
end
end
figure(9)
mesh(xxb,rr,inter)
A=inter;
Have you a good day
Andrei your code gives at xxb=0, inter= inter1=0 for all values of rr. therfore the condition xxb==0 for which inter=2.*pi doesn't ... Here below you find a code which give me the right values; but I don't know if is there another beautifull code more than that one
Thnk you very much for your Help!!!!and thank you very much andrei thank also Jan and Aziz Have you a good weekend : {
xxb0=-5;
pxxb=0.05;
xxbf=5;
xxb=xxb0:pxxb:xxbf;
dr=0.01;
r=0:dr:5;
rr=r';
ABDN1=ABDN';
w2=0.5625*4;
R= 0.05;
for l=1:length(xxb);
xb=xxb(l);
for m=1:length(rr)
a(l)=2.*abs(xxb(l));
c(m,l)=(rr(m).^2)+xxb(l)^2-R^2;
xp(m,l)=c(m,l)./a(l);
yp(m,l)=R.^2-((((2.*c(m,l))-((a(l)).^2))./(2.*a(l))).^2);
if (a(l)==0);
xp(m,l)=0;
yp(m,l)=0;
end
% end if R<rr(m) inter(m,l)=0; inter1(m,l)=inter(m,l)./(2.*pi);
intod(m,l)=2.*(dr./w2).*ABDN1(m).*G(m)'.*rr(m).* inter(m,l);
elseif xb==0
inter(m,l)=2.*pi;
inter1(m,l)=inter(m,l)./(2.*pi);
intod(m,l)=2.*(dr./w2).*ABDN1(m).*G(m)'.*rr(m).* inter(m,l);
% else(xb==R)&(rr(m)==0)
% inter(m,l)=(3/4).*pi;
% inter1(m,l)=inter(m,l)./(2.*pi);
% intod(m,l)=ABDN1(m).*G(m)'.*rr(m).* inter(m,l);
% elseif(xb==-R)&(rr(m)=0)
% inter(1,l00)=(3/4).*pi;
% inter1(m,l)=inter(m,l)./(2.*pi);
% intod(m,l)=ABDN1(m).*G(m)'.*rr(m).* inter(m,l);
end
if(yp(m,l)>0);
inter(m,l)=2.*(atan((sqrt(yp(m,l)))./xp(m,l)));
inter1(m,l)=inter(m,l)./(2.*pi);
intod(m,l)=2.*(dr./w2).*ABDN1(m).*G(m)'.*rr(m).* inter(m,l);
end
end
end
l0=round(((-R-xxb0)./pxxb)+1)
l1=round(((R-xxb0)./pxxb)+1)
for m=1;
for l=l0;
inter(m,l)=(3/4)*pi
inter1(m,l)=inter(m,l)./(2.*pi);
intod(m,l)=2.*(dr./w2).*ABDN1(m).*G(m)'.*rr(m).* inter(m,l);
end
end
for m=1;
for l=l1;
inter(m,l)=(3/4)*pi
inter1(m,l)=inter(m,l)./(2.*pi);
intod(m,l)=2.*(dr./w2).*ABDN1(m).*G(m)'.*rr(m).* inter(m,l);
end
end