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!