When plotting functions like 1/sin(x), how can I remove the vertical lines at the points of discontinuity?
조회 수: 7 (최근 30일)
이전 댓글 표시
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
채택된 답변
Voss
2024년 12월 24일
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
Walter Roberson
2024년 12월 24일
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" .
추가 답변 (2개)
Sam Chak
2024년 12월 24일
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
Walter Roberson
2024년 12월 25일
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
2024년 12월 24일
편집: Manish
2024년 12월 24일
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
Torsten
2024년 12월 25일
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)
?
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!