Clear Filters
Clear Filters

I am trying to run a forest fire simulation. However, when I run the following code in Matlab, the plot window is blank. What do i do?

4 views (last 30 days)
This is the code:
function y = fire_sim(N,p)
figure(1);
%sets size of forest NxN
C = ones(N,N);
Z = zeros(N,N);
x = 1:N;
y = 1:N;
%Set initial fire
fires = [floor(N/2),floor(N/2)];
[numfires,temp] = size(fires);
for i = 1:numfires
C(fires(i,1),fires(i,2))=2;
end
%Simulate spread
maxsteps = 1000; %number of iterations to plot
k=0;
while k<maxsteps && numfires>0
lastC = C;
numfires_next = 0;
fires_next = [];
for i = 1:numfires
if fires(i,1)~=1
if C(fires(i,1)-1,fires(i,2))==1
r = rand(1);
if r < p
numfires_next = numfires_next + 1;
C(fires(i,1)-1,fires(i,2))=2;
fires_next(numfires_next,:)=[fires(i,1)-1,fires(i,2)];
end
end
end
if fires(i,1)~=N
if C(fires(i,1)+1,fires(i,2))==1
r = rand(1);
if r < p
numfires_next = numfires_next + 1;
C(fires(i,1)+1,fires(i,2))=2;
fires_next(numfires_next,:)=[fires(i,1)+1,fires(i,2)];
end
end
end
if fires(i,2)~=1
if C(fires(i,1),fires(i,2)-1)==1
r = rand(1);
if r < p
numfires_next = numfires_next + 1;
C(fires(i,1),fires(i,2)-1)=2;
fires_next(numfires_next,:)=[fires(i,1),fires(i,2)-1];
end
end
end
if fires(i,2)~=N
if C(fires(i,1),fires(i,2)+1)==1
r = rand(1);
if r < p
numfires_next = numfires_next + 1;
C(fires(i,1),fires(i,2)+1)=2;
fires_next(numfires_next,:)=[fires(i,1),fires(i,2)+1];
end
end
end
C(fires(i,1),fires(i,2))=0;
end
surf(x,y,C,C);
view(360,90);
colormap([0 0 0; 0 1 0; 1 0 0])
caxis([0 2]);
axis([1,length(x),1,length(y),0,2])
shading interp
title(sprintf('Step = %.0f',k))
drawnow;
k=k+1;
if sum(sum(abs(lastC-C)))==0
break
end
fires = fires_next;
numfires = numfires_next;
end

Accepted Answer

Walter Roberson
Walter Roberson on 6 Mar 2016
The code works for me in R2014a. Run it like
fire_sim(300,.5);
A parameter of .5 creates some nice designs about half the runs (and dies out quickly the other half of the runs.)
  4 Comments
Walter Roberson
Walter Roberson on 11 May 2017
Change
C = ones(N,N);
to
if ~exist(N, 'var'); N = 300; fprintf('Defaulting to 300 x 300. Next time, pass the size as the first parameter. Do not just click Run, use the command line.\nDo not just click Run, use the command line.\nLike fire_sim(300, .5); at the command line.'); end
if ~exist(p, 'var'); p = 0.5; fprintf('Defaulting to p = 0.5. Next time, pass the probability as the parameter. \n'); end
C = ones(N,N);
Karri Rama
Karri Rama on 21 Nov 2019
@walter
I used fire_sim(300,.5)
I am not getting 3D plot, but getting nice 2D plot..
what changes should i make to get 3D plot

Sign in to comment.

More Answers (2)

Alex chavez
Alex chavez on 16 Jun 2017
What is this for loop doing since numfires equals one and what is actually happening inside the loop with this line "C(fires(i,1),fires(i,2))=2;"?
for i = 1:numfires
C(fires(i,1),fires(i,2))=2;
end

Aaron Staszewski
Aaron Staszewski on 26 Mar 2020
im trying to do something similar and was wondering how / which lines of code made sure that the red fire dots did not spread back over the already burnt grey area?

Community Treasure Hunt

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

Start Hunting!