"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:
Best Answer