function SimpleFireSpread(N,P,S)
%QQ此│707914447
%% SIMPLE FOREST FIRE SPREAD
%Function models a forest fire with each tree on fire has a set probability
%to ignite a tree which hasn't been burnt in its Moore Neighbourhood.
%The output is an animated graph showing the fire spread.
%% DEFINING INPUT
%N - The size of the matrix on which the forest will be grown (N*N)
%P - The average percantage of the matrix that will be forested (A P%
%chance for each tree to grow)
%S - The probability (as a percentage) of a fire to spread to neighbouring trees
%% INPUT ERRORS
if N < 1 || floor(N) ~= N %Invalid size
error('No valid input for size. Must be a positive integer.')
end
if P < 0 || floor(P) ~= P || P>100 %Invalid percent coverage
error('No valid input for percent coverage. Must be an integer value from 0 to 100')
end
if S < 0 || floor(S) ~= S || S>100 %Invalid spread probability
error('No valid input for spread probability. Must be an integer value from 0 to 100')
end
%% SETUP
F=Forest(N,P); %Randomly plotting a forest to burn
Spread=[0 1; 1 1; 1 0; 1 -1; 0 -1; -1 -1; -1 0; -1 1]; %Possible spreads (Moore Neighbourhood)
t=0; %Initial fire occurs at timestep 0
f=find(~F); %Finding all trees
F(f(randi(length(f))))=1; %Starting a fire on a random tree
%% LOOPS
while ~isempty(find(F==1,1)) %Loop only runs while there are fires
[i,j]=find(F==1); %Coordinates of the fire
for x=1:length(i) %For each fire
for M=1:8 %Checking each spreading option
try %Makes it so off grid checks don't cause an error
if F(i(x)+Spread(M),j(x)+Spread(M+8))==0 %Checking for trees
if randi([1,100])<= S %Chance a tree will ignite
F(i(x)+Spread(M),j(x)+Spread(M+8))=1; %Fire spreads to found trees
end
end
end
F(i(x),j(x))=2; %Trees that have been burned
end
end
imagesc(F); %The updated forest
drawnow; %Updating the plot each loop
title(['t = ' num2str(t)]) %Time since start of fire to be seen on the plot
t=t+1; %Timesteps spent burning
end
阿里matlab建模师
- 粉丝: 3525
- 资源: 2793
最新资源
- 微软AICopilot上线助推生成式AI发展,光通信产业链迎高增长契机
- AIGC技术落地进程加快,传媒行业迎来发展机遇
- 2023年上半年通信行业报告:新基建与AI驱动的结构性成长机会
- CRMRESTBuilder-2-5-0-0-managed.zip
- AIGC技术对中国智能投顾市场的推进与应用前景分析
- 生成式AI助力商汤亏损收窄:各板块业绩分析与展望
- 生成式AI商业应用加速,通信行业迎新机遇
- 基于spring+SQL Serve+jsp实现的网上订餐系统【源码+数据库】
- 生成式AI:企业CEO的应用指南及风险考量
- EasyPlayer.wasm EasyPlayer-element.min.js
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈