参考链接:https://blog.csdn.net/weixin_30278943/article/details/112067158
function out = my_laplacian(in)
out = -in + .20*(circshift(in,[ 1, 0]) + circshift(in,[-1, 0])...
+ circshift(in,[ 0, 1]) + circshift(in,[ 0,-1]))...
+ .05*(circshift(in,[ 1, 1]) + circshift(in,[-1, 1])...
+ circshift(in,[-1,-1]) + circshift(in,[ 1,-1]));
end
function [t, A, B] = initial_conditions(n)
t = 0;
A = ones(n);
B = zeros(n);
B(51:60 ,51:70) = 1;
B(61:80,71:80) = 1;
end
clc
clear all
close all
% 设置系数值
f=0.055; %进料率
k=0.062; %去除率
da = 1.0; %U的扩散率
db = 0.5; %V的扩散率
% 网格的大小
width = 128;
%5,000个模拟秒,每模拟秒4步
dt = 0.25;
stoptime = 5000;
% 初始化矩阵
[t, A, B] = initial_conditions(width);
% 运行主程序
tic
nframes = 1;
while t<stoptime
anew = A + (da*my_laplacian(A) - A.*B.^2 + f*(1-A))*dt;
bnew = B + (db*my_laplacian(B) + A.*B.^2 - (k+f)*B)*dt;
A = anew;
B = bnew;
t = t+dt;
nframes = nframes+1;
end
%画图
figure('name','Tuling')
axis off
hi = image(B);
hi.CDataMapping ='scaled';
colorbar;
hold on
delta = toc;
disp([num2str(nframes) 'frames in ' num2str(delta) ' seconds']);
clc
clear all
close all
% 设置系数值
f=0.055; %进料率
k=0.062; %去除率
da = 1.0; %U的扩散率
db = 0.5; %V的扩散率
% 网格的大小
width = 128;
%5,000个模拟秒,每模拟秒4步
dt = 0.25;
stoptime = 5000;
% 初始化矩阵
[t, A, B] = initial_conditions(width);
targetframerate = 24;
frametime = 1/(24*60*60*targetframerate);
nextframe = now + frametime;
tic
nframes = 1;
B_cell = cell(0);
while t<stoptime
anew = A + (da*my_laplacian(A) - A.*B.^2 + f*(1-A))*dt;
bnew = B + (db*my_laplacian(B) + A.*B.^2 - (k+f)*B)*dt;
A = anew;
B = bnew;
B_cell(nframes)={B};
t = t+dt;
ht.String = ['Time = ' num2str(t)];
if now > nextframe
drawnow
nextframe = now + frametime;
end
nframes = nframes+1;
end
delta = toc;
disp([num2str(nframes) ' frames in ' num2str(delta) ' seconds'])
figure('name','Tuling')
imageB = B_cell{1};
h=image('CData',imageB);
h.CDataMapping = 'scaled';
colorbar;
hold on
for i=5:5:20000
cell_B = B_cell{i};
set(h,'CData', cell_B);
drawnow;
end