If loop is not working

I'm trying to execute this code but the program does not do the if loop. I know that because in the solution the B matrix is showing all 0. I can see in the solution that the rest of the code is working properly and that there is a value of PImagc=50 so B shouldn't be all 0.
Here is the code:
clear all
close all
s=input(Introduce s (1-40): ');
n=input('Introduce n (1-100): ');
af=zeros(1,s);
bf=zeros(s,n);
cf=zeros(s,n);
B=zeros(1,s);
C=zeros(1,s);
syms a b c;
F=6*a^3-10*b^2+c+i*(2*a+b+c);
Re=real(F)
Im=imag(F)
for h=1:s
af(h)=h;
a=af(h);
for j=1:n
bf(h,j)=j;
b=bf(h,j);
PRe=subs(Re);
sol=solve(PRe,c);
c=cf(i,j);
PImag=subs(Im);
PImagc=subs(PImag,c,sol);
if PImagc==50
B(1,h)=V;
C(1,h)=sol;
else
bf(h,j)=bf(h,j);
end
end
end
disp('B is: ' )
B
Thank you!

5 commentaires

pfb
pfb le 30 Avr 2015
Is the value you are referring to exactly 50?
Or is it something like 50.000?
In the latter case the condition will be false, because your "50" is not exactly 50.
If this is the case, you should introduce a threshold, like
if(abs(PImagc-50)<1e-10)
[... code ...]
end
Lorena Ortiz
Lorena Ortiz le 30 Avr 2015
Thank you pfb. I've tried what you said but nothing happens...
Thank you anyway
pfb
pfb le 30 Avr 2015
Modifié(e) : pfb le 30 Avr 2015
Well, could mean that PImagic is not that close to 50.
You say that sometimes you get PImagic=50. Can you check what's
abs(PImagig-50)
in that case?
This should give you an idea of what your threshold should be. If this is 1e-6, you should use 1e-5 instead of 1e-10 above.
I put PImagc-50 and this is what I've obtained:
P =
0.-0.*i
I think that if I put what you said the program should work so maybe the problem is not there or I'm not doing well the program.
Thank you for your help pfb!
pfb
pfb le 30 Avr 2015
Modifié(e) : pfb le 30 Avr 2015
what about the absolute value?
The fact that the imaginary part has a minus sign means that there is a possibly very small negative imaginary part.
You should take a look at
abs(PImagc-50)
and, while you're at it, what's the output of
whos PImagc

Réponses (2)

Jan
Jan le 30 Avr 2015

1 vote

If you omit the brutal clear all it is easy to use the debugger. This tool is designed for stepping through the code line by line, such that you can examine directly what's going on. Therefore I'm astonished that clear all is used so often and recommended by any persons obviously, although it impedes the debugger such efficiently.
Guillaume
Guillaume le 30 Avr 2015

0 votes

The only line that modifies B is this one:
B(1, h) = V;
inside an if. Two possibilities:
  • V is 0. V never get assigned any value the code you've shown, and since you start with a clear all, it probably does not even exists. So, unless you've made missed a line in your post, if the statement was executed, it would result in an error (undefined variable). Which strongly points to:
  • The if condition is never true. You say that Pimagc is 50, but is it exactly 50 and not some value very close to 50 that matlab displays as 50? To check look at the value of Pimagc - 50. I suspect it is going to be a very small value but not exactly 0.

1 commentaire

Sorry, I put the wrong code. This is the good one:
clear all
close all
s=input(Introduce s (1-40): ');
n=input('Introduce n (1-100): ');
af=zeros(1,s);
bf=zeros(s,n);
cf=zeros(s,n);
B=zeros(1,s);
C=zeros(1,s);
syms a b c;
F=6*a^3-10*b^2+c+i*(2*a+b+c);
Re=real(F)
Im=imag(F)
for h=1:s
af(h)=h;
a=af(h);
for j=1:n
bf(h,j)=j;
b=bf(h,j);
PRe=subs(Re);
sol=solve(PRe,c);
c=cf(i,j);
PImag=subs(Im);
PImagc=subs(PImag,c,sol);
if PImagc==0
B(1,h)=b;
C(1,h)=sol;
else
bf(h,j)=bf(h,j);
end
end
end
disp('B es: ' )
B
And the value that I've obtained in the solution is 50.0000000000000000000000000000000-0.*i but I've put PImagc==50.0000000000000000000000000000000-0.*i and I have the same problem...
Thank you anyway

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by