-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfSimilar1D.m
114 lines (91 loc) · 2.5 KB
/
SelfSimilar1D.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
%% To plot homogeneous self-similar sets by iterating intervals
% Zhou Feng @ 2022-5-20
clc, clf, clear
tic
%% settings
ratios = [1/pi, 1/pi, 1/pi];
trans = [0, 1, sqrt(2)];
intervalInit = [0, pi * sqrt(2) / (pi - 1)];
numItrs = 7; % iteration time
% plot settings
showTitle = false;
showFirstItrs = true;
numFirstItrs = numItrs;
color = 'k';
thickness = 30;
%% examples
% % mid-third Cantor set
% ratios = [1/3 1/3];
% trans = [0, 2/3];
% intervalInit = [0, 1];
% % a classic example
% ratios = [1/2, 1/3, 1/4];
% trans = [0, 1/3, 3/4];
% intervalInit = [0, 1];
% Bernoulli convolutions
ratio = (sqrt(5) - 1) / 2;
ratios = ratio * ones(1, 2);
trans = [-1, +1];
intervalInit = [-1/(1-ratio), 1/(1-ratio)];
%% prepare params & error handling
isCompact = false;
if length(ratios) == length(trans)
isCompact = true;
end
if ~isCompact
error('Illegal settings. Dimensions of the parameters does not match!')
end
spaceDim = 1;
sizeIFS = length(ratios);
trans = sort(trans);
lenInit = diff(intervalInit);
numFirstItrs = numFirstItrs + 1;
%% generate points
ptsInit = intervalInit;
ptsNow = ptsInit;
sizeNow = length(ptsNow);
ptsTotal = cell(numItrs + 1, 1);
ptsTotal{1} = ptsInit;
for levelNow = 1:numItrs
ptsTmp = zeros(spaceDim, sizeNow * sizeIFS);
for indexFct = 1:sizeIFS
ptsTmp((indexFct - 1) * sizeNow + 1:indexFct * sizeNow) = ...
ratios(indexFct) * ptsNow + trans(indexFct);
end
ptsNow = ptsTmp;
sizeNow = length(ptsNow);
ptsTotal{levelNow + 1} = ptsNow;
end
%% plot
ptsPlt = reshape(ptsNow, 2, []);
numSegments = size(ptsPlt, 2);
figure(1)
plot(ptsPlt, ...
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
ptsPltTmp = reshape(ptsTotal{i}, 2, []);
numSegmentsTmp = size(ptsPltTmp, 2);
plot(ptsPltTmp, 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;
countIntervalsTotal = size(ptsPlt, 2);
tableResults = table(countPtsTotal, countIntervalsTotal);
format long
disp(tableResults)