How to remove extraneous Matlab-generated vertical lines that connect function values at jump discontinuities?

9 ビュー (過去 30 日間)
Hi there!
I previously asked a similar question but I now have a more focused question to ask, I think: When plotting a function that has discontinuities on some interval of interest, Matlab will generate vertical lines that connect function values from left to right, I think. These vertical lines are completely extraneous and do not represent function behavior or function values. So, I'd like to remove such lines. How can I best do this -- while also being careful not to remove any vertical lines that might represent legitimate function behavior / values?
Thanks in advance,
  2 件のコメント
Sam Chak
Sam Chak 2024 年 12 月 26 日
Perhaps, clearly describing the meaningful information you wish to display on the graph of , possibly accompanied by a sketch, would help us guide you on how to use specific MATLAB functions for plotting the graph.
Noob
Noob 2024 年 12 月 28 日

Hi Sam! 1/sin(x) is just a toy example and not the actual function I’m working with. I think I’ll follow what Walter suggested below, but then there’s the risk of setting certain function values to NaN, when there is no legitimate discontinuity. So, that part is a bit tricky, I think. Thanks for your time! If you have more comments, please feel free to share!

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2024 年 12 月 25 日
MATLAB does not do that.
What is happening is that when you use plot() on a finite set of data, then where there are places that the data suddenly shifts between being very large and very small (or very small and very large) then plot joins the two points. plot() is not adding-on extra lines: plot() is obeying your instructions to join the data that you passed in.
If you have discontinuities in the data you passed in, then plot() is always going to try to join the edges of the discontinuities.
The cure, to not have the edges joined, is to not pass plot() data that has discontinuities in it.
You have a couple of options:
  • detect the jumps yourself and plot() in segments
  • detect the jumps yourself and insert infinite or nan values at the discontinuity. plot() stops drawing at values that are not finite.
It is not possible for plot() to determine where discontinuities are and automatically handle them itself. plot() just draws what it is told to draw -- it can't tell a valid sudden steep area from discontinuities.
If you use fplot() then fplot() attempts to determine where discontinuities are, and optionally draws a dashed discontinuity line (defaults to drawing such lines.) This has performance implications.
  3 件のコメント
Walter Roberson
Walter Roberson 2024 年 12 月 28 日
You can try taking the symbolic derivative of the function, and solve()'ing the denominator of that for 0. Any 0 in the denominator is a discontinuity.
This is dependent on being able to express the function symbolically, and being able to take the derivative, and being able to solve the denoninator of the derivative for 0.
Noob
Noob 2024 年 12 月 28 日
Hi Walter!
Let me think about this and perhaps also try it.
Will keep you posted.
Thanks for your time!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by