Remove ignored complex values from a plot

2 ビュー (過去 30 日間)
Arne
Arne 2025 年 3 月 29 日
編集済み: David Goodmanson 2025 年 4 月 1 日
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 Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by