Exploring the 2D GrayScott Model with MATLAB

In summary, the conversation discusses the attempt to reproduce a 2D GrayScott model using MATLAB code from different sources. The goal is to create a screen saver, so the focus is on the visual results rather than the mathematics behind it. The conversation includes a code snippet and parameters that were found to create a self-replicating spots simulation, but it is noted that it is not suitable for the initial goal due to the heavy calculation involved. The conversation also mentions trying Mike's code and the issue of not being able to see waves, as well as the desire to replicate the self-replicating spots shown on another webpage. The conversation concludes with the mention of using an older version of MATLAB and the appreciation for Mike's blog in
  • #1
bolly
16
2
Dear Community,

I was trying to reproduce the 2D GrayScott model given either here: http://blogs.mathworks.com/graphics/2015/03/16/how-the-tiger-got-its-stripes/

or here: http://www.joakimlinde.se/java/ReactionDiffusion/index.php?size=0

The reason was to create a nice screen-saver (so I am not really interested in the mathematics behind). So I tried to use MATLAB for a first trial however my interpretation of the given MATLAB code (first URL) neither gives results as shown in both the urls. I would be very happy if someone could check the following code – maybe I just forget something important:function [t, A, B] = initial_conditions(n)
t = 0;
% Initialize A to one
A = zeros(n); %ones(n);
% Initialize B to zero which a clump of ones
B = zeros(n);
B(50:80 ,50:80) =10;
% A(50:80 ,50:80) =6;
%B(61:80,71:80) = 20;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
figure;
width = 128;
[t, A, B] = initial_conditions(width);

% Setup image
ih=imagesc(B);
set(ih,'cdatamapping','direct')
%colormap(hot);
axis image off;
th=title('');
set(gcf,'position',[80 70 512 512],'color',[1 1 1],'menubar','none')
% Create 'Quit' pushbutton in figure window
uicontrol('units','normal','position',[.45 .02 .13 .07], ...
'callback','set(gcf,''userdata'',1)',...
'fontsize',10,'string','Quit');
f=.55;
k=.062;

da = 1;
db = 0.1;
dt = .001;
done = 0;
count = 0;
while ~done
% 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;
%dAdt= A + (da*del2(A) - A.*B.^2 + f*(1-A))*dt;
% dbBdt= B + (db*del2(B) + A.*B.^2 - (k+f)*B)*dt;

dAdt = da*del2(A) - A.*B.*B + f*(1-A);
dBdt= db*del2(B) + A.*B.*B - (k+f)*B;%- k*B;

B = B + dBdt*dt;
A = A + dAdt*dt;

% avoid overflow
if A > 1000
done = 1;
A(60,60)
'over'
end
if A < 5.5000e-005
done = 1;
A(60,60)
'over'
end

if(mod(count,50) == 40)
set(ih,'cdata',A); %,B);
set(th,'string',sprintf('%d %0.2f %0.2f',count,A(60,60),B(60,60)));
drawnow;
end;
count = count + 1;
if ~isempty(get(gcf,'userdata')), done=1;
end % Quit if user clicks on 'Quit' button.
end
 
Physics news on Phys.org
  • #2
Mike's blog gives the code. Why not just use his code?
 
  • #3
Dear Community,

after try- and error-ing a while I finally found a suitable set of parameters which define the most fascinating manifestation of the GrayScott model: The self replicating spots- simulation. However since it takes a lot of calculation this approach is not well suited for my initial goal to make a screen saver out of it– however if someone else likes to experiment himself a little bit – here is a working code snipped (copied from here and there - majorly from Mikes Blog).

function [t, A, B] = initial_conditions(n)
t = 0;
% Initialize A to one
A = ones(n);
% Initialize B to zero which a clump of ones
B = zeros(n);
B(51:60 ,51:70) = 1;
B(61:80,71:80) = 1;

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]));

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)f = 0.02;
k = 0.079;% Diffusion rates
da = 1;
db = 0.4;%0.5;%.5;
% Size of grid
width = 128;


dt = .25; %.15
[t, A, B] = initial_conditions(width);
figure;
% Setup image
ih=imagesc(B);

set(ih,'cdatamapping','direct')
%colormap(hot);
axis image off;
th=title('');
set(gcf,'position',[80 70 512 512],'color',[1 1 1],'menubar','none')
% Create 'Quit' pushbutton in figure window
uicontrol('units','normal','position',[.45 .02 .13 .07], ...
'callback','set(gcf,''userdata'',1)',...
'fontsize',10,'string','Quit');
% Add a text object to show the current time.
ht = text(3,width-3,'Time = 0');
ht.Color = [.95 .2 .8];
done = 0;

%count = 0;
while ~done
anew = A + (da*my_laplacian(A) - A.*B.^2 + f*(1-A))*dt;
bnew = B + (db*my_laplacian(B) + A.*B.^2 - k*B) *dt; %;- (k+f)*B)*dt;
A = anew;
B = bnew;
t = t+dt;
if(mod(count,100) == 40)
set(ih,'cdata',B.*30); %,B); %multiplying B with a factor was important for visualization
drawnow;
end;
% count = count + 1;
if ~isempty(get(gcf,'userdata')), done=1;
end % Quit if user clicks on 'Quit' button.
end


@ Image analyst: I certainly tried Mikes code but using my old Matlab 6.5 there were no waves visible (probably because ‘image’ has no autoscale feature). Furthermore I wanted ‘Selfreplicating spots’ shown on Joakim Lindes’ page ( – however without Mikes great Blog I wouldn’t be able to run a GrayScott model myself).
 

1. What is the 2D GrayScott Model?

The 2D GrayScott Model is a mathematical model used to simulate the reaction-diffusion dynamics of a chemical system. It consists of two interacting chemical species that diffuse and react with each other over a 2D grid.

2. What is MATLAB?

MATLAB is a high-level programming language and interactive environment commonly used in scientific and engineering applications. It allows for easy visualization and manipulation of data and has a wide range of built-in functions for mathematical and statistical analysis.

3. How can MATLAB be used to explore the 2D GrayScott Model?

MATLAB can be used to simulate and visualize the behavior of the 2D GrayScott Model by implementing the necessary equations and parameters. It also allows for the manipulation of various model parameters to observe their effects on the system.

4. What are some potential applications of the 2D GrayScott Model?

The 2D GrayScott Model has been used to study various biological and chemical processes, such as pattern formation in animal coats and the formation of complex chemical structures. It has also been used in materials science and engineering to understand the formation of surface patterns and structures.

5. Is the 2D GrayScott Model an accurate representation of real-world systems?

While the 2D GrayScott Model can provide valuable insights into the behavior of reaction-diffusion systems, it is a simplified representation and may not accurately reflect all aspects of real-world systems. It should be used as a tool for understanding general principles and behaviors rather than making specific predictions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
12
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Quantum Physics
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
Back
Top