Double precision changes to complex double after calculation
조회 수: 13 (최근 30일)
이전 댓글 표시
Hi everyone,
I have a matrix with very simple data in double precision but after doing a calculation (listed below) some of the numbers, not all, change to complex numbers. Even the timestamp which is not used in the calculation changes to complex. The data that changes appears to be the same as the data that doesn't change.
Any ideas?
for i = 1:length(ws);
if L(i,1) >= -500 && L(i,1) <= -12
wsstd_uns(i,1) = timestamp(i,1);
wsstd_uns(i,2) = fric(i,1).*(0.35*((-(BLH(i,1)./(vK.*L(i,1)))).^(2/3)) + (2 -(10./BLH(i,1)))).^(1/2);
wsstd_uns(i,3:5) = [ws(i,1),wgst(i,1),(wgst(i,1)-ws(i,1))];
elseif L(i,1) >= 0 && L(i,1) <= 500
wsstd_s(i,1) = timestamp(i,1);
wsstd_s(i,2) = 2.*fric(i,1).*((1 -(10./BLH(i,1))).^(1/2));
wsstd_s(i,3:5) = [ws(i,1),wgst(i,1),(wgst(i,1)-ws(i,1))];
end
end
댓글 수: 2
Stephen23
2015년 1월 30일
You should not use i or j as the names of loop variables, as these are both names for the inbuilt imaginary unit .
Guillaume
2015년 1월 30일
Actually, as per the tip section of the doc you've linked:
- Since i is a function, it can be overridden and used as a variable. However, it is best to avoid using i and j for variable names if you intend to use them in complex arithmetic.
- For speed and improved robustness in complex arithmetic, use 1i and 1j instead of i and j.
In other words, unless you use i or j as the imaginary unit, it doesn't matter. And if you do use i or j as the imaginary unit, you shouldn't and should use 1i or 1j instead (which can't be used as a variable).
채택된 답변
Andreas Goser
2015년 1월 30일
Theory one: You use i as a variable. As this is a "reserved word" for complex calculations, there may be an unexpected effect.
Theory two: One of your functions or variables shadows the real MATLAB command and does something unexpected. Like you would assign plot=1 the then try to use the plot command.
추가 답변 (1개)
Guillaume
2015년 1월 30일
Well, you take the square root and cubic roots of some numbers so, if these numbers are negative, you'll get some complex numbers.
As for your timestamp, are you sure it's complex, that is imag(x) ~= 0. The real numbers in a matrix containing complex numbers are displayed as complex but with an imaginary part equal to 0.
댓글 수: 4
Guillaume
2015년 1월 30일
You can also vote for answers with the triangle on the left. This gives a little bit of credit.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!