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

100 views (last 30 days)

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.

Answers (3)

Sam Chak
Sam Chak on 24 Dec 2024 at 10:21
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 Comments
Noob
Noob on 25 Dec 2024 at 21:32
Edited: Noob on 25 Dec 2024 at 21:33
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!

Sign in to comment.


Manish
Manish on 24 Dec 2024 at 10:25
Edited: Manish on 24 Dec 2024 at 10:26
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 Comments
Noob
Noob on 24 Dec 2024 at 21:12
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 on 25 Dec 2024 at 1:35
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
?

Sign in to comment.


Voss
Voss on 24 Dec 2024 at 21:37
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 Comments
Noob
Noob on 24 Dec 2024 at 21:51
Edited: Noob on 24 Dec 2024 at 21:51
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 on 24 Dec 2024 at 21:59
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" .

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!