I am relatively new to Matlab. I am aware how to use the hold functions to do basic plots however I can't work out how to implement this into the following code to achieve multiple sets of data but on the same plot. Any help will be greatly appreciated.
Edit: Some code such as 'neighbours' are functions I have created so if some of the code seems strange, that may also be why. - If anyone needs to see these in order to help please let me know. Once again thanks!
Edit 2: I have added all the code you need to run the experiment. If you do this you will see what I am trying to achieve (hopefully) - Thanks again for the responses! __________________________________________________________________________
Firstly the Neighbours function (save this as a function):
function nbrs = neighbours(m, n, bdry, conn, prnt)
switch bdry
case 'closed'
I = [[NaN 1:m-1]; 1:m; [2:m NaN]];
J = [[NaN 1:n-1]; 1:n; [2:n NaN]];
case 'torus'
I = [[m 1:m-1]; 1:m; [2:m 1]];
J = [[n 1:n-1]; 1:n; [2:n 1]];
end
function k = ind(i1, j1, i, j)
k = sub2ind([m n], I(i1, i), J(j1, j));
end
nbrs = {};
for k = 1:m*n
[i j] = ind2sub([m n], k);
switch conn
case '4'
nbr = [ind(1, 2, i, j) ind(2, 1, i, j),ind(2, 3, i, j), ...
ind(3, 2, i, j)];
case '8'
nbr = [ind(1, 1, i, j), ind(1, 2, i, j), ind(1, 3, i, j), ...
ind(2, 1, i, j),ind(2, 3, i, j), ...
ind(3, 1, i, j), ind(3, 2, i, j), ind(3, 3, i, j)];
end
nbrs{k} = nbr(~isnan(nbr));
end
if prnt == 1
for k = 1:length(nbrs)
fprintf('%5d :', k); fprintf('%5d ', nbrs{k}); fprintf('\n')
end
end
end
___________________________________________________________________
Here is the code for the function rand_binary
function im = rand_binary(m, n, prob)
im = double(rand(m, n) > prob);
__________________________________________________________________
And run_simulation function:
function [pop2, xagg] = run_simulation(pop1, sf, nbrs, niter, plt)
if nargin < 5
plt = 1;
end
[m n] = size(pop1);
if plt
plot_world(pop1, sf);
drawnow
end
a = 0;
p = 1;
pop2 = pop1;
for i = 1:niter
for k = 1:m*n
nbr = nbrs{k};
na = sum(pop1(nbr) == a);
np = sum(pop1(nbr) == p);
if sf(k) == 0
if na >= 1
pop2(k) = a;
else
pop2(k) = p;
end
else
if na > np
pop2(k) = a;
elseif na < np
pop2(k) = p;
end
end
end
pop1 = pop2;
if plt
plot_world(pop1, sf);
title(sprintf('Day %d', i+1));
pause(0.2)
end
end
xagg = sum(pop2(:) == a)/(m*n);
____________________________________________________________________
Now this is the code to plot my graph. If you run this data as it is it should plot a smooth sigmoidal curve for a single sf value. However I want to compare the effect of different sf values by plotting their results on the same graph.
clear all
m = 100;
n = 100;
nbrs = neighbours(m, n, 'closed', '4', 0);
nsamp = 1000;
pagg = linspace(0, 1, nsamp);
xagg = NaN(1, nsamp);
psf = 0.2;
sf = rand_binary(m, n, psf);
ndays = 20;
niter = ndays-1;
plt = 0;
figure(3); clf
for i = 1:nsamp
pop1 = rand_binary(m, n, pagg(i));
[pop2, xagg(i)] = run_simulation(pop1, sf, nbrs, niter, plt);
figure(3);
plot(pagg, xagg, '.', 'MarkerSize', 10)
xlabel('Initial Population Aggressive');
ylabel('Final Population Aggressive');
title('Parametric Study for Neighbourhood Connectivity')
axis([0 1 0 1]);
drawnow
end
Any help with this problem would be greatly appreciated (sorry i haven't been all that clear, struggling to wrap my head around the code and the way to verbally express it).
Many thanks again, John