Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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!!!!
Thank you in advance. Adam
  5 Commenti
Jan
Jan il 21 Ago 2012
Please, adam, omit the "clear all" because it is a frequently used, but always useless waste of time. If you are really convinced that a brute clearing helps you for any reasons, use "clear variables".

Accedi per commentare.

Risposte (3)

Azzi Abdelmalek
Azzi Abdelmalek il 21 Ago 2012
Modificato: Azzi Abdelmalek il 21 Ago 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
  12 Commenti
adam
adam il 21 Ago 2012
Modificato: adam il 21 Ago 2012
Here is the code: {
clear variables
xxb=-5:0.05:5;
dr=0.01;
r=0:dr:5;
rr=r';
R= 0.05;
for l=1:numel(xxb);
xb=xxb(l);
for m=1:numel(rr)
a(l)=2.*xxb(l);
c(m,l)=xxb(l).^2+rr(m).^2-R^2;
d=a(l);
% 1) Condition to calculate Xp :
if (a(l)<0)&(a(l)>0)
xp(m,l) =c(m,l)./a(l);
else
xp(m,l)=0;
end
% 2) Condition to calculate yp :
if (a<0) & (a>0)
yp(m,l)=R^2-((((2.*c(m,l))-(a(l)^2))./(2.*a(l))).^2);
else
yp(m,l)=0;
end
% 3) Condition to calculate inter :
if a(l)==0
if R<rr(m)
Inter(m,l)=0
else
Inter(m,l)=1
end
end
if rr(m)<(R-xxb(l))
inter(m,l)=2.*pi;
if xp(m,l)==0;
inter(m,l)=(2/3).*pi ;
elseif yp(m,l)>0
inter(m,l)=atan(sqrt(yp(m,l))./xp(m,l));
else
inter(m,l)=0;
end
end
intera(m,l)=inter(m,l)./(2*pi);
end
end
figure(5)
mesh(xxb,rr,intera)
}
adam
adam il 22 Ago 2012
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.

Accedi per commentare.


Jan
Jan il 21 Ago 2012
Modificato: Jan il 21 Ago 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).
  4 Commenti
adam
adam il 21 Ago 2012
Hi Jan , you are right!! I posted already it But now problem I copy and I post In fact, the code is runing without displaying any error. The sizes of all my parameters xxb,r,c,xp,yp,inter are right. However, the values aren't what I expect. I relized that the conditions xp=0 and yp>0 don't work. Ok? To confirm you that, I tried this code without for loop ( or paramter xxb) for some values of xxb ( for -0.05, 0.05, 0.1 ....) for xxb=0 , the results are correct. But for xxb=-0.05, xxb=0.05, xxxb=0.1, the values of inter aren't correct. Here below please you find the code with only one For loop( for rr parameter). {
clear all
xxb=0.05;
dr=0.01;
r=0:dr:5;
rr=r';% row vector
w2=2.25;
R= 0.05;
for m=1:length(rr);
a=2.*xxb;
c(m)=xxb.^2+rr(m).^2-R^2;
if(a==0);
yp(m)=0;
xp(m)=0;
else
xp(m)=c(m)./a;
yp(m)=R^2-((((2.*c(m))-(a^2))./(2.*a)).^2);
end
if(abs(xxb)<(r(m)-R)); % cercles séparés
inter(m)=0;
elseif(xxb==0); % l'un à l'intérieur de l'autre
inter(m)=2*pi;
elseif (yp(m)>0);% surface d'intersection
inter(m)=2.*(atan((sqrt(yp(m)))./xp(m)));
elseif(xp(m)==0);
inter(m)=(3/4)*pi;
else
inter(m)=0;
end
inter1(m)=inter(m)./(2*pi)
}
Thank you very much for your HELLP
Jan
Jan il 21 Ago 2012
Modificato: Jan il 21 Ago 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".

Accedi per commentare.


adam
adam il 23 Ago 2012
Modificato: adam il 23 Ago 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.
Thank you in advance!!!
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
  3 Commenti
adam
adam il 23 Ago 2012
Modificato: adam il 23 Ago 2012
Andrei, that gives the same results.inter(1,100)=inter(1,102)=0
adam
adam il 24 Ago 2012
Modificato: adam il 25 Ago 2012
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

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by