-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfSimilar1DHomo.m
131 lines (110 loc) · 3.07 KB
/
SelfSimilar1DHomo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
%% To plot homogeneous self-similar sets by iterating intervals
% Zhou Feng @ 2022-5-20
clc, clf, clear
tic
%% settings
% num of maps
numMaps = 2;
% contract ratio
ratio = (sqrt(5)-1) / 2;
% translations
trans = [-1, 1];
% initial inverval
intervalInit = [-1/(1-ratio), 1/(1-ratio)];
numItrs = 1; % iteration time
% plot settings
showTitle = false;
showFirstItrs = true;
numFirstItrs = numItrs;
color = 'k';
thickness = 20;
%% examples
% % mid-third Cantor set
% numMaps = 2;
% ratio = 1/3;
% trans = [0, 2/3];
% intervalInit = [0, 1];
% % lambda Cantor set
% numMaps = 3;
% ratio = 1/3;
% lambda = 1/2;
% trans = [0, 2/3, lambda / 3];
% intervalInit = [0, 1];
% % Bernoulli convolutions
% numMaps = 2;
% ratio = 0.4;
% trans = [0, 1 - ratio];
% intervalInit = [0, 1];
% % an example
% numMaps = 5;
% a = 3;
% ratios = 1/3 * ones(1, numMaps);
% trans = [0, a.^ - (0:1:(numMaps - 2))];
% intervalInit = [0, 1.5];
%% prepare params & error handling
isCompact = false;
if numMaps == length(trans)
isCompact = true;
end
if ~isCompact
error('Illegal settings. Dimensions of the parameters does not match!')
end
ratios = ratio * ones(1, numMaps);
trans = sort(trans);
lenInit = diff(intervalInit);
numFirstItrs = numFirstItrs + 1;
%% generate points
ptsInit = intervalInit(1);
ptsNow = ptsInit;
sizeNow = length(ptsNow);
ptsTotal = cell(numItrs + 1, 1);
ptsTotal{1} = ptsInit;
for levelNow = 1:numItrs
ptsTmp = kron(ptsNow, ratios) + kron(ones(1, sizeNow), trans);
ptsNow = unique(ptsTmp);
sizeNow = length(ptsNow);
ptsTotal{levelNow + 1} = ptsNow;
end
%% plot
lenBasic = ratio^numItrs * lenInit;
ptsDiff = diff(ptsNow);
indexGaps = ptsDiff > lenBasic;
ptsLeft = ptsNow([true indexGaps]);
numSegments = length(ptsLeft);
ptsRight = [ptsNow([false, indexGaps]) - ptsDiff(indexGaps), ptsNow(end)] + lenBasic;
figure(1)
plot([ptsLeft; ptsRight], ...
zeros(2, numSegments), ...
color, "LineWidth", thickness)
set(gca, 'XColor', 'none', 'YColor', 'none')
if showTitle
title(['Iteration time = ', num2str(numItrs)], 'Interpreter', 'latex');
end
if showFirstItrs && numItrs + 1 >= numFirstItrs
figure(2)
for i = 1:numFirstItrs
lenBasicTmp = ratio^(i - 1) * lenInit;
ptsDiffTmp = diff(ptsTotal{i});
indexGapsTmp = ptsDiffTmp > lenBasicTmp;
ptsLeftTmp = ptsTotal{i}([true indexGapsTmp]);
numSegmentsTmp = length(ptsLeftTmp);
ptsRightTmp = [ptsTotal{i}([false, indexGapsTmp]) - ptsDiffTmp(indexGapsTmp), ...
ptsTotal{i}(end)] + lenBasicTmp;
plot([ptsLeftTmp; ptsRightTmp], ...
zeros(2, numSegmentsTmp) - i, ...
color, "LineWidth", thickness)
hold on
end
hold off
set(gca, 'XColor', 'none', 'YColor', 'none', 'ZColor', 'none')
if showTitle
title(['Iteration time = ', num2str(numFirstItrs - 1)], 'Interpreter', 'latex');
end
ylim([- numFirstItrs - 0.5 * thickness / 50, 0])
end
%% show param
countPtsTotal = sizeNow;
countShapesTotal = size(ptsLeft, 2);
tableResults = table(countPtsTotal, countShapesTotal);
format long
disp(tableResults)