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

11 ビュー (過去 30 日間)
Noob
Noob 2024 年 12 月 24 日
編集済み: Noob 2024 年 12 月 25 日

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.

回答 (3 件)

Sam Chak
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
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.
Noob
Noob 2024 年 12 月 25 日
編集済み: Noob 2024 年 12 月 25 日
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!

サインインしてコメントする。


Manish
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 件のコメント
Noob
Noob 2024 年 12 月 24 日
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 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)
ans = 1.0000e+15
?

サインインしてコメントする。


Voss
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 件のコメント
Noob
Noob 2024 年 12 月 24 日
編集済み: Noob 2024 年 12 月 24 日
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 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" .

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by