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

9 visualizaciones (últimos 30 días)
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 comentarios
Sam Chak
Sam Chak el 26 de Dic. de 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 el 28 de Dic. de 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!

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Dic. de 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 comentarios
Walter Roberson
Walter Roberson el 28 de Dic. de 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.
Noob
Noob el 28 de Dic. de 2024
Hi Walter!
Let me think about this and perhaps also try it.
Will keep you posted.
Thanks for your time!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by