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?

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

 Risposta accettata

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 Commenti

Hello, I tried this fix and it worked but only for the 2-D version but I could not figure out how to run the 3-D version of the forest fire. When I try to run it I get a blank graph and this error message: Error using fire_sim(line 4) Not enough input arguments. My line 4 looks like this C = ones(N,N);
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);
@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

Accedi per commentare.

Più risposte (2)

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
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?

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by