Why do I get warning messages about 'Imaginary Parts of Complex X and/or Y Arguments Ignored' ?

568 views (last 30 days)
Why do I get the following error message:
ERROR: Warning: Imaginary parts of complex X and/or Y arguments ignored.
Warning: Imaginary parts of complex X, Y, and/or Z arguments ignored.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Dec 2021
Edited: MathWorks Support Team on 13 Dec 2021
Explanation:
You are attempting to plot using two complex inputs to a plotting function, like PLOT or PLOT3. In this case, MATLAB will plot using the real part of the first input as the independent variable X and the real part of the second input as the dependent variable Y.
Common causes:
You have performed a square root or FFT operation on the vectors you are attempting to plot, and those operations resulted in a complex vector. Alternatively, you used the variables i and/or j in the computation of your input vectors but those variables did not exist when you performed your computation. In this case, MATLAB will treat i and/or j as the imaginary unit.
Solution:
Stop MATLAB on the line where the warning occurs. Verify that the two vectors or matrices you pass to the PLOT function, or the three you pass to PLOT3, are not complex. If you want to plot the real part of a vector versus the complex part, pass the vector as a single complex vector to the PLOT function. If you want to plot the magnitude of the elements, use the ABS function on the vector before passing it to the PLOT function.
Example demonstrating this warning:
ImaginaryPartIgnored.m
  1 Comment
Steven Lord
Steven Lord on 21 Mar 2018
Adam, where on the standard 2-D axis should the point with coordinates (1+2i, 3) be drawn?
If only the X coordinate can be complex while the Y coordinate must be real or vice versa you could use the real, imag, and plot3 functions.
v = 1:10;
x = v + 1i*v.^2;
y = abs(x);
plot3(real(x), imag(x), y)
xlabel('real(x)');
ylabel('imag(x)');
zlabel('y')
Or you could use abs to take the absolute value of your coordinates as stated in the Support team's answer, or you could find some other way to generate real values from your complex values for purposes of plotting.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!