MATLAB Answers

John
0

How to avoid a vertical line at discontinuity point?

Asked by John
on 21 Jan 2015
Latest activity Answered by Jit
on 4 Apr 2016
When I plot a function with discontinuity points, I get a vertical line at such points, as it can be seen in this simple example:
clc;
clear all;
h=@(x) (x>=3 & x<=7).*1+(3<x & x>7).*3;
x = linspace(0,10,100);
figure(1)
plot(x,h(x));
As it can be seen, there are near vertical lines at 3 and 7.
Attempting to remedy this, I tried the following:
clc;
clear all;
h=@(x) (x>=3 & x<=7).*1+(3<x & x>7).*3;
tol=0.0001;
x1=linspace(0,3-tol,1000);
x2=linspace(3,7,1000);
x3=linspace(7+tol,10,1000);
figure(2)
plot(x1,h(x1),x2,h(x2),x3,h(x3))
The vertical lines disappear alright, but it cycles through the colors. I can fix this coloring stuff, but at this point I must ask if there is a better way to deal with this, specially because in some situations the discontinuity points are not so straight-forwardly given.

  2 Comments

How better to handle such a discontinuity than with a vertical lines? That tells you exactly what is happening.
It's true, but if you run his example, then you see that is not exactly a vertical line so if someone looks at the graph, he/she will not know if it is a discontinuity or a sharp change.

Sign in to comment.

4 Answers

Answer by Matt J
on 21 Jan 2015
Edited by Matt J
on 21 Jan 2015
 Accepted Answer

at this point I must ask if there is a better way to deal with this, specially because in some situations the discontinuity points are not so straight-forwardly given.
You will not be able to avoid searching analytically for the discontinuities. A distinction between a sharp change and a discontinuity can't be made by numerical operations on a finite number of samples of the function.
However, you can easily write your own plotting mfile that takes a list of discontinuities and automates the work you've done breaking up the plot. Below, I've done something fancy enough to handle multiple plots and to accept most of the different syntaxes of the usual plot function,
f=@(x) 2*( x>=0 );
g=@(x) sign(x-1).*exp((x-1));
t=linspace(-2,2,2001);
figure
myplot([0,1],t,f(t),'r--', t,g(t),'b-')
and here's the generalized plotting function that it calls.
function myplot(discontinuities, varargin)
%Same as plot(), except that first argument is a list of
%discontinuities. No line joining across these discontinuities will
%appear in the plot
%
%
if nargin<3 || ~isnumeric(varargin{2})
varargin=[{1:length(varargin{1})}, varargin];
end
d=[-inf;discontinuities(:);inf];
intervals=[d(1:end-1), d(2:end)];
N=length(intervals);
idx=find(diff(cellfun(@isnumeric,varargin))==0);
M=length(idx);
for i=1:N
args=varargin;
for j=idx
xdata=args{j};
cut = xdata<=intervals(i,1) | xdata>=intervals(i,2);
args{j}(cut)=nan;
args{j+1}(cut)=nan;
end
plot(args{:}); hold on
end
hold off

  0 Comments

Sign in to comment.


Answer by Jit
on 4 Apr 2016

If you know where the discontinuities are, you can separate them with NaNs. I have to do this to mark bad data in time-series analysis, matlab will plot the line as separated segments.
figure
time = 0:0.1:2;
freq = 2;
time = 0:0.01:2;
data = sin(2*pi*freq*time);
subplot(2,1,1);
plot(time,data);
grid on; grid minor;
ind_bad = [50:100];
data(ind_bad) = NaN;
subplot(2,1,2);
plot(time,data);
grid on; grid minor;

  0 Comments

Sign in to comment.


Answer by Zoltán Csáti on 21 Jan 2015

I advise you to handle the subintervals distinctly and plot them independently from each other. In that case no vertical (more precisely this is not a vertical line, since it links two consecutive data points) line appears. It could be nicely done with OOP. Create an interval class. If you have k number of subintervals, then create k instances of the interval class. After it you can call the plot function for them as an object array.

  0 Comments

Sign in to comment.


Answer by Matt J
on 21 Jan 2015
Edited by Matt J
on 21 Jan 2015

Another option is simply to plot without joining the points. If your sampling is dense enough, the continuous parts of the locus will still look as though they are joined. Compare the red and blue curves:
g=@(x) sign(x-1).*exp((x-1));
t=linspace(-2,2,2001);
plot(t,g(t),'r*', t,g(t),'b-')

  0 Comments

Sign in to comment.