-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_paths.m
740 lines (646 loc) · 22.1 KB
/
draw_paths.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
function draw_paths(logfile, n)
n=n-1; % this is because the 'beacon' shouldn't be analysed - remove this line if not using a beacon
% scatter the boldness vs distance (bolder should generally be further from
% home)
% take smaller time slices to compare random and bold dispersions
% find a way for there to be a catastrophe, so that we can see how quickly
% the population re-stabilises
% explore the effects of having the robots only become bolder from bolder
% robots, rather than simply any
% perhaps the increase and the decrease could both be much slower - more
% stable pop?
warning("check number of kilobots (" + num2str(n) + ") is right!")
%warning("check that you are(n't) plotting zeros for BOTH histograms!")
% to view, something like:
% fig = openfig('log2019-06-10 14:48:32.fig');
% set(fig, 'Visible', 'on')
fh(1) = figure('visible', 'off');
xlabel("time (ms)"); ylabel("X"); zlabel("Y");
hold on
fh(2) = figure('visible', 'off');
xlabel("X"); ylabel("Y");
title("Paths of n="+num2str(n)+ " Kilobots")
hold on
fh(3) = figure('visible', 'off');
xlabel("dist"); ylabel("freq per robot");
hold on
fh(4) = figure('visible', 'off');
xlabel("dist"); ylabel("freq total");
hold on
fh(5) = figure('visible', 'off');
xlabel("dist"); ylabel("freq total");
hold on
fh(7) = figure('visible', 'off');
xlabel("time"); ylabel("bold");
hold on
fh(8) = figure('visible', 'off');
xlabel("kbot"); ylabel("time disconnected");
hold on
fh(9) = figure('visible', 'off');
xlabel("distance"); ylabel("time total");
hold on
fh(10) = figure('visible', 'off');
xlabel("time"); ylabel("Gauss centroid");
hold on
fh(11) = figure('visible', 'off');
xlabel("kbot"); ylabel("time disconnected");
hold off
fh(12) = figure('visible', 'off');
xlabel("time"); ylabel("number of robots");
hold on
fh(13) = figure('visible', 'off');
xlabel("time"); ylabel("proportion of swarm");
hold on
fh(14) = figure('visible', 'off');
xlabel("time"); ylabel("proportion of swarm");
title("Relative Boldness of n="+num2str(n)+" Kilobots Enduring Population Catastrophes")
hold on
fh(15) = figure('visible', 'off');
xlabel("time"); ylabel("proportion of swarm");
title("Relative Boldness of n="+num2str(n)+" Kilobots Enduring Boldness Catastrophes")
hold on
fh(16) = figure('visible', 'off');
xlabel("Average Boldness"); ylabel("Distance Travelled");
title("Distance Travelled by n="+num2str(n)+" Kilobots")
hold on
fh(17) = figure('visible', 'off');
xlabel("Average Target Distance"); ylabel("Distance Travelled");
title("Distance Travelled by n="+num2str(n)+" Kilobots")
hold on
fh(6) = figure('visible', 'off');
hold off
lost = zeros(1,n);
[~,~,dat]=xlsread(logfile, 1);
dist=dat(2:end,7);
lostslice = zeros(n, round((length(dist)-50)/100));
xbins = -10:10;
%count number of kilobots that reach their target
count = 0;
travelled_tots = zeros(1,n);
for i = 2:n+1
[~,~,dat]=xlsread(logfile, i);
i = i - 1; % some jiggery pokery - first robot is the counter, so shouldn't take part
X=dat(2:end,2);
Y=dat(2:end,3);
t=dat(2:end,1);
targ=dat(2:end,8);
msgs=dat(2:end,5);
bold=dat(2:end,6);
dist=dat(2:end,7);
targ=dat(2:end,8);
targplot = zeros(1,length(X));
xplot = zeros(1,length(X));
yplot = zeros(1,length(Y));
tplot = zeros(1,length(t));
boldplot = zeros(1,length(bold));
targplot = zeros(1,length(targ));
reachtarg = targplot;
if i == 1
disttotplot = zeros(1,n*length(dist));
boldtotplot = zeros(n,length(bold));
targtotplot = zeros(n,length(targ));
alpha1 = zeros(1,length(tplot));
alpha2 = zeros(1,length(tplot));
beta1 = zeros(1,length(tplot));
beta2 = zeros(1,length(tplot));
kappa1 = zeros(1,length(tplot));
end
distplot = zeros(1,length(dist));
for j = 1:length(X)
xplot(j) = X{j};
end
for j = 1:length(Y)
yplot(j) = Y{j};
end
for j = 1:length(t)
tplot(j) = t{j};
end
for j = 1:length(dist)
distplot(j) = dist{j};
end
for j = 1:length(targ)
targplot(j) = targ{j};
end
for j = 1:length(bold)
boldplot(j) = bold{j};
% attempt to numerically obtain values for alpha, beta, gamma as in
% Edmund's ODEs --> Don't think this will work, it needs to be
% proportional to the current population of shy/med/bold else it
% will always even out
if j > 2
if (boldplot(j)>80 && boldplot(j-1)<81)
alpha1(i) = alpha1(i) + 1;
end
if (boldplot(j)>160 && boldplot(j-1)<161)
alpha2(i) = alpha2(i) + 1;
end
if (boldplot(j)<81 && boldplot(j-1)>80)
beta1(i) = beta1(i) + 1;
end
if (boldplot(j)<161 && boldplot(j-1)>160)
beta2(i) = beta2(i) + 1;
end
if (boldplot(j)<81 && boldplot(j-1)>160)
kappa1(i) = kappa1(i) + 1;
end
end
end
for j = 1:length(msgs)
msgsplot(j) = msgs{j};
end
for j = 1:length(targ)
targplot(j) = targ{j};
if j ~= 1
reachtarg(j) = targ{j} - targ{j-1};
end
end
count = count + length(nonzeros(reachtarg));
% for plotting the radial vs tangential movement as an angle compared
% to radial line
D = sqrt(xplot.^2+yplot.^2);
AN = atan2(yplot,xplot);
CHAN = AN;
travelled = 0;
for j = 2:length(AN)
CHAN(j) = AN(j) - AN(j-1);
xchan = xplot(j)-xplot(j-1);
ychan = yplot(j)-yplot(j-1);
dot = xplot(j)*xchan+yplot(j)*ychan;
mags = sqrt(xchan^2+ychan^2)*sqrt(xplot(j)^2+yplot(j)^2);
VECANG(length(AN)*(i-1)+j) = acos(dot/mags);
% use magnitudes/'mags' to track how far each kilobot has travelled
travelled = travelled + mags;
end
travelled_tots(i) = travelled;
disttotplot((i-1)*length(dist)+1:i*length(dist)) = distplot;
%figure(1);
disp("plotting 3D plot trajectories")
set(0, 'currentfigure', fh(1));
plot3(tplot,xplot,yplot);
%figure(5);
disp("plotting trajectory plot distances")
set(0, 'currentfigure', fh(5));
plot(tplot,sqrt(xplot.^2+yplot.^2));
%figure(2);
disp("plotting 2D trajectory")
set(0, 'currentfigure', fh(2));
plot(xplot(500:end), yplot(500:end), 'Color', [0,0,0,3/n]);
ylim([-1 1]);
xlim([-1 1]);
%figure(3); - plotting here will overlay a different colour for each
%kilobot, so this becomes unintelligible for large n
disp("plotting histogram")
set(0, 'currentfigure', fh(3));
histogram(distplot);%, xbins);
%histogram(nonzeros(distplot), xbins);
%figure(7);
%plots the boldness for a single kilobot over time. Hold is on so this
%becomes unintelligible for large n
disp("plotting boldness")
set(0, 'currentfigure', fh(7));
plot(tplot, boldplot);
boldtotplot(i,:) = boldplot;
targtotplot(i,:) = targplot;
% only count when kilobot is isolated for more than a few seconds
discnct = msgsplot;
for j = 5:length(msgsplot)
discnct(j) = sum(msgsplot(j-4:j));
end
lost(i) = nnz(discnct);
steps = round((length(disttotplot)/n-50)/100);
for j = 1:steps
lostslice(i,j) = nnz(discnct((j-1)*100+1:j*100));
end
disp(num2str(i) + "/" + num2str(n))
end
%{
% an attempt to numerically find alpha beta and kappa values for Edmund's
% set of ODEs
foralpha1 = zeros(1,length(tplot));
foralpha2 = zeros(1,length(tplot));
forbeta1 = zeros(1,length(tplot));
forbeta2 = zeros(1,length(tplot));
forkappa = zeros(1,length(tplot));
grnd = 1;
for i = 2:length(tplot)
if alpha1(i) || alpha2(i) || beta1(i) || beta2(i)
shy = (sum(boldtotplot(:,i-1)<80))/n;
med = (sum(boldtotplot(:,i-1)>80)+sum(boldtotplot(:,i)<160))/n;
bold = (sum(boldtotplot(:,i-1)>160))/n;
foralpha1(i) = double(alpha1(i))/(shy*med*(i-grnd));
foralpha2(i) = double(alpha2(i))/(med*bold*(i-grnd));
forbeta1(i) = double(beta1(i))/(med*(i-grnd));
forbeta2(i) = double(beta2(i))/(bold*(i-grnd));
forkappa(i) = double(kappa1(i))/(shy*(i-grnd));
grnd = i;
end
end
alpha = sum(foralpha1+foralpha2)/length(tplot);
beta = sum(forbeta1+forbeta2)/length(tplot);
kappa = sum(forkappa)/length(tplot);
alpha10 = sum(foralpha1)/length(tplot);
beta10 = sum(forbeta1)/length(tplot);
alpha20 = sum(foralpha2)/length(tplot);
beta20 = sum(forbeta2)/length(tplot);
kappa10 = sum(forkappa)/length(tplot);
%}
%med = (255/26)*sqrt(n/pi); %med is boldness at edge of where all kilobots could
%pack into tightly (at bold = 0)
%med = (255/26)*sqrt((n+pi*3^2)/pi);
%bold = (255/26)*((sqrt(n)+1)*3)/2; %bold is the region that a homogenous spread
%bold = (255/26)*sqrt(n*6/pi); %bold is the region that a homogenous spread
%of kilobots would cover
med = (255/26)*sqrt((n+pi*3^2)/pi);
bold = (255/26)*((sqrt(n)-1)*3)/2;
%figure(15);
disp("plotting boldness levels")
set(0, 'currentfigure', fh(15));
plot(tplot, sum(boldtotplot<med)/n);
plot(tplot, sum(boldtotplot>med&boldtotplot<bold)/n);
plot(tplot, sum(boldtotplot>bold)/n);
ylim([0 1]);
legend({"Shy", "Medium", "Bold"}, 'Location','northwest');
%figure(12);
disp("plotting boldness levels")
set(0, 'currentfigure', fh(12));
plot(tplot, sum(boldtotplot<80)/n);
plot(tplot, sum(boldtotplot>80&boldtotplot<160)/n);
plot(tplot, sum(boldtotplot>160)/n);
ylim([0 1]);
legend({"Shy", "Medium", "Bold"}, 'Location','northwest');
%figure(13);
disp("plotting split boldness levels")
set(0, 'currentfigure', fh(13));
plot(tplot, sum(boldtotplot<40)/n);
plot(tplot, sum(boldtotplot>40&boldtotplot<80)/n);
plot(tplot, sum(boldtotplot>80&boldtotplot<120)/n);
plot(tplot, sum(boldtotplot>120&boldtotplot<160)/n);
plot(tplot, sum(boldtotplot>160&boldtotplot<200)/n);
plot(tplot, sum(boldtotplot>200)/n);
ylim([0 1]);
legend({"Shy", "Shy-Medium", "Medium", "Medium-Bold", "Bold", "Very Bold"}, 'Location','northwest');
%med = (255/26)*sqrt(n/pi); %med is boldness at edge of where all kilobots could
%pack into tightly (at bold = 0)
med = (255/26)*sqrt((n+pi*3^2)/pi);
med2 = (255/26)*sqrt(((n/2)+pi*3^2)/pi);
med3 = (255/26)*sqrt(((n/4)+pi*3^2)/pi);
bold = (255/26)*((sqrt(n)-1)*3)/2; %bold is the region that a homogenous spread
bold2 = (255/26)*((sqrt(n/2)-1)*3)/2; %bold is the region that a homogenous spread
bold3 = (255/26)*((sqrt(n/4)-1)*3)/2; %bold is the region that a homogenous spread
%bold = (255/26)*sqrt(n*6/pi); %bold is the region that a homogenous spread
%of kilobots would cover
%figure(14);
disp("plotting boldness levels")
set(0, 'currentfigure', fh(14));
sh = [sum(boldtotplot(:,1:1500)<med)/n sum(boldtotplot(1:(n/2),1501:3000)<med2)/(n/2) sum(boldtotplot(1:(n/4),3001:end)<med3)/(n/4)];
me = [sum(boldtotplot(:,1:1500)>med&boldtotplot(:,1:1500)<bold)/n sum(boldtotplot(1:(n/2),1501:3000)>med2&boldtotplot(1:(n/2),1501:3000)<bold2)/(n/2) sum(boldtotplot(1:(n/4),3001:end)>med3&boldtotplot(1:(n/4),3001:end)<bold3)/(n/4)];
bo = [sum(boldtotplot(:,1:1500)>bold)/n sum(boldtotplot(1:(n/2),1501:3000)>bold2)/(n/2) sum(boldtotplot(1:(n/4),3001:end)>bold3)/(n/4)];
plot(tplot, sh, tplot, me, tplot, bo);
%plot(tplot(:,1:1500), sum(boldtotplot(:,1:1500)>bold)/128);
%plot(tplot(:,1501:3000), sum(boldtotplot(:,1501:3000)>bold)/64);
%plot(tplot(:,3001:end), sum(boldtotplot(:,3001:end)>bold)/32);
ylim([0 1]);
legend({"Shy", "Medium", "Bold"}, 'Location','northwest');
tot = sum(lost)/n;
disp("plotting disconnection time")
set(0, 'currentfigure', fh(8));
bar(1:n, lost);
title("Time spent with at least 1 neighbour avg = " + num2str(tot));
disp(lost);
name = extractBetween(logfile, 1,strlength(logfile)-5);
%{
disp("giffing disconnection time by slice")
set(0, 'currentfigure', fh(11));
for i = 1:steps
disp("plotting time " + num2str(i*100))
bar(1:n, lostslice(:,i));
tot = sum(lostslice(:,i))/n;
title("Time spent with at least 1 neighbour avg = " + num2str(tot));
if i == 1
avgs = [tot];
gif([char(name) ' disconnection.gif'],'DelayTime',0.2, 'frame', gcf);
else
avgs = [avgs; tot];
gif
end
end
%}
%{
disp("giffing surviving half disconnection time by slice")
set(0, 'currentfigure', fh(11));
for i = 1:steps
disp("plotting time " + num2str(i*100))
bar(1:n/2, lostslice(1:n/2,i));
tot = sum(lostslice(1:n/2,i))/(n/2);
title("Time spent with at least 1 neighbour avg = " + num2str(tot));
if i == 1
halfavgs = [tot];
gif([char(name) ' half disconnection.gif'],'DelayTime',0.2, 'frame', gcf);
else
halfavgs = [halfavgs; tot];
gif
end
end
%}
%{
disp("giffing surviving quarter disconnection time by slice")
set(0, 'currentfigure', fh(11));
for i = 1:steps
disp("plotting time " + num2str(i*100))
bar(1:n/4, lostslice(1:n/4,i));
tot = sum(lostslice(1:n/4,i))/(n/4);
title("Time spent with at least 1 neighbour avg = " + num2str(tot));
if i == 1
quartavgs = [tot];
gif([char(name) ' quarter disconnection.gif'],'DelayTime',0.2, 'frame', gcf);
else
quartavgs = [quartavgs; tot];
gif
end
end
%}
% plot over the top with the plots for all bots each 100s
plot([1:steps]*100, sum(lostslice));
ylim([0 n*100]);
title("Time spent by " + num2str(n) + " robots with at least 1 neighbour");
disp(lost);
%{
%figure(4); - plotting here is total of all kilobots
disp("plotting histogram total")
set(0, 'currentfigure', fh(4));
%%%%histfit(disttotplot,max(disttotplot) - min(disttotplot));
x = unique(disttotplot);
y = transpose(histc(disttotplot(:), x));
f = fit(x.',y.','gauss1');
g = fit(x.',y.','gauss2');
histogram(disttotplot);%, xbins);
%%%histogram(nonzeros(disttotplot),xbins);
% see if Gauss2 is necessary:
% regenerate data from gaussian fit
tot = 0;
for i = x
tot = tot + f(i);
end
prob = 0*x;
probg = 0*x;
for i = x+1
prob(i) = f(i-1)/tot;
probg(i) = g(i-1)/tot;
end
distances = 0*x;
distances2 = 0*x;
distances_actual = 0*x;
for i = 1:length(disttotplot)
% choose the location of kilobot as a point in the prob distribution
loc = rand;
distances_actual(abs(disttotplot(i))+1) = distances_actual(abs(disttotplot(i))+1) + 1;
for j = x+1
% incremement the relevant distance
loc = loc - prob(j);
loc2 = loc - probg(j);
% if the number lies within the distribution relevant to a certain
% distance, score that distance
if loc < 0
distances(j) = distances(j) + 1;
loc = 10; % reset to any number more than 1, so it doesn't score twice
end
if loc2 < 0
distances2(j) = distances2(j) + 1;
loc2 = 10; % reset to any number more than 1, so it doesn't score twice
end
end
end
% see the predictions if you want
%figure;
%bar(x,distances)
%figure;
%bar(x,distances2)
%figure;
%bar(x,distances_actual)
g1 = sqrt(sum((distances_actual - distances).^2));
g2 = sqrt(sum((distances_actual - distances2).^2));
% if using 2 peaked Gaussian significantly improves the fit, use 2 peaked
% Gaussian
if g1*0.8 > g2
f = g;
end
plot(x,f(x));
coverage = zeros(1,x(end)+4);
area = zeros(1,x(end)+4);
area(1) = pi*0.33^2;
for i = 1:x(end)+4
area(i+1) = pi*((0.33*(i+1))^2-(0.33*(i))^2);
end
for i = x+1
coverage(i) = coverage(i) + distances(i)*(area_cross(0,1) - area_cross(1/6,1))/area(i);
coverage(i+1) = coverage(i+1) + distances(i)*(area_cross(1/6,1)-area_cross(1/6+1/3,1))/area(i+1);
coverage(i+2) = coverage(i+2) + distances(i)*(area_cross(1/6+1/3,1)-area_cross(1/6+2/3,1))/area(i+2);
coverage(i+3) = coverage(i+3) + distances(i)*(area_cross(1/6+2/3,1)-area_cross(1,1))/area(i+3);
end
%}
%figure(16)
disp("plotting travelled distances")
set(0, 'currentfigure', fh(16));
mean_bold = zeros(1,n);
for i = 1:n
mean_bold(i) = mean(boldtotplot(i,:));
end
scatter(mean_bold,travelled_tots);
y=polyfit(mean_bold,travelled_tots,1);
hold on
plot(mean_bold,mean_bold*y(1)+y(2));
ylim([0 1+round(max(travelled_tots*1.1))]);
xlim([0 1+round(max(mean_bold*1.1))]);
%figure(17)
disp("plotting travelled distances")
set(0, 'currentfigure', fh(17));
mean_targ = zeros(1,n);
for i = 1:n
mean_targ(i) = mean(targtotplot(i,:));
end
scatter(mean_targ,travelled_tots);
ylim([0 1+round(max(travelled_tots*1.1))]);
xlim([0 1+round(max(mean_targ*1.1))]);
y=polyfit(mean_targ,travelled_tots,1);
hold on
plot(mean_targ,mean_targ*y(1)+y(2));
disp("saving files")
saveas(fh(17), name + "17", 'fig')
saveas(fh(17), name + "17", 'png')
disp("plotting radial/tangential angle")
set(0, 'currentfigure', fh(6));
polarhistogram(VECANG, 'BinWidth',0.125)
title("trajectory angles for " + num2str(n) + " kilobots")
disp("saving files")
saveas(fh(1), name + "1", 'fig')
saveas(fh(1), name + "1", 'png')
saveas(fh(2), name + "2", 'png')
saveas(fh(2), name + "2", 'fig')
saveas(fh(3), name + "3", 'fig')
saveas(fh(3), name + "3", 'png')
saveas(fh(4), name + "4", 'fig')
saveas(fh(4), name + "4", 'png')
saveas(fh(5), name + "5", 'fig')
saveas(fh(5), name + "5", 'png')
saveas(fh(6), name + "6", 'fig')
saveas(fh(6), name + "6" + num2str(count/n) + ".png", 'png')
saveas(fh(7), name + "7", 'fig')
saveas(fh(7), name + "7", 'png')
saveas(fh(8), name + "8", 'fig')
saveas(fh(8), name + "8", 'png')
saveas(fh(11), name + "11", 'fig')
saveas(fh(11), name + "11", 'png')
saveas(fh(12), name + "12", 'fig')
saveas(fh(12), name + "12", 'png')
saveas(fh(13), name + "13", 'fig')
saveas(fh(13), name + "13", 'png')
saveas(fh(14), name + "14", 'fig')
saveas(fh(14), name + "14", 'png')
saveas(fh(15), name + "15", 'fig')
saveas(fh(15), name + "15", 'png')
saveas(fh(16), name + "16", 'fig')
saveas(fh(16), name + "16", 'png')
fh(9) = figure('visible', 'off');
xlabel("distance"); ylabel("time total");
hold on
set(0, 'currentfigure', fh(9));
first = zeros(1,n*100);
disp("making steady state gif")
hold off
%{
fs = [];
gs = [];
ferrs = [];
gerrs = [];
% slice into 100s sections:
slice = 100;
sections = round((length(disttotplot)/n-50)/slice);
for k = 1:sections
disp("plotting time " + num2str(k*slice))
% for each kilobot
for i = 1:n
% for each second in the current 100s slice
%for j = (100*k)-99:100*k
for j = 1:slice
%first((i-1)*n+j) = disttotplot((i-1)*length(disttotplot)/n+j);
first((i-1)*slice+j) = disttotplot((k-1)*slice+round(length(disttotplot)/n)*(i-1)+j);
%first((i-1)*j+j) = disttotplot(((i-1)*round(length(disttotplot)/n-50)/100)+j);
end
end
Y = [min(disttotplot)-1:max(disttotplot)+1];
%h = histogram(first((k-1)*n*100+1:k*n*100), 'BinEdges', [Y(1)-0.5 Y+0.5]);
h = histogram(first, 'BinEdges', [Y(1)-0.5 Y+0.5]);
hold on
a = unique(first);
b = transpose(histc(first(:), a));
%g = fit(a.',b.','gauss1');
g = fit(a.',b.','gauss2');
plot(a,g(a));
plot(x,f(x)./sections);
legend({"Data", "centroid = " + num2str(f.b1) + ", amplitude = " + num2str((f.a1)/sections) + ", width = " + num2str(f.c1),"centroid = " + num2str(g.b1) + ", amplitude = " + num2str((g.a1)) + ", width = " + num2str(g.c1)},'Location','northeast')
hold off
fs = [fs f.b1];
ferrs = [ferrs f.c1/2];
gs = [gs g.b1];
gerrs = [gerrs g.b1/2];
%length(first((k-1)*n*100+1:k*n*100))
ylim([0 1000]);
%display tot above each column
for i = 1:length(Y)
h(1).Values;
val = num2str(h(1).Values(i));
if h(1).Values(i) == 0
val = "";
end
text(Y(i),h(1).Values(i),val,'vert','bottom','horiz','center');
end
title("Distribution at " + num2str(k*slice) + " seconds");
if k == 1
gif([char(name) ' reaching steady state.gif'],'DelayTime',0.2, 'frame', gcf);
else
gif
end
end
%{
% for plotting quarter of the swarm's distribution every 100s
set(0, 'currentfigure', fh(9));
first = zeros(1,(n/4)*100);
disp("making steady state quarters gif")
hold off
fs = [];
gs = [];
ferrs = [];
gerrs = [];
% slice into 100s sections:
slice = 100;
sections = round((length(disttotplot)/n-50)/slice);
overall = [];
for k = 1:sections
disp("plotting time " + num2str(k*slice))
% for each kilobot
for i = 1:n/4
% for each second in the current 100s slice
%for j = (100*k)-99:100*k
for j = 1:slice
%first((i-1)*n+j) = disttotplot((i-1)*length(disttotplot)/n+j);
first((i-1)*slice+j) = disttotplot((k-1)*slice+round(length(disttotplot)/n)*(i-1)+j);
%first((i-1)*j+j) = disttotplot(((i-1)*round(length(disttotplot)/n-50)/100)+j);
end
end
Y = [min(disttotplot)-1:max(disttotplot)+1];
%h = histogram(first((k-1)*n*100+1:k*n*100), 'BinEdges', [Y(1)-0.5 Y+0.5]);
h = histogram(first, 'BinEdges', [Y(1)-0.5 Y+0.5]);
hold on
a = unique(first);
b = transpose(histc(first(:), a));
%g = fit(a.',b.','gauss1');
g = fit(a.',b.','gauss2');
plot(a,g(a));
plot(x,f(x)./sections);
legend({"Data", "centroid = " + num2str(f.b1) + ", amplitude = " + num2str((f.a1)/sections) + ", width = " + num2str(f.c1),"centroid = " + num2str(g.b1) + ", amplitude = " + num2str((g.a1)) + ", width = " + num2str(g.c1)},'Location','northeast')
hold off
fs = [fs f.b1];
ferrs = [ferrs f.c1/2];
gs = [gs g.b1];
gerrs = [gerrs g.b1/2];
%length(first((k-1)*n*100+1:k*n*100))
ylim([0 1000]);
%display tot above each column
for i = 1:length(Y)
h(1).Values;
val = num2str(h(1).Values(i));
if h(1).Values(i) == 0
val = "";
end
text(Y(i),h(1).Values(i),val,'vert','bottom','horiz','center');
end
title("Distribution at " + num2str(k*slice) + " seconds");
if k == 1
gif([char(name) ' reaching steady state quarter.gif'],'DelayTime',0.2, 'frame', gcf);
else
gif
end
overall = [overall first];
if k == 15 || k == 30 || k == 49
histogram(overall);
saveas(fh(9), name + num2str(k), 'png')
overall = [];
end
end
%}
%figure; ylim([0 25]); hold on; errorbar(1:length(fs), fs, ferrs); errorbar(1:length(gs), gs, gerrs);
fh(10) = figure('visible', 'off');
xlabel("time"); ylabel("Gauss centroid");
hold on
set(0, 'currentfigure', fh(10));
disp("plotting steady state")
hold on
ylim([0 25]);
plot([1:length(fs)].*100, fs, [1:length(gs)].*100, gs);
saveas(fh(10), name + "10", 'fig')
saveas(fh(10), name + "10", 'png')
%}
save(name + " variables")
disp("average number of targets met = " + num2str(count/n))
disp("finished")
end