Complex variable

Hello, In a simulation I define a complex array to compute some physical quantity as follows : j_e_real = zeros(length(h),length(t)); j_e = complex(j_e_real,0);
Nevertheless, at the beginning of my simulation (while loop), real numbers are written into the array j_e. This works well until complex numbers need to be written further in the same array j_e (later in my while loop).
This totally froze Matlab. By using 'whos', I checked that my array j_e is still defined as a complex array.
When I artificially add an epsilon complex quantity each time I write something in that array, it doesn't froze.
Any ideas, suggestion? It seems that the memory is not allocated in a proper way, but I don't understand why.
Thx, Maxime

3 个评论

Jan
Jan 2011-7-11
Please explain "this totally froze Matlab" with any details. Do you get an error message or does Matlab crash? Please show us the lines, which causes the problems, otherwise any answer must be a pure speculation.
Ok, thanks Jan.
When the while loop is about to write a complex number, it takes about 100 times more to execute the line which is (h and k are indexes in the loop):
j_e(k,n) = j_e(k,n-1) + (d1 + 2*d2 + 2*d3 + d4)*Dt/6;
(RK4 numerical method with d1...d4 complex quantities)
I found out this by using a conditional break point with a condition on the time spend to execute the line (functions tic and toc).
I didn't spend a whole night to go (about 1e6 times in this loop in the full simulation), but I am almost sure that it could go. There is no crash, no error, it's just 100 times slower when it's writing complex numbers into the array after a first part of the simulation writing only real numbers.
Hence, I can't give any error code unfortunately.
Thanks if you have some ideas!
Jan
Jan 2011-7-11
@Kooglof: without seeing your code, the assistence is a pure guessing and therefore inefficient.
Do you need j_e_real at all? If not, it wastes memory and this would be sufficient: j_e = zeros(length(h),length(t)). Then the memory for the complex values is allocated automatically the first time a complex value is assigned to j_e.
I suggest to use the PROFILEr to find out, which lines are slower than with real values.

请先登录,再进行评论。

回答(1 个)

Titus Edelhofer
Titus Edelhofer 2011-7-11

0 个投票

Hi,
I guess the following is happening: the real part and the imag part are stored in two different arrays. Depending on the operating system and the version of MATLAB you are using, MATLAB does not allocate memory for the imaginary part as long as all imag parts are zero. The moment the first imaginary part is non zero, the memory is allocated. Therefore, next guess, the moment the first non zero imaginary part is written into the array, you should see in the task manager (or using top in linux) a large jump in memory usage (making the operating system swap memory, which is slow).
Titus

1 个评论

Jan
Jan 2011-7-11
a = rand(1000, 1000); b = complex(a, 0);
Now "b(end,end)=i" does not get more memory from the OS, according to the Windows taskmanager.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2011-7-11

Community Treasure Hunt

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

Start Hunting!

Translated by