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

조회 수: 3 (최근 30일)
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...

채택된 답변

Torsten
Torsten 2023년 5월 13일
편집: Torsten 2023년 5월 13일
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
Vivaan
Vivaan 2023년 5월 13일
Is it possible to store this function in a variable somehow?
Torsten
Torsten 2023년 5월 13일
편집: Torsten 2023년 5월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

chicken vector
chicken vector 2023년 5월 13일
편집: chicken vector 2023년 5월 13일
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
Torsten
Torsten 2023년 5월 13일
편집: Torsten 2023년 5월 13일
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.
chicken vector
chicken vector 2023년 5월 13일
편집: chicken vector 2023년 5월 13일
You are right, I was completely blind to those.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by