something wrong with my code (iteration)
1 回表示 (過去 30 日間)
古いコメントを表示
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
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
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 件のコメント
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 Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!