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?
13 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2016-3-6
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 个评论
Walter Roberson
2017-5-11
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
2019-11-21
@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
更多回答(2 个)
Alex chavez
2017-6-16
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
1 个评论
Image Analyst
2017-6-16
I answered that in your other thread: https://www.mathworks.com/matlabcentral/answers/344976-what-is-this-for-loop-doing-since-numfires-equals-one-and-what-is-actually-happening-inside-the-loop#answer_270871 before I knew you also asked it here. So read it there.
Aaron Staszewski
2020-3-26
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?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Orange 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!