complex plots bug?

7 views (last 30 days)
Scott Hebert
Scott Hebert on 21 Feb 2020
Commented: Scott Hebert on 21 Feb 2020
Am I doing something wrong or is this a bug in Matlab plotting?
Set up nested for loops to iterate through the real and complex values in the complex plane and evaluate a function on the complex domain for some criteria. Use real(z^2) > 0 as an example. Plot any z values that meet the real(z^2) > 0 criteria.
The resulting plots have the following 2 bugs: a phantom vertical line at Real value 1 and no points plot along the real axis. Command line testing of the evaluation shows that matlab is correctly executing the function and reporting the correct values, so it seems to be something in the handoff to the plot engine (maybe??). Is there some known way to work around this?
Example code:
%------------------------------------------------------------------------------------------------------------------------------------------------------------------
clf
hold on
grid on
xlim([-5 5])
ylim([-5,5])
disp("starting job...")
for zx=-5:.1:5
for zy=-5:.1:5
z=zx+zy*i; %compose z from real and imag parts
if(real(z^2)>0) %sample function evaluation criteria
plot(z,'b.') % bug has plots showing a vertical line at re(1) and no points on the real axis.
end
end
end
disp("job complete")
sample output:

Accepted Answer

Stephen23
Stephen23 on 21 Feb 2020
Edited: Stephen23 on 21 Feb 2020
"Am I doing something wrong or is this a bug in Matlab plotting?"
There is no bug in the plotting.
Exactly as documented, when z is complex, plot will plot the imaginary part against the real part.
What you have overlooked is that MATLAB silently removes the "complex" property from an operation's output array when the imaginary part of every value in that array is zero. And that is what happens with some of your values, the ones where you multiply i by zero... which not surprisingly have zero imaginary part, therefore z is not a complex array, and so the input to plot is not a complex array, so plot applies other rules (plotting the input value against the indices of that input array, which for a scalar are always 1).
You can check this yourself by only plotting the non-complex values inside your loop:
if isreal(z)
plot(z,'b.')
end
I don't know if there is a neat way to force an array to be complex. To make z complex add this:
if isreal(z)
z = complex(z);
end
or this
z = complex(real(z),imag(z));
Giving:
  3 Comments
Stephen23
Stephen23 on 21 Feb 2020
"So you are thinking the plot function is taking the values from the real line that I flagged as missing and mapping them up the imaginary axis at real=1 then"
That is not just what I think, it is also supported by the test that I did (see my answer), the documentation that I read (explained in my answer), and the values that I looked at:
>> zx = 5;
>> zy = 0;
>> z = zx+zy*i;
>> isreal(z)
ans =
1
Scott Hebert
Scott Hebert on 21 Feb 2020
Thanks.
Apologies, my intention with my comment wasnt to pose a question, but rather was to summarize and highlight this behavior for anyone in the future. in other words your answer neatly addresses both 'bugs' at once. The plot engine has no idea what you are passing it, and it does not have any inherent ability to stick to one mode or another based on how it was inititialized (complex or real numbers for instance). Forcing all values to be recognized as complex numbers prior to being passed to the plotter is mandatory. While this behavior can be deciphered from the function description, the examples I've seen never make use of the complex() function, even though in this context of passing single variables it seems to be mandatory. Alternatively the end user could make sure to keep both real and imaginary parts seperate and pass them individually to plot, rather than as a singular complex variable.
In the absense of examples using the complex() function, in my head I was foolishly assuming Matlab had typecast my variable z as complex and would continue to pass it as such, but as you correctly pointed out no such behavior actually exists.
Thanks again for your quick and thorough reply!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!