When plotting functions like 1/sin(x), how can I remove the vertical lines at the points of discontinuity?
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (2)
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 commentaires
Walter Roberson
le 24 Déc 2024
Or use symbolic fplot with the option ShowPoles off
I believe that the display of asymptotes at poles is acceptable because these gray dashed lines merely indicate that the curve approaches infinity and are not part of the mathematical function itself. The original poster likely intended to remove the continuity of the graph (vertical lines) at the poles when using the plot() function; however, this does not make sense at the singularities.
Anyhow, thanks for the option to hide the asymptotes.
fplot(@(x) 1./sind(x), [-359 359], 'ShowPoles', 'off'), grid on, xlabel('degree')
Noob
le 24 Déc 2024
Walter Roberson
le 25 Déc 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.
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 commentaires
Sam Chak
le 24 Déc 2024
But this approach does not "remove" the vertical lines.

Adjust the threshold to 1e-2.
x = linspace(-2*pi, 2*pi, 1000);
y = 1 ./ sin(x);
threshold=1e-2;
% Identify points where sin(x) is zero and set corresponding y values to NaN
y(abs(sin(x)) < threshold) = NaN; % Use a small threshold to account for numerical precision
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;
Noob
le 24 Déc 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)
?
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




