필터 지우기
필터 지우기

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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by