Removing vertical lines in a piecewise function when discontinuty points are not known

3 views (last 30 days)
I am creating various cantor staircases by starting from different functions:
for example:
N = 3;
f{1} = @(x)sin(2*pi*x);
for i = 1:N-1
f{i+1} = @(x)(0.5*f{i}(3*x).*(x<=1/3) + 0.5.*(x>1/3 & x<2/3) + (0.5+0.5*f{i}(3*x-2)).*(x>=2/3 & x <=1));
end
x = linspace(0,1,1000);
plot(x,f{N}(x))
The problem is that vertical lines are emerging.
How do I remove the vertical lines when I do not know(do not want to find) the discontinuity points? Surely there has to be a way...

Accepted Answer

Torsten
Torsten on 13 May 2023
Edited: Torsten on 13 May 2023
Maybe something like this. But the jump height of 0.05 at discontinuities most probably has to be adjusted in other applications.
N = 3;
f{1} = @(x)sin(2*pi*x);
for i = 1:N-1
f{i+1} = @(x)(0.5*f{i}(3*x).*(x<=1/3) + 0.5.*(x>1/3 & x<2/3) + (0.5+0.5*f{i}(3*x-2)).*(x>=2/3 & x <=1));
end
x = linspace(0,1,1000);
y = f{N}(x);
idx = find(abs(diff(y)) > 0.05);
idx(end+1) = numel(x);
hold on
iend = 0;
for i = 1:numel(idx)
istart = iend + 1;
iend = idx(i);
plot(x(istart:iend),y(istart:iend),'b');
end
hold off
grid on
  2 Comments
Torsten
Torsten on 13 May 2023
Edited: Torsten on 13 May 2023
You can save the separate "sections" of the x- and y-array in a cell array. This cell array will have elements equal to the number of sections. In the loop, you can add the two lines
X_array{i} = x(istart:iend);
Y_array{i} = y(istart:iend);
before or after the plot command.

Sign in to comment.

More Answers (1)

chicken vector
chicken vector on 13 May 2023
Edited: chicken vector on 13 May 2023
EDIT: Thanks to @Torsten
figure;
tiledlayout(2,2);
for N = 2 : 5
nexttile;
f{1} = @(x)sin(2*pi*x);
for i = 1 : N-1
f{i+1} = @(x)(0.5*f{i}(3*x).*(x<=1/3) + 0.5.*(x>1/3 & x<2/3) + (0.5+0.5*f{i}(3*x-2)).*(x>=2/3 & x <=1));
end
x = linspace(0,1,1000);
y = f{N}(x);
horizontalIdx = [diff(y)~=0, 1];
breakIdx = [1 strfind([0 horizontalIdx 0], [1 0])];
hold on;
for line = 1 : length(breakIdx)-1
idx = breakIdx(line):breakIdx(line+1)-1;
plot(x(idx), y(idx),'Color',[0 0.4470 0.7410]);
end
hold off;
box on;
end
OLD ANSWER:
I am assuming you meant horizontal lines as I don't see any vertical ones.
N = 3;
f{1} = @(x)sin(2*pi*x);
for i = 1:N-1
f{i+1} = @(x)(0.5*f{i}(3*x).*(x<=1/3) + 0.5.*(x>1/3 & x<2/3) + (0.5+0.5*f{i}(3*x-2)).*(x>=2/3 & x <=1));
end
x = linspace(0,1,1000);
y = f{N}(x);
You can use:
horizontalIdx = find([diff(y)==0, 0]);
y(horizontalIdx) = NaN;
figure;
plot(x,y);
Or:
horizontalIdx = [diff(y)~=0, 0];
startIdx = strfind([0 horizontalIdx], [0 1]) + 1;
endIdx = strfind([horizontalIdx 0], [1 0]);
figure;
hold on;
for line = 1 : length(startIdx)
idx = startIdx(line):endIdx(line);
plot(x(idx), y(idx),'Color',[0 0.4470 0.7410]);
end
hold off;
box on;
  2 Comments
Torsten
Torsten on 13 May 2023
Edited: Torsten on 13 May 2023
I am assuming you meant horizontal lines as I don't see any vertical ones.
No, @Vivaan meant the graph at discontinuities, thus where wrong vertical lines appear.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!