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

13 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by