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

14 views (last 30 days)
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 Comments
Sam Chak
Sam Chak on 26 Dec 2024
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 on 28 Dec 2024

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!

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Dec 2024
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 Comments
Walter Roberson
Walter Roberson on 28 Dec 2024
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.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!