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

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 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
why don't you post your code? how are we supposed to find out your problem?
adam
adam 2012 年 8 月 21 日
編集済み: adam 2012 年 8 月 21 日
Hi Aziz, Thank you for your message!!! OK here below please you find my code. to confirm the results, I run the code firsly without second For loop (i.e for xxb fixe) doen't work.
When I compare the results for exemple for xxb=0.05, -0.05, 0.1. I realised that the conditions of If loop doen't work
Exactelly : elseif xp==0
inter(m,l)=(3/4).*pi;
elseif(yp>0);
inter(m,l)=(atan((sqrt(yp(m,l)))./xp(m,l)));
Thank you in advance!!
{
clear all
xb=-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.*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(m)).^2-((((2.*c(m,l))-((a(l)).^2))./(2.*a(l))).^2);
theta(m,l)=atan((sqrt(yp(m,l)))./xp(m,l));
if R<rr
inter(m,l)=0;
elseif xb==0
inter(m,l)=2.*pi;
elseif rr+R<xb
inter(m,l)=0;
elseif xp==0
inter(m,l)=(3/4).*pi;
elseif(yp>0);
inter(m,l)=(atan((sqrt(yp(m,l)))./xp(m,l)));
else
inter(m,l)=0;
end
end
inter1(m,l)=inter(m,l)./(2*pi);
end
figure(5)
mesh(xxb,rr,inter1)
}
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
編集済み: Azzi Abdelmalek 2012 年 8 月 21 日
the value of xxb? give all your values
Jan
Jan 2012 年 8 月 21 日
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".
adam
adam 2012 年 8 月 21 日
Thanks!!!

サインインしてコメントする。

回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
編集済み: Azzi Abdelmalek 2012 年 8 月 21 日

0 投票

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 件のコメント

adam
adam 2012 年 8 月 21 日
I dont understand you 0.1>0.05. For xp==0, we doent find inter=0.75*pi. The condition yp >0 doesn't work. and furthermore, the values of C aren't the right. when xxb=0.05 and rr=0, I HEVE TO FIND 0.
All these conditions work very well using one For loop (without For loop of xxb)
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
編集済み: Azzi Abdelmalek 2012 年 8 月 21 日
ok it's a misunderstanding i tried your code, and it works, what is the problem? look at the updated answer
adam
adam 2012 年 8 月 21 日
Thnk you very much. I try to explain the problem. in fact, the cod 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
adam
adam 2012 年 8 月 21 日
Hi Aziz, I Tried your suggestion but donsn't work !!!!
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
編集済み: Azzi Abdelmalek 2012 年 8 月 21 日
Your if elseif elseif is not correct. i don't know exactly what are you looking for? but i can tell you that i found that your conditions are not exclusifs, which means if your condition 1 is true it claculate inter, then find the condtion 3 is true, then skip it that whhat you want?
adam
adam 2012 年 8 月 21 日
Ok Aziz, I'll send you the diagram of computing describing all the conditions may be the right solution will be in this direction
adam
adam 2012 年 8 月 21 日
I'll try that diagram : {
1) Condition to calculate xp :
If a<0&a>0
Xp =c./a;
else
xp=0;
end
2) Condition to calculate yp :
{
If a<0 & a>0
yp(m)=R^2-((((2.*c(m))-(a^2))./(2.*a)).^2
else
yp(m)=0;
end
3) Condition to calculate inter : {
If a=0
if R<r
Inter=0
else
Inter=1
end
if r<(R-xxb)
inter=2.*pi;
if xp=0;
inter=(2/3).*pi ;
else
if yp>0
inter=sqrt(yp)./xp;
else
inter=0;
end
end
end
}
adam
adam 2012 年 8 月 21 日
Hi Aziz, I tried that if conditions but displays an error.
{
% 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==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 }
Jan
Jan 2012 年 8 月 21 日
Whenever you mention in the forum, that an error appears, post the complete message and the line, which causes the error.
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
please, writte the code with all data. what is a?
adam
adam 2012 年 8 月 21 日
編集済み: adam 2012 年 8 月 21 日
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 2012 年 8 月 22 日
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
Jan 2012 年 8 月 21 日
編集済み: Jan 2012 年 8 月 21 日

0 投票

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 件のコメント

adam
adam 2012 年 8 月 21 日
I tried also that . I explain more may be you understand more my problem. when using the second For loop, the conditions xp=0, yp>0 dont work.
Jan
Jan 2012 年 8 月 21 日
You compare the results of two different codes, but you post one of them only. Of course we can only guess what the difference might be. "don't work" is a bad description of the problem. Do the results differ from your expectations (if so, how) or do you get an error message (if so, which one)? Did you use the debugger already to check the values of x and y? The debugger is more powerful and much more direct than asking the forum.
adam
adam 2012 年 8 月 21 日
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 2012 年 8 月 21 日
編集済み: Jan 2012 年 8 月 21 日
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".

サインインしてコメントする。

adam
adam 2012 年 8 月 23 日
編集済み: adam 2012 年 8 月 23 日

0 投票

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 件のコメント

adam
adam 2012 年 8 月 23 日
ok, thank you. I'll try it
adam
adam 2012 年 8 月 23 日
編集済み: adam 2012 年 8 月 23 日
Andrei, that gives the same results.inter(1,100)=inter(1,102)=0
adam
adam 2012 年 8 月 24 日
編集済み: adam 2012 年 8 月 25 日
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

サインインしてコメントする。

カテゴリ

製品

質問済み:

2012 年 8 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by