Remove ignored complex values from a plot

2 次查看(过去 30 天)
I am trying to plot the generated power of a helium-filled balloon in function of height for different values of a certain parameter. The parameter is the mass per unit length of the used cables (3 kinds). For the heaviest cable the calculated power becomes complex at a certain hight. MATLAB ignores these values and plots them as 0, this however isn't aesthetic. Is there a way i can remove this from the plot?
  1 个评论
Walter Roberson
Walter Roberson 2025-3-29
For the heaviest cable the calculated power becomes complex at a certain hight. MATLAB ignores these values and plots them as 0
That is not correct.
When you plot() something that is complex-valued, using plot(y), then MATLAB plots real(y) versus imag(y)
When you plot() something that is complex-valued, using plot(x,y) then MATLAB plots real(x) versus real(y) and gives a warning.
If you are seeing 0 plotted there, then it is because the real portion of the data is zero.
(If the program happens to take the square root of a negative number, then the complex value produced would happen to have zero as the real portion, so zero as the real portion is the natural consequence of taking square root of negative numbers.)

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2025-3-29
Anything set to nan will not plot so you can set the imaginary locations to nan:
imagLocations = (imag(y) ~= 0)
% Set those locations to nan
x(imagLocations) = nan
y(imagLocations) = nan
% Plot non-nan, non-complex elements.
plot(x, y, 'b.-', 'LineWidth', 3, 'MarkerSize', 25);

更多回答(1 个)

David Goodmanson
David Goodmanson 2025-3-30
编辑:David Goodmanson 2025-4-1
Hi Arne,
Either you are getting complex numbers because of an error in the algebra that you coded up (I assume you have checked for this), or you have entered a region that is forbidden by the physics and want to exclude it. If you are certain that the complex numbers are due to being in an unphysical region, then you can use something like
ind = (imag(data)==0);
newdata = data(ind)
newheight = height(ind)
to give you a restricted data set and then plot it. Note: Suppose you have some other quantity to plot as a function of all the heights, for example pressure. If height and pressure both have length, say, 100, and newheight and newdata both have length 60, then
plot(height,pressure,newheight,newdata)
gives you two curves on the plot, one of them shorter.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by