# 【元胞自动机】基于matlab元胞自动机生态养殖【含Matlab源码 657期】

2021年09月15日 阅读数：8

1 模型介绍

2 模型的参数

2.1 Moore型邻居

2.2 具体参数设定
ide

## 2、源代码

``````clear;
clc;
rho=50;length=50;
fish=[500 300 500];%300kg鱼、500kg虾、500kg蟹
lambda=0.8;
T=10;%10周
alpha=[0.00001,0.00015,0.00035,0.00025,0.0002];%不一样状态下的鱼儿们消耗系数
range=[15,50,150,500];
%==========画方块图
area=MyCreate(length,rho,lambda);
DrawArea(area,range);
n=length;%n=50
plot([(0:n)',(0:n)']+0.5,[0,n]+0.5,'k');%k表明黑色
plot([0,n]+0.5,[(0:n)',(0:n)']+0.5,'k');
axis image;
set(gca,'xtick',[]);
set(gca,'ytick',[]);%消除x,y轴
figure;
%=========
data=zeros(T,3);%10行3列的零
for i=1:T
[area,fish]=MyChange(area,fish,range,alpha);
DrawArea(area,range);
drawnow;
data(i,:)=fish;
end
function [area,fish]=MyChange(area,fish,range,alpha)
n=length(area);
for i =1:n
for j=1:n
%周围9个方块共同impact
if MyRound(area,i,j)<range(1)%15
area(i,j)=MyRound(area,j,j)*(1-rand*mean(fish)*alpha(1));
%(1-rand*mean(fish)*alpha(1))鱼虾蟹等影响系数
elseif MyRound(area,i,j)<range(2)%50
area(i,j)=MyRound(area,j,j)*(1-rand*mean(fish)*alpha(2));
elseif MyRound(area,i,j)<range(3)%150
area(i,j)=MyRound(area,j,j)*(1-rand*mean(fish)*alpha(3));
elseif MyRound(area,i,j)<range(4)%500
area(i,j)=MyRound(area,j,j)*(1-rand*mean(fish)*alpha(4));
else
area(i,j)=MyRound(area,j,j)*(1-rand*mean(fish)*alpha(5));
end
end
end
avg=mean(mean(area));
%成长系数设置的很迷惑
%蟹类主要以鱼虾等动物尸体为食物，因此其数目会受到鱼虾的影响
function DrawArea(area,range)
temp=area;
n=length(temp);
Area(:,:,1)=temp;
Area(:,:,2)=temp;
Area(:,:,3)=temp;
%给每一个方格块上色，水藻密度越大，颜色越绿
for i=1:n
for j=1:n
if temp(i,j)<range(1)
Area(i,j,:)=[0 230 214];
elseif temp(i,j)<=range(2)
Area(i,j,:)=[139 255 223];
elseif temp(i,j)<=range(3)
Area(i,j,:)=[147 255 170];
elseif temp(i,j)<=range(4)
Area(i,j,:)=[91  240 91];
else
Area(i,j,:)=[4 176 58];
end
end
end
``````

spa