When plotting functions like 1/sin(x), how can I remove the vertical lines at the points of discontinuity?

7 visualizaciones (últimos 30 días)

Hi there! When I plot functions such as 1/sin(x), there are vertical lines that appear at the points of discontinuity. How can I best remove these vertical lines, if I wanted to see the graph for a bigger domain, say, from -2pi to 2pi? Thanks in advance.

Respuesta aceptada

Voss
Voss el 24 de Dic. de 2024
x = linspace(-2*pi,2*pi,1000);
y = 1./sin(x);
figure
% put NaNs where y changes sign:
y_plot = y;
y_plot([false diff(sign(y))~=0]) = NaN;
plot(x,y_plot)
ylim([-5 5])
grid on
  2 comentarios
Noob
Noob el 24 de Dic. de 2024
Editada: Noob el 24 de Dic. de 2024
Hi Voss!
What does this line of code mean:
y_plot([false diff(sign(y))~=0]) = NaN
I understand diff represents symoblic differentiation.
I undertand sign( ) checks the sign of a number.
I understand NaN = Not a number.
I understand y_plot( ... ) = NaN means setting function values for y_plot, if certain conditions are met.
But, what does false and ~= mean?
And, do you think I really need to use symbolic math?
Can I do it without symbolic math?
Thanks!
Walter Roberson
Walter Roberson el 24 de Dic. de 2024
I understand diff represents symoblic differentiation.
Not in this case. There are two major diff() functions. When diff() is applied to a sym or symfun or symmatrix then diff() means symbolic differentiation.
However, when diff() is applied to numeric values, diff(A) means roughly (A(2:end)-A(1:end-1)) -- the consecutive differences function. (There is some nuance to this having to do with row vectors compared to column vectors compared to arrays, and there are options having to do with multiple differences at the same time.)
In this particular context, diff() is "consecutive differences". sign(y) returns -1 where y is negative, 0 where y is 0, and +1 where y is positive. diff()~=0 applied to sign() is looking for places where the sign() is changing
false is a function that called by itself returns a scalar logical false. It is equivalent to logical(0) in this context.
~= is "not equal" .

Iniciar sesión para comentar.

Más respuestas (2)

Sam Chak
Sam Chak el 24 de Dic. de 2024
Use fplot().
fplot(@(x) 1./sind(x), [-359 359]), grid on, xlabel('degree')
% d = -345:30:345;
% x = deg2rad(d);
% y = 1./sin(x);
% plot(x, y)
  5 comentarios
Noob
Noob el 25 de Dic. de 2024
Editada: Noob el 25 de Dic. de 2024
Hi @Walter Roberson! Do you feel that one should truncate the intervals of interest and only plot the function where it is known to be continous (putting aside the issue of smoothness and differentibability, for the moment)? In my work, I am sort of aiming to demonstrate a function's behavior for an extended domain (say, for larger angles than previously considered). Perhaps removing the discontinuitity is unnecessary, and maybe even wrong to do, and that one should aim to remove the vertical lines that do not actually represent function values: They are merely extraneous lines from Matlab connecting function values, I think. What do you think? Thanks!

Iniciar sesión para comentar.


Manish
Manish el 24 de Dic. de 2024
Editada: Manish el 24 de Dic. de 2024
Hi,
I understand that you want to remove the vertical lines that appear at the points of discontinutity for the function y=1/sin(x).
This can be achieved setting 'y' values to NAN where '1/sin(x)' is close to zero.Use a small threshold to identify the values that are very close to zero and adjust the threshold accordingly.
This approach results in skipping the vertical lines.
Refer the code sample below for the better understanding:
x = linspace(-2*pi, 2*pi, 1000);
y = 1 ./ sin(x);
threshold=1e-6;% small threshold
% Identify points where sin(x) is zero and set corresponding y values to NaN
y(abs(sin(x)) < threshold) = NaN;
figure;
plot(x, y, 'b-', 'LineWidth', 1.5);
ylim([-5, 5]);
xlabel('x');
ylabel('1/sin(x)');
title('Plot of 1/sin(x) without Vertical Lines');
grid on;
Hope this helps!
  4 comentarios
Noob
Noob el 24 de Dic. de 2024
Hi Manish!
Interesting solution, but I'm afraid the threshold is not good enough; I would imagine a much smaller number needs to be used, such as 1e-15, but then the spikes to infinity and -infinity would still appear.
Torsten
Torsten el 25 de Dic. de 2024
I would imagine a much smaller number needs to be used, such as 1e-15
so you want the value of the vertical axis to run up to
1/sin(1e-15)
ans = 1.0000e+15
?

Iniciar sesión para comentar.

Categorías

Más información sobre Logical 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