-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotSwitchesAndPercentages.m
72 lines (61 loc) · 2.21 KB
/
plotSwitchesAndPercentages.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
% Nick Lavrov
%
% Runs the SPP Model and identifies each time the
% alignment switches, defining a 'switch point.'
% Then goes every (stepIncrement) back from that
% switch point up to (totalStepsBack) and reruns
% the simulation 30 times and tracks how many times
% a switch occurs, creating a switch fraction.
% Then plots the positions and alignment of the
% original simulation, the switch fraction at each
% switch point, and the average switch fraction
numLocusts = 30; % number of locusts
N = 5000; % number of steps
dt = 1; % size of step
r = 4; % interaction radius
length = 90; % length of domain
totalStepsBack = 40; % furthest to look before switch point
stepIncrement = 1; % how much to step back from switch point until total
% The main function call
[x,u,a,sp,spa,studied,switchPercentAtBlock] = ...
switchFrequency(numLocusts, N, dt, r, length,...
totalStepsBack, stepIncrement);
% plot positions
figure(1);
plot(x, '.');
xlabel('Time');ylabel('Position');
% plot alignment and switch markers
figure(2);
plot(a);hold on;plot(sp,spa,'red');hold off;
xlabel('Time');ylabel('Alignment');
numSwitches = size(switchPercentAtBlock,2);
% check if a switch occurred before plotting
if (numSwitches)
% plot the switch percent for each switch point for
% each step before the switch point
figure(3);
plot(0:stepIncrement:totalStepsBack,switchPercentAtBlock);
xlabel('Steps before switch point');
ylabel('Switch percentage');
ylim([0 1]);
switch numSwitches
case 1
legend(['t=' num2str(studied(1))]);
case 2
legend(['t=' num2str(studied(1))], ['t=' num2str(studied(2))]);
case 3
legend(['t=' num2str(studied(1))],...
['t=' num2str(studied(2))], ['t=' num2str(studied(3))]);
otherwise
legend(['t=' num2str(studied(1))],...
['t=' num2str(studied(2))], ['t=' num2str(studied(3))]);
end
if (numSwitches > 1)
% then plot the average switch percent
figure(4);
plot(0:stepIncrement:totalStepsBack,mean(switchPercentAtBlock.'));
xlabel('Steps before switch point');
ylabel('Average switch percentage');
ylim([0 1]);
end
end