When plotting functions like 1/sin(x), how can I remove the vertical lines at the points of discontinuity?
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Voss
il 24 Dic 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 Commenti
Walter Roberson
il 24 Dic 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" .
Più risposte (2)
Sam Chak
il 24 Dic 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 Commenti
Walter Roberson
il 25 Dic 2024
There are two versions of fplot() . One of them https://www.mathworks.com/help/matlab/ref/fplot.html works with function handles; the other https://www.mathworks.com/help/symbolic/fplot.html works with symbolic expressions or symbolic functions.
Manish
il 24 Dic 2024
Modificato: Manish
il 24 Dic 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 Commenti
Torsten
il 25 Dic 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)
?
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





