フィルターのクリア

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 ビュー (過去 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

採用された回答

Walter Roberson
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
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
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
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

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

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by