フィルターのクリア

something wrong with my code (iteration)

1 回表示 (過去 30 日間)
Abdullah
Abdullah 2012 年 1 月 7 日
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 件のコメント
the cyclist
the cyclist 2012 年 1 月 7 日
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.

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

採用された回答

Walter Roberson
Walter Roberson 2012 年 1 月 7 日
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 件のコメント
Abdullah
Abdullah 2012 年 1 月 7 日
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 2012 年 1 月 7 日
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.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by