-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLACylinder.m
289 lines (252 loc) · 8.39 KB
/
DLACylinder.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
%% DLA SIMULATION USING RECTANGULAR MATRIX
clear
clf
clc
% Set up variables
xwidth = input('x-width: ');
ywidth = input('y-width: ');
% numberOfParticles = input('Number of Particles: ');
% Initial Setup:
% We create a matrix of zeros and then aggegate the whole bottom row.
% Change entry of matrix to 0.1-1 if it is aggregated.
matrix = zeros(ywidth,xwidth);
for i = 1:xwidth
matrix(ywidth,i) = 0.1;
end
% We want to let a particle randomly enter from the the top of the matrix
particleNumber = xwidth; %integer
x = 0; %integer
y = 0; %integer
aggregate = 0; %bool
endScript = 0; %bool
while endScript == 0
particleNumber = particleNumber+1;
%first decide where the particle starts from
%start on top
randPlace = rand*xwidth;
randPlace = ceil(randPlace);
x = randPlace;
y = 1;
% Now we have a starting point on the top edge of the matrix. Now we want
% to perform random walk in the matrix until we are next to an entry
% which is not 0. Set aggregate to false.
aggregate = 0;
while aggregate == 0
randWalk = rand;
if x == 1
% top left corner
if y == 1
if randWalk < 0.5
x = x+1;
else
y = y+1;
end
% bottom left corner
elseif y == ywidth
if randWalk < 0.5
x = x+1;
else
y = y-1;
end
% left edge other than corners
else
if randWalk < 1/3
x = x+1;
elseif randWalk < 2/3
y = y+1;
else
y = y-1;
end
end
elseif x == xwidth
% top right corner
if y == 1
if randWalk < 0.5
x = x-1;
else
y = y+1;
end
% bottom left corner
elseif y == ywidth
if randWalk < 0.5
x = x-1;
else
y = y-1;
end
% right edge other than corners
else
if randWalk < 1/3
x = x-1;
elseif randWalk <2/3
y = y+1;
else
y = y-1;
end
end
% not on left or right edges
else
% on top edge but not corners
if y == 1
if randWalk < 1/3
x = x+1;
elseif randWalk < 2/3
x = x-1;
else
y = y+1;
end
% on bottom edge but not corners
elseif y == ywidth
if randWalk < 1/3
x = x+1;
elseif randWalk < 2/3
x = x-1;
else
y = y-1;
end
% just in the middle somewhere!
else
if randWalk < 0.25
x = x+1;
elseif randWalk < 0.5
x = x-1;
elseif randWalk < 0.75
y = y+1;
else
y=y-1;
end
end
end
% now check whether it is now adjacent to a 1
if x == 1
% top left corner
if y == 1
if (matrix(y,x+1) + matrix(y+1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
% bottom left corner
elseif y == ywidth
if (matrix(y,x+1) + matrix(y-1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
% left edge other than corners
else
if (matrix(y,x+1) + matrix(y-1,x) + matrix(y+1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
end
elseif x == xwidth
% top right corner
if y == 1
if (matrix(y,x-1) + matrix(y+1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
% bottom left corner
elseif y == ywidth
if (matrix(y,x-1) + matrix(y-1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
% right edge other than corners
else
if (matrix(y,x-1) + matrix(y-1,x) + matrix(y+1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
end
% not on left or right edges
else
% on top edge but not corners
if y == 1
if (matrix(y,x+1) + matrix(y,x-1) + matrix(y+1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
% on bottom edge but not corners
elseif y == ywidth
if (matrix(y,x+1) + matrix(y,x-1) + matrix(y-1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
% just in the middle somewhere!
else
if (matrix(y,x+1) + matrix(y,x-1) + matrix(y-1,x) + matrix(y+1,x))~=0
% Then there is an adjacent particle, so we
% need to stick it
aggregate=1;
end
end
end
%We keep repeating this until the particle is finally stuck
end
if aggregate
if particleNumber < 500
matrix(y,x) = 0.1;
elseif particleNumber < 1000
matrix(y,x) = 0.2;
elseif particleNumber < 1500
matrix(y,x) = 0.3;
elseif particleNumber < 2000
matrix(y,x) = 0.4;
elseif particleNumber < 2500
matrix(y,x) = 0.5;
elseif particleNumber < 3000
matrix(y,x) = 0.6;
elseif particleNumber < 3500
matrix(y,x) = 0.7;
elseif particleNumber < 4000
matrix(y,x) = 0.8;
elseif particleNumber < 4500
matrix(y,x) = 0.9;
else
matrix(y,x) = 1.0;
end
% uncomment this if want to watch diagram 'grow'
% figure(1)
% hold on
% imshow(matrix)
% If the particle hits the top then we stop simulation
% note that, at this point, the variables x and y represent a stuck particle already
if (y == 1)
endScript = 1;
% now want to calculate diameter of fractal
% we could change to make sure that we always add particles until we hit
% the edge, then diameter we could work out
% xdif = abs(x - middlex);
% ydif = abs(y - middley);
% radius = sqrt(xdif^2 + ydif^2);
% diameter = 2*radius;
% fractdim = log(particleNumber)/log(radius);
% Set the escape parameter equal to true to stop the loop
end % end escape check
end
end
%Tell why escaped, if true
% if (endScript == 1)
% disp ('Script ended early as aggregation was about to exceed matrix dimensions');
%
% end
% uncomment this for diagram to just appear at end
figure(1)
imagesc(matrix)
colormap(jet)
title(['DLA with ' num2str(particleNumber) ' particles'])
xlabel(num2str(xwidth))
ylabel(num2str(ywidth))
axis equal
xlim([0,xwidth])
ylim([0,ywidth])
disp(['Number of particles: ' num2str(particleNumber)])
% disp(['Fractal Dimension: ' num2str(fractdim)])
% disp(['Diameter: ' num2str(diameter)])