Azzera filtri
Azzera filtri

something wrong with my code (iteration)

1 visualizzazione (ultimi 30 giorni)
Hello, everyone what is wrong with my code??
I = 2.8438-0.883j;
V = 7955;
Em = 7967;
for a= 0:0.01:2
for b= 0:0.01:2
E = V + (a+b*i)*I;
if (abs(E) - Em) == 0
am = a;
bm = b;
end
end
end
thank you in advance
  1 Commento
the cyclist
the cyclist il 7 Gen 2012
The only thing wrong with your code that I can see is that you haven't explained what it is you are trying to do, and why you think it isn't working.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 7 Gen 2012
It is a bit unusual to use "j" as the complex constant in the definition of "I", but then to use "i" as the complex constant in the definition of "E".
Your code does not initialize "am" or "bm", so if the "if" condition never holds true, those variables will not be initialized.
It is improbable that abs(E) - Em will ever work out as exactly 0. Please read http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
  2 Commenti
Abdullah
Abdullah il 7 Gen 2012
thanks for your reply
yes i decreased the step size from 0.1 to 0.00001 and also it did not work .. seems that == condition is very difficult to obtain.. so what to do In my code ?
Walter Roberson
Walter Roberson il 7 Gen 2012
Please read that FAQ and follow the suggestions there to use a tolerance.
It seems to me that for any given "a" value, you should be able to compute the "b" needed.
solve(V + ab * I = 7967, ab)
and then a would be real(ab) and b would be imag(ab)
The solution is a = 3.848656051 and b = 1.195007839 -- which is outside the "a" range of your "for" loop, so your loop would not have found the answer even if you had used a tolerance.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by