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

10 次查看(过去 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.

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by