Simulate predator prey system using loops

14 vues (au cours des 30 derniers jours)
Nathan Bhoedjang
Nathan Bhoedjang le 23 Avr 2020
I've written a code to simulate a predator-prey system in MATLAB (code below). I want to simulate the movements of each animal, but I don't know how to turn the output of a loop into a simulation. I'm quite new to MATLAB so some help would be appreciated. To clear things up: I've put each animal in a structure and put those in two maps: one for preys and one for predators.
% VARIABELEN
numDays = 50;
probbrirthPrey = 0.06;
probbirthPred = 0.04;
birthagePrey = 10;
birthagePred = 10;
axis = 10;
W = 0:1:axis;
rpred = 2;
rprey = 1.5;
preyid = 0;
predid = 0;
% MAPPEN MET INITIËLE DIEREN
predMap = containers.Map('KeyType', 'int64', 'ValueType', 'any');
pred = struct('x', rand * axis, 'y', rand * axis, 'age', 25, 'honger', 0);
predid = predid + 1;
predMap(predid) = pred;
preyMap = containers.Map('KeyType', 'int64', 'ValueType', 'any');
prey = struct('x', rand * axis, 'y', rand * axis, 'age', 15);
preyid = preyid + 1;
preyMap(preyid) = prey;
prey = struct('x', rand * axis, 'y', rand * axis, 'age', 20);
preyid = preyid + 1;
preyMap(preyid) = prey;
fields = {'x', 'y', 'age'};
% DAGEN
for t = 1:numDays
disp(['---> DAG ', num2str(t),':']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PROOIDIEREN (PREYS)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Prooidieren:');
for p = keys(preyMap)
thep = p{1};
fprintf(' Prooidier %d\n',thep);
newPrey = preyMap(thep);
fprintf(' Coördinaten: (%.2f, %.2f)', newPrey.x, newPrey.y)
% COÖRDINATEN
d = rand * rprey;
rad = rand * 2 * pi;
newpreyx = newPrey.x + cos(rad) * d;
newpreyy = newPrey.y + sin(rad) * d;
while newpreyx<0 || newpreyx>axis || newpreyy<0 || newpreyy>axis
d = rand * rprey;
rad = rand * 2 * pi;
newpreyx = newPrey.x + cos(rad) * d;
newpreyy = newPrey.y + sin(rad) * d;
end
newPrey.x = newpreyx;
newPrey.y = newpreyy;
% LEEFTIJD
newPrey.age = newPrey.age + 1;
% WEERGAVE
Plaats = sprintf(' Beweegt naar: (%.2f, %.2f)', newPrey.x, newPrey.y);
disp(Plaats)
Leeftijd = sprintf(' Leeftijd: %d', newPrey.age);
disp(Leeftijd)
% GEBOORTE
if newPrey.age > birthagePrey
out=rand;
if out<probbrirthPrey
child = struct('x', W(randi([1,numel(W)])), 'y', W(randi([1,numel(W)])), 'age', 0);
preyid = preyid + 1;
preyMap(preyid) = child;
fprintf(' Prooidier %d krijgt kind %d', thep, preyid);
end
end
preyMap(thep) = newPrey;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ROOFDIEREN (PREDATORS)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Roofdieren:');
for q = keys(predMap)
theq = q{1};
fprintf(' Roofdier %d\n',theq);
newPred = predMap(theq);
fprintf(' Coördinaten: (%.2f, %.2f)', newPred.x, newPred.y)
newPred.honger = newPred.honger + 1;
% COÖRDINATEN
d = rand * rpred;
rad = rand * 2 * pi;
newpredx = newPred.x + cos(rad) * d;
newpredy = newPred.y + sin(rad) * d;
while newpredx<0 || newpredx>axis || newpredy<0 || newpredy>axis
d = rand * rpred;
rad = rand * 2 * pi;
newpredx = newPred.x + cos(rad) * d;
newpredy = newPred.y + sin(rad) * d;
end
newPred.x = newpredx;
newPred.y = newpredy;
% LEEFTIJD
newPred.age = newPred.age + 1;
% WEERGAVE
Plaats = sprintf(' Beweegt naar: (%.2f, %.2f)', newPred.x, newPred.y);
disp(Plaats)
Leeftijd = sprintf(' Leeftijd: %d', newPred.age);
disp(Leeftijd)
% GEBOORTE
if newPred.age > birthagePred
out=rand;
if out<probbirthPred
pred = struct('x', W(randi([1,numel(W)])), 'y', W(randi([1,numel(W)])), 'age', 0, 'honger', 0);
predid = predid + 1;
predMap(predid) = pred;
fprintf(' Roofdier %d krijgt kind %d', theq, predid);
end
end

Réponses (1)

Muthaffar A-Jassim
Muthaffar A-Jassim le 23 Avr 2020
x^3+3*x-2*x-6

Catégories

En savoir plus sur Animation dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by