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!