Dynamic faults detection in Mobile Ad-hoc Network

PROJECT VIDEO

ABSTRACT

Mobile Ad-Hoc Networks (MANETs) are set of mobile nodes that communicates wirelessly without a centralized supporting system. Faulty nodes a ect the reliable transmission of messages across the network. In this thesis we deal with the fault identi cation problem in static topology MANETs. A comparison based approach is used where a set of tasks is given to the nodes and outcomes are compared. Based on these comparisons the nodes are classi ed either as faulty or fault free. Our new diagnosis model is based on the spanning tree concept in which the testing of the nodes as well as the construction of the spanning tree takes place simultaneously. As a result of which the maintenance and the repairing overhead of the spanning tree is completely avoided thus reducing the number of messages exchanged. We have also developed a simulator which can be applied to a network with large number of nodes. We have carried out the simulation in-order to nd out the total number of messages exchanged and the total diagnosis time. On analysing the results we have seen that our model performs better than its previous counterparts. The correctness and complexity proofs are also being provided which also shows that our model performs better from a communication as well as latency viewpoint.

INTRODUCTION

Since the early 1980s wireless cellular systems are quite popular. These cellular systems mainly operate with the help of a centralised supporting system, otherwise known as the access point. It is this access point that helps the users to stay connected in the network. But when it comes to places where there is no xed access point, this technology has own its limitations. In case of rescue and emergency operations installing a centralised supporting system is time consuming. So in-order to overcome this problem we have mobile ad-hoc networks which can be quickly deployed in places where it is not possible otherwise. MANETs are basically a collection of mobile nodes that communicate wirelessly.

MANETs

Mobile Ad-Hoc Networks(MANETs) are basically a collection of mobile nodes that communicate wirelessly without any centralised supporting system. Here the users or nodes are free to roam within the transmission range. Mobile ad-hoc networks (MANET) are gaining much popularity in various rescue and emergency operations because of its self-organizable, autonomous and can-be-deployed-anywhere type of characteristics. Nodes in MANER are equipped with a receiver and a transmitter.

FAULT DIAGNOSIS

As MANETs are mainly used in rescue and emergency operations, having a reliable communication between the mobiles is of utmost importance. Hence the design of dependable MANETs is gaining popularity among the research communities. But the main problem in designing of dependable MANETs is the distributed self-diagnosis problem. Here each fault free mobile has to keep information regarding the state of all the nodes in the neighborhood or in some applications each node should be able to identify the state of all the nodes in the network [1]. Many elegant distributed diagnosis algorithms are available for wired networks and most of them are based either on the invalidation models such as PMC model [2] or comparison model such as the broadcast comparison model [3] and the generalized comparison model [4]. The comparison approach is the most popular diagnosis approach. Here the nodes are given a set of tasks, the tasks are then executed and the outcomes are compared. The comparison outcomes output by the generalized comparison model is summarized below. The comparison outcome is 0, when both the comparator and the compared mobiles are fault-free. If at least one of the compared mobiles is faulty and the comparator is fault-free, the comparison outcome is 1. Finally, the comparison result is unreliable if the comparator mobile is faulty[1]. The earliest works of fault diagnosis in case of MANETs using the comparison approach was proposed by Chessa and Santi in [5]. They have used the shared nature of the communication channel to distribute the diagnosis. In [5], Chessa and Santi have presented with a distributed diagnosis algorithm that allows the fault free mobiles to know the fault status of all the mobiles in the network. The most recent work to solve the diagnosis problem is presented in [1]. In [1] an adaptive distributed self-diagnosis protocol (Adaptive-DSDP) is proposed to solve the diagnosis problem in xed-topology MANETs. In case of xed-topology MANETs it is assumed that the topology of the network is xed during the diagnosis session. This model uses a spanning tree containing all the fault-free nodes which is maintained, repaired and used to transmit the information about other nodes. In this report we have proposed a new diagnosis model based on the spanning tree concept in which the testing of the nodes as well as the construction of the spanning tree takes place simultaneously. Here the test request message helps in the construction of the spanning tree. As a result the overhead of maintaining and repairing of the spanning is completely is avoided, thus improving the time as well as the message complexity.

MOTIVATION

We have analysed the Adaptive-DSDP model and have found that there is an overhead of spanning tree maintenance which occurs all the time even if there is no diagnosis session running. Also the spanning tree is maintained with a particular node as its root i.e. the initiator is xed. So if the initiator node fails or any other node detects an altered behavior the diagnosis session will not start. Further the spanning tree repairing starts after the testing and gathering phase which increases the diagnosis latency as well as the communication complexity. Spanning tree maintenance and repairing consumes a lot of time, so constructing it in the testing and the gathering phase itself will be more efficient.

CONCLUSION

The earliest work on fault identi cation in case of mobile ad-hoc networks was carried out by Chessa and Santi in there work in [5]. There model, known as Static-DSDP, considers a comparison based approach and the network topology is assumed to be xed during the diagnosis session. Such type of network is known as xed topology network. Another work in case of xed topology MANETs was carried out by Elhadef et all. in [6]. The model also known as Dynamic-DSDP uses a spanning tree approach to disseminate the local diagnostic messages collected during the testing phase. Here the spanning tree is constructed after the fault status of the nodes has been identi ed.
Adaptive-DSDP [1] also considers a xed topology environment and a spanning tree approach which is a improvement over the Dynamic-DSDP model. In case of Adaptive-DSDP the spanning tree is initially con gured with the MANET and the protocol enables the maintenance as well as the recon guration of the spanning tree while the hosts are moving or they are diagnosed by their neighbor. In this thesis we have proposed a new model for xed topology environment. A spanning tree approach has also been considered here. In this model the testing of the nodes, gathering of information about neighbors and building of the spanning tree takes place simultaneously. As a result of which the maintenance and the repairing overhead of the spanning tree is completely avoided thus reducing the number of messages exchanged. We have also developed a simulator which can be applied to a network with large number of nodes. We have carried out the simulation in-order to nd out the total number of messages exchanged and the total diagnosis time. On analysing the results we have seen that our model performs better than its previous counterparts. The correctness and complexity proofs are also being provided. From the message and time complexity thus derived we see that our model performs better from a communication as well as latency viewpoint.

FUTURE WORK

The model we have proposed is for a xed topology MANET. Moreover this model identi es only the permanent faults. In future it is possible to extend this model for dynamic topology MANETs and for identifying intermittent as well as dynamic faults. One of the approach for identifying intermittent faults is to repeat our proposed algorithm for a xed number of times.

MATLAB SOURCE CODE

Instructions to run the code

1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Use the files from below link and download them into the same folder as the source codes

fault diagnosis timing

4- Also note that these codes are not in a particular order. Copy them all and then run the program.
5- Run the “FINAL.m” file

Code 1 – Script M File – FINAL.m

clc
clear
close all
%sn=50;
sn=input('Please enter the number of mobiles: '); %total number of mobile nodes
while isempty(sn) || ~isnumeric(sn) %loop for getting a valid number of mobile nodes
    sn=input('Please enter the number of mobiles: ');
end

% placing the sensors in the network
figure(1) 
[nodex,nodey,avg]=placing_sensors(sn); 
%avg
title('Ideal placement of the mobiles')
disp('Press enter to continue')
pause

%faulty nodes
figure(2)
[fnodex,fnodey,fni,l]=faulty_nodes(nodex,nodey,sn);
title('Mobile nodes with faulty points (red-permanent faults, black-transient faults, green-intermittent)')
%title('Flooding Process')
disp('Press enter to continue')
pause

%information transfer
[sender,receiver,s_coor,r_coor,opt]=getsensorinfo(sn,fni,nodex,nodey);
if opt==1 || opt==2 %case when either sender or receiver is out of bound
    figure(3)
    for i=1:sn
        plot(nodex(i),nodey(i),'b*') % plotting all the mobiles
        text(nodex(i)+0.05,nodey(i),num2str(i))
        hold on
    end
    %plot(s_coor(1),s_coor(2),'m*','LineWidth',3)
    text(s_coor(1)+0.5,s_coor(2),'\leftarrow Faulty Node','FontSize',12,'FontWeight','bold')
    
% elseif opt==2
%     figure(3)
%     for i=1:sn
%         plot(nodex(i),nodey(i),'b*') % plotting all the mobiles
%         text(nodex(i)+0.05,nodey(i),num2str(i))
%         hold on
%     end
%     %plot(d_coor(1),d_coor(2),'m*','LineWidth',3)
%     text(d_coor(1)+0.5,d_coor(2),'\leftarrow Faulty Node','FontSize',12,'FontWeight','bold')
    
elseif opt==0 %case when both sender and reciever are inbound
    figure(3)
%    subplot(2,1,1)    
    for i=1:length(nodex) %placement of nodes
        plot(nodex(i),nodey(i),'*')
        text(nodex(i)+0.05,nodey(i),num2str(i)) %numbering
        hold on
    end
    plot(s_coor(1),s_coor(2),'c*')
    text(s_coor(1)+0.05,s_coor(2),'\leftarrow Sender') %marking the sender
    plot(r_coor(1),r_coor(2),'c*')
    text(r_coor(1)+0.05,r_coor(2),'\leftarrow Receiver') %marking the reciever
%    [total1,good1,bad1,diag]=witherror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l); %subplot 1 for the scenario with errors
    [total1]=witherror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l,avg,fni,sender,receiver);
    title('Scenario with error detection')

%    subplot(2,1,2)    
    figure(4)
    for i=1:length(nodex)
        plot(nodex(i),nodey(i),'*') %placement of nodes
        text(nodex(i)+0.05,nodey(i),num2str(i)) %numbering
        hold on
    end
    plot(s_coor(1),s_coor(2),'c*')
    text(s_coor(1)+0.05,s_coor(2),'\leftarrow Sender') %marking the sender
    plot(r_coor(1),r_coor(2),'c*')
    text(r_coor(1)+0.05,r_coor(2),'\leftarrow Receiver') %marking the receiver
    [total2]=withouterror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l,avg,fni,sender,receiver); %subplot 2 for the scenario without errors
    title('Scenario without errors')
% 
%     %graphs
%     %graphs(good1,bad1,total1,total2,sn,nodex,nodey,diag)
% total1
% total2
    graphs(total1,total2,sn,nodex,nodey,fnodex,fnodey,fni)
end

Code 2 – Function M File -graphs.m

function graphs(total1,total2,sn,nodex,nodey,fnodex,fnodey,fni)

[r3 c3]=size(total1);
[r4 c4]=size(total2);

% total1
% total2

[num,txt,raw]=xlsread('fault diagnosis timing');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('fault diagnosis timing',blank_mat,1,'A4')

total1_ind=[];
total2_ind=[];

for i=1:r4 %finding the ids of the scenario with no errors
    for j=1:length(nodex)
        if total2(i,1)==nodex(j) && total2(i,2)==nodey(j)
            total2_ind=[total2_ind j];
        end        
    end
end      

for i=1:r3 %finding the ids of the scenario with errors
    for j=1:length(nodex)
        if total1(i,1)==nodex(j) && total1(i,2)==nodey(j)
            total1_ind=[total1_ind j];
        end        
    end
end      

t=[]; %diagnosable nodes
p=[]; %non diagnosable nodes
g=[]; %good nodes
len=length(fnodex);
for i=1:length(total1_ind)
    op=0; %if its 0, then its a good node. else bad node
    for j=1:length(fni)
        if total1_ind(i)==fni(j)
            if j>=ceil(len/4)+1 && j<=ceil(len/2)
                t=[t total1_ind(i)];
                op=1;                 
                break
            else
                p=[p total1_ind(i)];
                op=1;
                break
            end
        end
    end
    
    if op~=1
        g=[g total1_ind(i)];
    end
end
    
from=[];
to=[];
for i=1:length(total1_ind)    
    op=0;
    if i==1
        %first will always come in "from"
        from=[from total1_ind(i)];
        continue
    elseif i==length(total1_ind)        
        %last will always come in "to"
        to=[to total1_ind(i)];
        break
    end    
          
    for j=1:length(g) %cheking for good nodes
        if total1_ind(i)==g(j)
            to=[to total1_ind(i)];
            from=[from total1_ind(i)];
            op=1;
            break
        end
    end
    if op==1
        continue
    end
       
    for j=1:length(t) %checking for diagnosable nodes
        if total1_ind(i)==t(j)
            to=[to total1_ind(i)];
            from=[from total1_ind(i)];
            op=1;
            break
        end
    end
    if op==1
        continue
    end
    
    for j=1:length(p) %checking for permanent nodes
        if total1_ind(i)==p(j)
            to=[to total1_ind(i)];
            from=[from from(length(from))];
            op=1;
            break
        end
    end
    if op==1;
        continue
    end
end
    
ack_sent=[]; %time to send the acknowledgement
for i=1:length(from)
    dist=sqrt((nodex(from(i))-nodex(to(i)))^2+(nodey(from(i))-nodey(to(i)))^2); % distance between the nodes
    ack_sent=[ack_sent dist];
end

ack_rec=ack_sent; %time to receive the acknowledgement
l=length(fni);
permanent=fni([1:ceil(l/4) (ceil(l/2)+1):l]); %indexes without transient fault
diagnosable=fni(ceil(l/4)+1:ceil(l/2)); %indexes of transient fault
ind=1;
for i=to
    for j=permanent
        if i==j
            ack_rec(ind)=0;
        end
    end
    
    for k=diagnosable
        if i==k
            a=ack_rec(ind);
            b=ack_rec(ind)+4;
            time= a + (b-a).*rand(1,1);
            ack_rec(ind)=ack_rec(ind)+time;
        end
    end
    ind=ind+1;
end

xlswrite('fault diagnosis timing',from',1,'A4')
xlswrite('fault diagnosis timing',to',1,'B4')
xlswrite('fault diagnosis timing',ack_sent',1,'C4')
xlswrite('fault diagnosis timing',ack_rec',1,'D4')
xlswrite('fault diagnosis timing',total2_ind(1:end-1)',1,'F4') %"from" nodes in without error senario
xlswrite('fault diagnosis timing',total2_ind(2:end)',1,'G4') %"to" nodes in without error senario
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
oneunit=(1/sn)*100; % 1 unit of energy
Eg=(r3/sn)*100; % energy lost in good nodes
Eb=(r4/sn)*100; % energy lost in bad nodes

x1=[];
for i=1:length(from)
    x=100-oneunit*i;
    x1=[x1 x]; %nodewise energy loss in scenario with error nodes
end

x2=[];
for i=1:length(total2_ind(1:end-1))
    x=100-oneunit*i;
    x2=[x2 x]; %nodewise energy loss in scenario without error nodes
end

figure(5)
subplot(2,1,1)
bar(x1)
set(gca,'XTickLabel',{from})
ylabel('Energy')
xlabel('Nodes')
title('Scenario with error nodes')

subplot(2,1,2)
bar(x2)
set(gca,'XTickLabel',{total2_ind(1:end-1)})
xlabel('Nodes')
ylabel('Energy')
title('Scenario without error nodes')

%%------------- Data loss Graph --------------
lenth = length(x1);
for i = 1:lenth
b(i) = x1(lenth);
lenth = lenth-1;
end
x1_1 = b;

lenth1 = length(x2);
for i = 1:lenth1
b(i) = x2(lenth1);
lenth1 = lenth1-1;
end
x2_1 = b;

figure(6)
subplot(2,1,1)
grid on
plot(x1_1,'g','linewidth',2)
hold on
plot(x1_1,'ok','linewidth',3)
set(gca,'XTickLabel',{from})
title('DATA LOSS in Scenario with error nodes')

subplot(2,1,2)
grid on
plot(x2_1,'c','linewidth',2)
hold on
plot(x2_1,'or','linewidth',3)
set(gca,'XTickLabel',{total2_ind(1:end-1)})
title('DATA LOSS in Scenario without error nodes')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% delay graph
figure(7)
subplot(2,1,1)
plot(1:r3)
grid on
axis([0 r3+2 0 r3+2])
set(gca,'XTickLabel',{from})
title('Delay in scenario with errors')
xlabel('Serial Number of Nodes in path')
ylabel('Delay (sec)')

subplot(2,1,2)
plot(1:r4)
grid on
axis([0 r3+2 0 r3+2])
set(gca,'XTickLabel',{total2_ind(1:end-1)})
title('Delay in scenario without errors')
xlabel('Serial Number of Nodes in path')
ylabel('Delay (sec)')


Code 3 – Function M File – faulty_nodes.m

function [fnodex,fnodey,fni,l]=faulty_nodes(nodex,nodey,sn)

p=40; %percentage of faulty nodes out of the total number of nodes
fn=floor((p*sn)/100); %number of total faulty nodes (fn-faulty nodes)

fni=randi(sn,1,fn); %indexes of faulty nodes in nodex and nodey (fni-faulty nodes index)

fnodex=[];
fnodey=[];
for i=fni
    fnodex=[fnodex nodex(i)]; % assigning the x coordinates to faulty nodes (assignment is done using the previously assigned x nodes)
    fnodey=[fnodey nodey(i)]; % assigning the y coordiantes to faulty nodes (assignment is done using the previously assigned y nodes)
end

l=length(fni); % total number of faulty nodes 'l'

pfx=[fnodex(1:ceil(l/4))]; %permanent faults
pfy=[fnodey(1:ceil(l/4))];
%ceil(fni/4)

tfx=[fnodex((ceil(l/4)+1):ceil(l/2))]; %transient faults
tfy=[fnodey((ceil(l/4)+1):ceil(l/2))];
% (ceil(fni/4)+1)
% ceil(fni/2)

ifx=[fnodex((ceil(l/2)+1):ceil((3*l)/4))]; %intermittent fault
ify=[fnodey((ceil(l/2)+1):ceil((3*l)/4))];
% (ceil(fni/2)+1)
% ceil((3*fni)/4)

dfx=[fnodex((ceil((3*l)/4)+1):l)]; %dynamic faults
dfy=[fnodey((ceil((3*l)/4)+1):l)];
% (ceil((3*fni)/4)+1)

for i=1:sn
    plot(nodex(i),nodey(i),'b*') % plotting all the mobiles
    text(nodex(i)+0.05,nodey(i),num2str(i))
    hold on
end

%flooding process starts %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[num,txt,raw]=xlsread('static faults');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('static faults',blank_mat,1,'A5')

x=1; %initialize the index of master node
for i=1:sn    
    for j=1:length(fni)
        if i~=fni(j)
            x=i; % master node index which is fault free
            break
        end
    end
    if x==i
        break
    end
end            

pause(0.5)
plot(nodex(x),nodey(x),'b*','LineWidth',5)
text(nodex(x)+0.05,nodey(x),'\leftarrow Master Node','FontSize',15,'FontWeight','bold')
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)])
end
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)],'w')
end
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)])
end
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)],'w')
end
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)])
end
pause(0.5)
for i=1:sn
    plot([nodex(x) nodex(i)],[nodey(x) nodey(i)],'w')
end

time=[];
for i=[1:(x-1) (x+1):sn]
    dist=sqrt((nodex(x)-nodex(i))^2+(nodey(x)-nodey(i))^2);
    time=[time; dist]; % calculating the distances of each node from master node
end

xlswrite('static faults',[1:(x-1) (x+1):sn]',1,'A5')
xlswrite('static faults',time,1,'B5')
xlswrite('static faults',time,1,'C5')

ind=1;
for i=[1:(x-1) (x+1):sn] %excluding the master node here
    for j=[fni(1:(l/2))] %these excludes the dynamic faults, they are not yet discovered. also excludes intermittent faults
        if i==j
            time(ind)=0;
            break
        end
    end
    
    for j=[fni((ceil(l/2)+1):ceil((3*l)/4))]
        if i==j
            a=time(ind);
            b=time(ind)+5; 
            X= a + (b-a).*rand(1,1);
            time(ind)=time(ind)+X;
            break
        end
    end    
    ind=ind+1;
end
xlswrite('static faults',time,1,'D5')
%flooding process ends %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

plot(pfx,pfy,'ro','LineWidth',3) %displaying the permanent faults
plot(tfx,tfy,'ko','LineWidth',3) %displaying the transient faults
plot(ifx,ify,'go','LineWidth',3) %displaying the intermittent faults
%plot(dfx,dfy,'mo','LineWidth',3) %displaying the dynamic faults
%legend('r* Permanent','k* Transient','g* Intermittent','m* Dynamic','Location','NorthEastOutside')

%line 98-102 for the erased nodes during flooding
for i=1:sn
    plot(nodex(i),nodey(i),'b*') % plotting all the mobiles
    text(nodex(i)+0.05,nodey(i),num2str(i))
    hold on
end

end

Code 4 – Function M File – check_nn2.m

function [coor2]=check_nn2(nn,fnodex,fnodey,len,nodex,nodey,coor1)

i=1;
while i>=1
    [r1 c1]=size(nn);
    if i>r1
        x=randi(4);
        switch x
            case 1
                [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey);
                searchx=leftnodesx;
                searchy=leftnodesy;
            case 2
                [upnodesx,upnodesy]=nodes_up(nodex,nodey,coor1);
                searchx=upnodesx;
                searchy=upnodesy;
            case 3
                [downnodesx,downnodesy]=nodes_down(nodex,nodey,coor1);
                searchx=downnodesx;
                searchy=downnodesy;
            case 4
                [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey);
                searchx=rightnodesx;
                searchy=rightnodesy;
            otherwise
                disp('wrong')
        end        
        [nn]=nearest_node(searchx,searchy,coor1);
        i=1;
    end 
% i=1;
% while i>=1    
    for j=1:length(fnodex)
        if nn(i,1)==fnodex(j) && nn(i,2)==fnodey(j)        
%             bad=[bad; fnodex(j) fnodey(j)];
%             total=[total; fnodex(j) fnodey(j)];
            i=i+1;
            coor2=[];
            break
        else
            coor2=[nn(i,1) nn(i,2)];
        end
    end
    
    if ~isempty(coor2)        
        break
    end
end

end

Code 5 – Function M File – nearest_node.m

%function [coor2_1,coor2_2,coor2_3,coor2_4,coor2_5]=nearest_node(nodesx,nodesy,coor1,savenodes)
function [nn]=nearest_node(nodesx,nodesy,coor1)
distance=[];
nn=[];
%[rs cs]=size(savenodes);
% for i=1:length(nodesx)
%     for j=1:rs
%         if nodesx(i)~=savenodes(j,1) && nodesy(i)~=savenodes(j,2)
%             remnodesx=[remnodesx nodesx(i)];
%             remnodesy=[remnodesy nodesy(i)];
%         end
%     end
% end

for i=1:length(nodesx)
    dist=sqrt( ((coor1(1)-nodesx(i))^2) + ((coor1(2)-nodesy(i))^2) );
    distance=[distance dist];
end

[b,ix]=sort(distance); 

for i=ix
    nn=[nn; nodesx(i) nodesy(i)];
end


% [r1 c1]=size(nn);
% [r2 c2]=size(used_nodes);
% for i=1:r1
%     for j=1:r2
%         if nn(i,1)==used_nodes(j,1) && nn(i,2)==used_nodes(j,2)
%             nn(i,:)=[];
%         end
%     end
% end

% if nargout==0
%     coor2_1=coor1;
%     coor2_2=coor1;
%     coor2_3=coor1;
%     coor2_4=coor1;
%     coor2_5=coor1;
% end

end

Code 6 – Function M File – blinking_line.m

function blinking_line(coor1,coor2)
%disp('BLINKING LINE')
for iiii=1:3   
        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
        pause(0.3)
        delete(abc)
        pause(0.3)
        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
        pause(0.3)
        delete(abc)
        pause(0.3)
%        abc=plot([coor1(1) coor2(1)],[coor1(2) coor2(2)]);
end
    
end

Code 7 – Function M File – bestmatch.m

function bestmatch(iii,coor1,coor2,fnodex,fnodey,l)
% coor1
% coor2
% pause
if iii>=1 && iii<=ceil(l/4)
    hold on
    blinking_line(coor1,coor2) %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot([coor1(1) fnodex(iii)],[coor1(2) fnodey(iii)],'r','LineWidth',2) %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot(fnodex(iii),fnodey(iii),'r*','LineWidth',2)
    text(fnodex(iii)+0.05,fnodey(iii),'\leftarrow Permanent Error')
elseif iii>=(ceil(l/2)+1) && iii<=ceil((3*l)/4)
    hold on
    blinking_line(coor1,coor2)  %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot([coor1(1) fnodex(iii)],[coor1(2) fnodey(iii)],'g','LineWidth',2) %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot(fnodex(iii),fnodey(iii),'g*','LineWidth',2)
    text(fnodex(iii)+0.05,fnodey(iii),'\leftarrow Intermittent Error')
elseif iii>=(ceil((3*l)/4)+1) && iii<=l
    hold on
    blinking_line(coor1,coor2)  %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot([coor1(1) fnodex(iii)],[coor1(2) fnodey(iii)],'m','LineWidth',2)  %%%%%%%%%%% NOT WORKING %%%%%%%%%%%
    plot(fnodex(iii),fnodey(iii),'m*','LineWidth',2)
    text(fnodex(iii)+0.05,fnodey(iii),'\leftarrow Dynamic Error')
end

end

Code 8 – Function M File – infotransfer1.m

function nn=infotransfer1(coor1,r_coor,nodex,nodey,total)

hori=coor1(1)-r_coor(1); 
vert=coor1(2)-r_coor(2); 

if abs(hori)>abs(vert)
    
    
elseif abs(hori)<abs(vert)
    
end

Code 9 – Function M File – nodes_down.m

function [downnodesx,downnodesy]=nodes_down(nodesx,nodesy,coor1,r_coor)

downnodesx=[];
downnodesy=[];
%downnodes_index=[];

for i=1:length(nodesx)
    if nodesy(i)<coor1(2) && nodesy(i)>=r_coor(2)
        downnodesx=[downnodesx nodesx(i)];            
        downnodesy=[downnodesy nodesy(i)];
        
%        downnodes_index=[downnodes_index i]; % index of nodex and nodey for upnodesx and upnodesy
    end
end



end

Code 10 – Function M File – nodes_left.m

function [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey,r_coor)

leftnodesx=[];
leftnodesy=[];
%leftnodes_index=[];

for i=1:length(nodex)
    if nodex(i)<coor1(1) && nodex(i)>=r_coor(1)
        leftnodesx=[leftnodesx nodex(i)];            
        leftnodesy=[leftnodesy nodey(i)];
        
 %       leftnodes_index=[leftnodes_index i]; % index of nodex and nodey for leftnodex and leftnodey
    end
end

end

Code 11 – Function M File – nodes_right.m

function [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey,r_coor)

rightnodesx=[];
rightnodesy=[];
%rightnodes_index=[];

for i=1:length(nodex)
    if nodex(i)>coor1(1) && nodex(i)<=r_coor(1)
        rightnodesx=[rightnodesx nodex(i)];            
        rightnodesy=[rightnodesy nodey(i)];
        
%        rightnodes_index=[rightnodes_index i]; % index of nodex and nodey for leftnodex and leftnodey
    end
end

end

Code 12 – Function M File – nodes_up.m

function [upnodesx,upnodesy]=nodes_up(nodesx,nodesy,coor1,r_coor)

upnodesx=[];
upnodesy=[];
%upnodes_index=[];

for i=1:length(nodesx)
    if nodesy(i)>coor1(2) && nodesy(i)<=r_coor(2)
        upnodesx=[upnodesx nodesx(i)];            
        upnodesy=[upnodesy nodesy(i)];
        
 %       upnodes_index=[upnodes_index i]; % index of nodex and nodey for upnodesx and upnodesy
    end
end

end

Code 13 – Function M File – getsensorinfo.m

function [sender,receiver,s_coor,r_coor,opt]=getsensorinfo(sn,fni,nodex,nodey)

opt=0;
sender=input('Enter the sender node ID: '); %sender node ID
while isempty(sender) || sender>sn  %getting a valid value
    sender=input('Enter the sender node ID: ');
end

for i=fni
    if sender==i;
%        error('The sender is a faulty node')                
        opt=1; %when sender is out bound
    end
end

receiver=input('Enter the receiver node ID: '); %reciever node ID
while isempty(receiver) || receiver>sn  %getting a valid value
    receiver=input('Enter the receiver node ID: ');
end

for i=fni
    if receiver==i;
        %error('The receiver is a faulty node')                
        opt=2; %when reciever is out bound
    end
end

s_coor=[nodex(sender) nodey(sender)]; %sender coordinates
r_coor=[nodex(receiver) nodey(receiver)]; %receiver coordinates

end

Code 14 – Function M File – infotransfer.m

function nn=infotransfer(coor1,r_coor,nodex,nodey,total)

hori=coor1(1)-r_coor(1); %horizontal distance between "coor1" and "r_coor"
vert=coor1(2)-r_coor(2); %vertical distance between "coor1" and "r_coor"
% searchnodesx1=[];
% searchnodesy1=[];
if abs(hori)>abs(vert) %if hosirzontal distance is greater then vertical distance
    % search horizontally 
    if coor1(1)>r_coor(1) % search in left direction    
        [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey,r_coor); %set of nearest nodes in left direction till "r_coor"       
        
%         for i=1:length(leftnodesx)
%             if r_coor(1)<leftnodesx(i)
%                 searchnodesx1=[searchnodesx1; leftnodesx(i)];
%                 searchnodesy1=[searchnodesy1; leftnodesy(i)];
%             end
%         end
                
        if coor1(2)<r_coor(2) %search in upwards direction
            [upnodesx,upnodesy]=nodes_up(leftnodesx,leftnodesy,coor1,r_coor);
            searchnodesx=upnodesx;
            searchnodesy=upnodesy;
        elseif coor1(2)>r_coor(2) %search in downwards direction
            [downnodesx,downnodesy]=nodes_down(leftnodesx,leftnodesy,coor1,r_coor);
            searchnodesx=downnodesx;
            searchnodesy=downnodesy;
        end                 
    elseif coor1(1)<r_coor(1) % search in right direction
        [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey,r_coor);
        
%         for i=1:length(rightnodesx)
%             if r_coor(1)>rightnodesx(i)
%                 searchnodesx1=[searchnodesx1; rightnodesx(i)];
%                 searchnodesy1=[searchnodesy1; rightnodesy(i)];
%             end
%         end
        
        if coor1(2)<r_coor(2) %search in upwards direction
            [upnodesx,upnodesy]=nodes_up(rightnodesx,rightnodesy,coor1,r_coor);
            searchnodesx=upnodesx;
            searchnodesy=upnodesy;
        elseif coor1(2)>r_coor(2) %search in downwards direction
            [downnodesx,downnodesy]=nodes_down(rightnodesx,rightnodesy,coor1,r_coor);
            searchnodesx=downnodesx;
            searchnodesy=downnodesy;
        end                    
    end    
elseif abs(hori)<abs(vert)
    % search vertically
    if coor1(2)>r_coor(2) % search in down direction
        [downnodesx,downnodesy]=nodes_down(nodex,nodey,coor1,r_coor);        
        
%         for i=1:length(downnodesy)
%            if r_coor(2)<downnodesy(i)
%                 searchnodesx1=[searchnodesx1; downnodesx(i)];
%                 searchnodesy1=[searchnodesy1; downnodesy(i)];
%             end
%         end
        
        if coor1(1)<r_coor(1) %search in right direction
            [rightnodesx,rightnodesy]=nodes_right(coor1,downnodesx,downnodesy,r_coor);            
            searchnodesx=rightnodesx;
            searchnodesy=rightnodesy;
        elseif coor1(1)>r_coor(1) %search in left direction
            [leftnodesx,leftnodesy]=nodes_left(coor1,downnodesx,downnodesy,r_coor);            
            searchnodesx=leftnodesx;
            searchnodesy=leftnodesy;
        end        
    elseif coor1(2)<r_coor(2) % search in up direction
        [upnodesx,upnodesy]=nodes_up(nodex,nodey,coor1,r_coor);        
        
%         for i=1:length(upnodesy)
%            if r_coor(2)>upnodesy(i)
%                 searchnodesx1=[searchnodesx1; upnodesx(i)];
%                 searchnodesy1=[searchnodesy1; upnodesy(i)];
%             end
%         end

        if coor1(1)<r_coor(1) %search in right direction
            [rightnodesx,rightnodesy]=nodes_right(coor1,upnodesx,upnodesy,r_coor);            
            searchnodesx=rightnodesx;
            searchnodesy=rightnodesy;
        elseif coor1(1)>r_coor(1) %search in left direction
            [leftnodesx,leftnodesy]=nodes_left(coor1,upnodesx,upnodesy,r_coor);            
            searchnodesx=leftnodesx;
            searchnodesy=leftnodesy;
        end        
    end    
end

[nn]=nearest_node(searchnodesx,searchnodesy,coor1); %set of nearest nodes from "coor1" in the direction of "r_coor"
%nn2=nn;
 nn2=[];
 %pause
if ~isempty(total) %loop to delete already occured nodes 
    [r1 c1]=size(nn);
    [r2 c2]=size(total);
    for i=1:r1
        for j=1:r2            
            if nn(i,1)==total(j,1) && nn(i,2)==total(j,2)
                nn2=[nn2 i];
%                 i
%                 disp('caught')
%                 nn2(i,:)
%                 nn2(i,:)=[]; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            end
        end
    end    
end
nn(nn2,:)=[]; %deleting the rows from "nn" which have already occured

% [r c]=size(nn);
% for i=1:r
%     plot(nn(i,1),nn(i,2),'y*')
% end
% pause(1)
% for i=1:r
%     plot(nn(i,1),nn(i,2),'b*')
% end
    
%nn=nn2;
end

Code 15 – Function M File – placing_sensors.m

function [nodex,nodey,avg]=placing_sensors(sn)

low=0; %lower bound to both the axis
high=10; %upper bound to both the axis
 
nodex=[];
nodey=[];
for i=1:sn
    nodex=[nodex (low+(high-low)*rand)];  %x coordinates of all the points
    nodey=[nodey (low+(high-low)*rand)];  %y coordinates of all the points
end
 
for i=1:sn 
    plot(nodex(i),nodey(i),'b*') %ploting the x and y coordinates
    text(nodex(i)+0.05,nodey(i),num2str(i)) %giving all the nodes there respective numbers
    hold on
end

av=0;
ind=0;
for i=1:length(nodex)
    for j=1:length(nodex)
        dist=sqrt((nodex(i)-nodex(j))^2+(nodey(i)-nodey(j))^2);
        av=av+dist;
        ind=ind+1;
    end
end

avg=av/ind;
end

Code 16 – Function M File – witherror.m

%function [total,good,bad,diag]=witherror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l)
function [total]=witherror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l,avg,fni,sender,receiver)

coor1=s_coor; %initialising "coor1" with the value of "sender coordinate" i.e. "s_coor"
%blinking will always occur from "coor1" to "coor2"
total=[];
total=[total; coor1];
% good=[];
% bad=[];
% diag=[];
while coor1(1)~=r_coor(1) && coor1(2)~=r_coor(2)   % loop will run untill "coor1" is not equal to "receiver coordinates" i.e. "r_coor"
    nn=infotransfer(coor1,r_coor,nodex,nodey,total); %finding out the valid group of nodes to mark "coor2"
    %pause    
    [coor2,total,x]=check_nn(nn,fnodex,fnodey,l,nodex,nodey,coor1,total,r_coor); %finding suitable node "coor2" from "nn". also stores its coordinates
    if x==0 %the node is not diagnosable
        [coor2]=check_avg(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver); 
    end    
    blinking_line(coor1,coor2)
    plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'k','LineWidth',2)
    if coor2(1)==r_coor(1) && coor2(2)==r_coor(2)
        total=[total; coor2];
%        good=[good; coor2];        
        break
    else
        coor1=coor2;                
        total=[total; coor2];
%        good=[good; coor1];
    end
end

end

Code 17 – Function M File – withouterror.m

function [total]=withouterror(s_coor,r_coor,nodex,nodey,fnodex,fnodey,l,avg,fni,sender,receiver)

coor1=s_coor;
total=[];
while coor1(1)~=r_coor(1) && coor1(2)~=r_coor(2)    
    nn=infotransfer(coor1,r_coor,nodex,nodey,total);
%    [coor2]=check_nn2(nn,fnodex,fnodey,l,nodex,nodey,coor1);
    coor2=[nn(1,1) nn(1,2)];
    [coor2]=check_avg2(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver);
    blinking_line(coor1,coor2)
    plot([coor1(1) coor2(1)],[coor1(2) coor2(2)],'k','LineWidth',2)
    total=[total; coor1];    
    if coor2(1)==r_coor(1) && coor2(2)==r_coor(2)
        total=[total; r_coor];    
        break
    else
        coor1=coor2;                
    end
end

end

Code 18 – Function M File – check_nn.m

%function [coor2,bad,total,diag]=check_nn(nn,fnodex,fnodey,len,nodex,nodey,coor1,bad,total,r_coor,diag)
function [coor2,total,x]=check_nn(nn,fnodex,fnodey,len,nodex,nodey,coor1,total,r_coor,fni,avg,sender,receiver)
%[coor2]=check_avg2(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver)
i=1;
x=0;
while i>=1
    [r1 c1]=size(nn);
    if i>r1 % loop will run when the given "nn" does not have any value. we have to find new set of "nn"
 %       disp('yes')
        [leftnodesx,leftnodesy]=nodes_left(coor1,nodex,nodey,r_coor); %nodes to the left of "coor1" till "r_coor"
        [upnodesx,upnodesy]=nodes_up(nodex,nodey,coor1,r_coor); %nodes to the up of "coor1" till "r_coor"
        [downnodesx,downnodesy]=nodes_down(nodex,nodey,coor1,r_coor); %nodes to the down of "coor1" till "r_coor"
        [rightnodesx,rightnodesy]=nodes_right(coor1,nodex,nodey,r_coor); %nodes to the right of "coor1" till "r_coor"
       
        [nn1]=nearest_node(upnodesx,upnodesy,coor1); %arranging the obtained sets in ascending order. it contains the nearest nodes 
        [nn2]=nearest_node(downnodesx,downnodesy,coor1);
        [nn3]=nearest_node(leftnodesx,leftnodesy,coor1);
        [nn4]=nearest_node(rightnodesx,rightnodesy,coor1);
        
        if ~isempty(nn1) %if "nn1" is not empty, it will find the nearest node 
            for ii=1:length(fnodex)
                if nn1(1,1)==fnodex(ii) && nn1(1,2)==fnodey(ii) %cheking if the nearest nodes in up direction is faulty
                    a=[];
                    break
                else
                    a=[nn1(1,1) nn1(1,2)]; %nearest nodes in up direction
                end
            end
        else % if "nn1" is empty
             a=[];
        end 
       
         if ~isempty(nn2)%if "nn2" is not empty, it will find the nearest node 
            for ii=1:length(fnodex)
                if nn2(1,1)==fnodex(ii) && nn2(1,2)==fnodey(ii) %cheking if the nearest nodes in down direction is faulty
                    b=[];
                    break
                else
                    b=[nn2(1,1) nn2(1,2)]; %nearest node in down direction
                end
            end
         else% if "nn2" is empty
             b=[];
         end
        
         if ~isempty(nn3)%if "nn3" is not empty, it will find the nearest node 
            for ii=1:length(fnodex)    
                if nn3(1,1)==fnodex(ii) && nn3(1,2)==fnodey(ii) %cheking if the nearest nodes in left direction is faulty
                    c=[];
                    break
                else
                    c=[nn3(1,1) nn3(1,2)]; %nearest node in left direction
                end
            end
         else% if "nn3" is empty
             c=[];
         end
        
         if ~isempty(nn4)%if "nn4" is not empty, it will find the nearest node 
            for ii=1:length(fnodex)    
                if nn4(1,1)==fnodex(ii) && nn4(1,2)==fnodey(ii) %cheking if the nearest nodes in right direction is faulty
                    d=[];
                    break
                else
                    d=[nn4(1,1) nn4(1,2)]; %nearest node in right direction
                end
            end
         else% if "nn4" is empty
             d=[];
         end
            
        if ~isempty(a)
            ad=sqrt((coor1(1)-a(1))^2+(coor1(2)-a(2))^2); %distance to the nearest up node
        else
            ad=50; %high value given so that it will not be chosen at the time of selection
        end
        
        if ~isempty(b)
            bd=sqrt((coor1(1)-b(1))^2+(coor1(2)-b(2))^2); %distance to the nearest down node
        else
            bd=50;%high value given so that it will not be chosen at the time of selection
        end
        
        if ~isempty(c)
            cd=sqrt((coor1(1)-c(1))^2+(coor1(2)-c(2))^2); %distance to the nearest left node
        else
            cd=50;%high value given so that it will not be chosen at the time of selection
        end
        
        if ~isempty(d)
            dd=sqrt((coor1(1)-d(1))^2+(coor1(2)-d(2))^2); %distance to the nearest right node
        else
            dd=50;%high value given so that it will not be chosen at the time of selection
        end
        
        mat=[ad bd cd dd];
        [mini,ind]=min(mat);  %finding the closest of all the 4 direction nodes
        switch ind
            case 1
                nn=nn1; %up is closest
            case 2
                nn=nn2; %down is closest
            case 3
                nn=nn3; %left is closest
            case 4
                nn=nn4; %right is closest
            otherwise
                disp('wrong')
        end
        
        i=1;
    end 
    
    for j=1:length(fnodex) %loop will rum for the number of faulty nodes times
        if nn(i,1)==fnodex(j) && nn(i,2)==fnodey(j) %checking the nearest node for faultyness                        
            coor_1=coor1;
            coor_2=[fnodex(j) fnodey(j)];
            %[coor2]=check_avg2(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver);
            %pause
            blinking_line(coor_1,coor_2)  %this blinking is till the faulty node
%            disp('---------')            
            x=0;
            if j>=(ceil(len/4)+1) && j<=ceil(len/2)
                plot(fnodex(j),fnodey(j),'ko','LineWidth',2)
                text(fnodex(j)+0.05,fnodey(j),'\leftarrow Transient Error Diagnosed')
                coor2=[fnodex(j) fnodey(j)];
                %diag=[diag; coor2];
%                total=[total; coor2];
                x=1; %this means that the faulty node is diagnosable
                break
            else
                bestmatch(j,coor_1,coor_2,fnodex,fnodey,len) 
                %bad=[bad; fnodex(j) fnodey(j)]; %storing the coordinates
                total=[total; fnodex(j) fnodey(j)];
            end     
            
            if x==1 % if the node can be diagnosed, break. no need to search further
                break
            else % if the node cnt be diagnosed, mark it with its color
                i=i+1; % and move on to the next nearest node in "nn"
                coor2=[]; %no value of "coor2" obtained
                break
            end
        else
            coor2=nn(i,:); % "coor2" is the i^th nearest node from "nn" as it is not a faulty node as checked at line 113
        end
    end
    
    if ~isempty(coor2) %breaks the loop if we have a value of "coor2"
        break
    end
end

end

Code 19 – Function M File – check_avg.m

function [coor2]=check_avg(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver)

dist=sqrt((coor1(1)-coor2(1))^2+(coor1(2)-coor2(2))^2);
range=ceil(avg/2);

if dist<range;
    coor2=coor2;
    
elseif dist>range;
    range_ind=[]; %nodes in the range of coor1
    for i=[1:sender-1  sender+1:length(nodex)]
        dist=sqrt((coor1(1)-nodex(i))^2+(coor1(2)-nodey(i))^2);
        if dist<=range
            range_ind=[range_ind i]; %finding all the nodes in the range of coor1
        end
    end    
    %range_ind
    
    for i=1:length(fni)
        x=find(range_ind==fni(i));
        range_ind(x)=[]; %deleting the faulty indexes
    end
    %range_ind
    %pause

    %the closest node to r_coor in the range of coor1 will connected
    distances=[];
    for i=range_ind
        dist=sqrt((r_coor(1)-nodex(i))^2+(r_coor(2)-nodey(i))^2);
        distances=[distances; i dist];
    end    
    x=find(distances(:,2)==min(distances(:,2)));
    coor2=[nodex(distances(x,1)) nodey(distances(x,1))];    
end    

Code 20 – Function M File – check_avg2.m

function [coor2]=check_avg2(coor2,coor1,avg,nodex,nodey,r_coor,fni,sender,receiver)

dist=sqrt((coor1(1)-coor2(1))^2+(coor1(2)-coor2(2))^2);
range=ceil(avg/2);

if dist<range;
    coor2=coor2;
    
elseif dist>range;
    range_ind=[]; %nodes in the range of coor1
    for i=[1:sender-1  sender+1:length(nodex)]
        dist=sqrt((coor1(1)-nodex(i))^2+(coor1(2)-nodey(i))^2);
        if dist<=range
            range_ind=[range_ind i]; %finding all the nodes in the range of coor1
        end
    end    
    %range_ind
    
    %the closest node to r_coor in the range of coor1 will connected
    distances=[];
    for i=range_ind
        dist=sqrt((r_coor(1)-nodex(i))^2+(r_coor(2)-nodey(i))^2);
        distances=[distances; i dist];
    end    
    x=find(distances(:,2)==min(distances(:,2)));
    coor2=[nodex(distances(x,1)) nodey(distances(x,1))];    
end    

 

Forest fire detection system

PROJECT VIDEO

SCENARIO

1- Deploy nodes in the forest.

2- All the nodes in the equal range.

3- All the nodes that are in the one range having a one cluster head they work as a cluster coordinator.

4- Data aggregation happening on that point.

5- In the scenario first we send packet from source to destinatiosink,n when environment having no problem.

6- In the second when the fire happen that time those will detect the fire this node working as a source sent information to destination (base station).

7- Third case when fire destroy the cluster head then election algorithm elect the new cluster head and communicate with other nodes sent information to destination.

MATLAB SOURCE CODE

Instructions to run the code

1- Copy each of below codes in different M files.
2- Place all the files in same folder

3- Use the Excel files from below link and download them into the same folder as the source codes

Timeframe

4- Also note that these codes are not in a particular order. Copy them all and then run the program.
5- Run the “FINAL.m” file

Code 1 – Script M File – FINAL.m

clc
clear all
close all

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%%%%%%%%%%%  WELCOME TO JUNGLE FIRE DETECTION PROGRAMME  %%%%%%%%%%%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')

hn=input('Please enter the approx number of HEAD NODES (minimum 5): '); % number of head nodes
while hn<5 % minimum number of head nodes must be 5
    disp('Too few sensors, please enter again')
    hn=input('Please enter the approx number of HEAD NODES (minimum 5): ');
end
%hn=30;
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Placing the sensors')
disp('Press enter to continue..')
pause

figure(1)
[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
title('Placement of the sensors with networking between the head nodes and base station')
hold off

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Condition of no fire')
disp('Press enter to continue..')
pause

for i=1:2
    figure(2)
    normal_condition(hn)
    title('Flow of information when there is no fire')
    hold off
    pause(0.3)
    if i==3
        break
    else
        set(gca,'Color','w')
    end
end

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Condition of fire')
disp('Press enter to continue..')
pause

figure(3)
start_fire=fire_condition(hn);
title('Condition of fire increasing through the jungle')
hold off

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Condition of  fire detection')
disp('Press enter to continue..')
pause

figure(4)
%firedetection2(hn,x,y,x1,y1,row,col)
[axis,time,burntnodes,basetime,times,unik]=fire_detection3(hn,bp,x,y,x1,y1,row,col);
title('Detecting the fire(scenario 1)')

% figure(5)
% [axis,time,burntnodes,basetime]=fire_detection(hn,bp,x,y,x1,y1,row,col);
% %test_scene(hn)
% title('Detecting the fire(scenario 1)')

disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('Graphs')
disp('Press enter to continue..')
pause
% 
graphs(axis,time,burntnodes,x1,y1,basetime,x,y,times,unik,start_fire)

Code 2 – Function M File – fire_condition.m

function [start_fire]=fire_condition(hn)

[x,y,x1,y1,row,col,bp]=placing_sensors(hn);

[r c]=size(x);
dh=sqrt((x(1,1)-x(1,c))^2 + (y(1,1)-y(1,c))^2);
dv=sqrt((x(1,1)-x(r,1))^2 + (y(1,1)-y(r,1))^2);

start_fire=[((dh/2)+x(1,1)) ((dv/2)+4)];
plot(start_fire(1),start_fire(2),'ko')

div=row/col;

if (start_fire(2)-y(1,1))<(start_fire(1)-x(1,1))
    ii=1;
    ij=1;
    while ii<(start_fire(2)-y(1,1)) || ij<(start_fire(2)-y(1,1))    
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
    end
elseif (start_fire(2)-y(1,1))>(start_fire(1)-x(1,1))
    ii=1;
    ij=1;
    while ii<(start_fire(1)-x(1,1)) || ij<(start_fire(1)-x(1,1))    
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
    end
end

end

Code 3 – Function M File – placing_sensors.m

function [x,y,x1,y1,row,col,bp]=placing_sensors(hn)

col=5*ceil(hn/30); %0-30hn=5col, 30-60hn=10col...
row=ceil(hn/col); 

rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
text(1,((row*4)+8),'BASE')
hold on
[x y]=meshgrid(linspace(1,(col*4),col),linspace(4,(row*4),row));
plot(x,y,'go','LineWidth',5)
axis([-5 ((col*4)+5) 0 ((row*4)+10)])

bp=[5 ((4*row)+6)]; % base station reciever point
a=x(1,:);
b=y(end,:);
for i=1:length(a)
    plot([a(i) bp(1)],[b(i) bp(2)],'LineWidth',2);
end

% difference=(row*col)-hn;
% if difference~=0
%     for i=1:difference
%         plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
%     end
% end
% clear difference

%if mod(hn,col)==0 % network when number of head nodes is a multiple of col
    [r c]=size(x);
    a1=x(1,:);
    b1=x(r,:);
    c1=y(1,:);
    d1=y(r,:);
    
    a2=x(:,1);
    b2=x(:,c);
    c2=y(:,1);
    d2=y(:,c);
    
    for i=1:length(a1)
        m1=[a1(i) b1(i)];
        n1=[c1(i) d1(i)];
        plot(m1,n1,'LineWidth',2)
    end
    
    for i=1:length(a2)
        m2=[a2(i) b2(i)];
        n2=[c2(i) d2(i)];
        plot(m2,n2,'LineWidth',2)
    end
% else % network when number of head nodes is not a multiple of col
%     [r c]=size(x);
%     a1=x(r,:);
%     b1=x(2,:);
%     c1=y(r,:);
%     d1=y(2,:);
%     
%     a2=x(r:-1:2,1);
%     b2=x(r:-1:2,c);
%     c2=y(r:-1:2,1);
%     d2=y(r:-1:2,c);
%     
%     for i=1:length(a1)
%         m1=[a1(i) b1(i)];
%         n1=[c1(i) d1(i)];
%         plot(m1,n1,'LineWidth',2)
%     end
%     
%     for i=1:length(a2)
%         m2=[a2(i) b2(i)];
%         n2=[c2(i) d2(i)];
%         plot(m2,n2,'LineWidth',2)
%     end
%     
%     difference=hn-(col*(row-1));    
%     plot([x(1,1) x(1,difference)],[y(1,1) y(1,difference)],'LineWidth',2)
%     for i=1:difference
%         plot([x(1,i) x(2,i)],[y(1,i) y(2,i)],'LineWidth',2);
%     end    
% end
clear difference r c
dh=sqrt((x(1,1)-x(1,2))^2+((y(1,1)-y(1,2))^2)); % horizontal distance between two head nodes
dv=sqrt((x(1,1)-x(2,1))^2+((y(1,1)-y(2,1))^2)); % vertical distance between two head nodes

[x1 y1]=meshgrid(((x(1,1)-(dh/4)):dh/2:(x(1,end)+(dh/4))),((y(1,1)-(dv/4)):dv/2:(y(end,1)+(dv/2))));
plot(x1,y1,'g*')
[r c]=size(x1);
difference=(r*c)-(hn*4);

% if ~isempty(difference)
%     d=difference/2;
%     for i=0:d-1
%         plot(x1(1,c-i),y1(1,c-i),'w*')
%         plot(x1(2,c-i),y1(2,c-i),'w*')        
%     end
% end

for i=1:row
    column=col;    
%     if i==1 && mod(hn,col)~=0           
%         column=hn-((row-1)*col);        
%     end
    
    for j=1:column
        plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])        
        plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
        plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
        plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])        
    end
end

end

Code 4 – Function M File – fire_detection3.m

function [axis,time,burntnodes,basetime,times,unik]=fire_detection3(hn,bp,x,y,x1,y1,row,col)

%[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%figure(4)
[r c]=size(x);
[r1 c1]=size(x1);
dh=sqrt((x(1,1)-x(1,c))^2 + (y(1,1)-y(1,c))^2); %horizontal distance
dv=sqrt((x(1,1)-x(r,1))^2 + (y(1,1)-y(r,1))^2); %vertical distance
start_fire=[((dh/2)+x(1,1)) ((dv/2)+4)]; %origin of fire
%plot(start_fire(1),start_fire(2),'ko')
div=row/col;
time=0; %time in sec for the fire to reach at the edge of forest
axis=[]; %values of axis of ellipse from the start point to the boundry of forest
burntnodes=[]; % number of burnt nodes
basetime=[]; 
times=[]; %time for each ellipse 
unik=[]; %number of nodes burnt
if (start_fire(2)-y(1,1))<(start_fire(1)-x(1,1)) %vertical distance < horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
    n=0;    
    X1=x;
    Y1=y;    
    while ii<=(start_fire(2)-y(1,1)) || ij<=(start_fire(2)-y(1,1)) % detect vertical radius first, and then horizontal               
%        subplot(1,2,1)        
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div); % to detect and mark the dead nodes       
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
%        flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
        [X,Y]=bully3(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
        %flowinfo1(row,col,hn,x,y,deadnodeset_a,ra)       
        %checkdeadnode1(deadnodeset,x,y,x1,y1,bp)
          
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes       
%         %deadnodeset_a=deadnodeset;        
%         %checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
        
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
        time=time+1;
        axis=[axis; ii ij];
        [ra ca]=size(deadnodeset);
        burntnodes=[burntnodes ra];
        [raa caa]=size(unik_deadnodes);
        if ~isequal(raa,0)
            unik=[unik raa];
            times=[times; round(clock)];
        end
        n=n+ii;
        basetime=[basetime n];   
        
    end    
elseif (start_fire(2)-y(1,1))>(start_fire(1)-x(1,1)) %vertical distance > horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
    n=0;
    X1=x;
    Y1=y;
    
    while ii<=(start_fire(1)-x(1,1)) || ij<=(start_fire(1)-x(1,1))     % detect horizontal radius first, and then vertical
%        subplot(1,2,1)
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div);  % to detect and mark the dead nodes
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
        %flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
        [X,Y]=bully3(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;
%         %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes                
%         %deadnodeset_a=deadnodeset;        
%         % checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
       
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
        time=time+1;
        axis=[axis; ii ij];
        [ra ca]=size(deadnodeset);
        burntnodes=[burntnodes ra];
        [raa caa]=size(unik_deadnodes);
        if ~isequal(raa,0)
            unik=[unik raa];
            times=[times; round(clock) ];
        end
        n=n+ii;
        basetime=[basetime n];        
    end    
end
end

Code 5 – Function M File –bully3.m

function [X,Y]=bully3(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1)
bars=0;
%t=0;
while bars>=0
    subplot(1,2,1)
    bar(bars)
    axis([0 3 0 110])
    grid on
    ylabel('Temperature')
    set(gca,'XTick',[])

    bars=bars+randi(10,1,1);
    %bars=bars+15;
    
    subplot(1,2,2)
    rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
    text(1,((row*4)+8),'BASE')
    hold on
    cp=[5 ((row*4)+6)];
%    coloring_b(X1,Y1,row,col,x,y,x1,y1)

    plot(x,y,'go','LineWidth',5)
    plot(x1,y1,'g*')
    set(gca,'XTick',[])
    set(gca,'YTick',[])

    [r c]=size(deadnodeset);
    for i=1:r
        plot(deadnodeset(i,1),deadnodeset(i,2),'ko','LineWidth',3)        
    end    
    [ra ca]=size(unik_deadnodes);
    for i=1:ra
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ro','LineWidth',3)    
        plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'r')
    end
    pause(0.5)
    for i=1:ra    
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'yo','LineWidth',3)
        plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'y')
    end
    pause(0.5)    
    
    if bars>=90   
        %bars=1;
        break
    end
end
    
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ko','LineWidth',3)
    plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'w')
end

[r c]=size(X1);
for i=1:r
    for j=1:c
        if X1(i,j)==x(i,j) && Y1(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')            
        elseif X1(i,j)~=0 && Y1(i,j)~=0
            plot([X1(i,j) x1(((2*i)-1),((2*j)-1))],[Y1(i,j) y1(((2*i)-1),((2*j)-1))],'w')
            plot([X1(i,j) x1(((2*i)-1),(2*j))],[Y1(i,j) y1(((2*i)-1),(2*j))],'w')
            plot([X1(i,j) x1((2*i),((2*j)-1))],[Y1(i,j) y1((2*i),((2*j)-1))],'w')
            plot([X1(i,j) x1((2*i),(2*j))],[Y1(i,j) y1((2*i),(2*j))],'w')
        end
    end
end


[rr cc]=size(x);
[r3 c3]=size(deadnodeset);

X=x;
Y=y;

for i=1:rr
    for j=1:cc     
        for k=1:r3
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)               
                a1=x1(((2*i)-1),((2*j)-1));
                a2=y1(((2*i)-1),((2*j)-1));
                b1=x1(((2*i)-1),(2*j));
                b2=y1(((2*i)-1),(2*j));
                c1=x1((2*i),((2*j)-1));
                c2=y1((2*i),((2*j)-1));
                d1=x1((2*i),(2*j));
                d2=y1((2*i),(2*j));
                
                a=[a1 a2];
                b=[b1 b2];
                c=[c1 c2];
                d=[d1 d2];

                for l=1:r3 
                    if a(1)==deadnodeset(l,1) && a(2)==deadnodeset(l,2)
                        a=[];
                        break
                    else
                        X(i,j)=a(1); %a becomes the new head
                        Y(i,j)=a(2);
                    end
                end
                    
                if isempty(a)
                   for l=1:r3                       
                       if b(1)==deadnodeset(l,1) && b(2)==deadnodeset(l,2)
                           b=[];
                           break
                       else
                            X(i,j)=b(1); %b becomes the new head
                            Y(i,j)=b(2);
                       end
                   end 
                end
                
                if isempty(b)
                   for l=1:r3                       
                       if c(1)==deadnodeset(l,1) && c(2)==deadnodeset(l,2)
                           c=[];
                          break
                       else
                            X(i,j)=c(1); %c becomes the new head
                            Y(i,j)=c(2);                            
                       end
                   end 
                end
               
                if isempty(c)
                   for l=1:r3                       
                       if d(1)==deadnodeset(l,1) && d(2)==deadnodeset(l,2)
                           d=[];
                           break
                       else
                            X(i,j)=d(1); %d becomes the new head
                            Y(i,j)=d(2);
                       end
                   end 
                end
                
                if isempty(d)
                    X(i,j)=0; %the whole cluster is burnt
                    Y(i,j)=0;
                end
            end
        end
    end
end
% new head nodes obtained (up)
for i=1:rr
    for j=1:cc
        xyz=0;
        if x(i,j)==X(i,j) && y(i,j)==Y(i,j)
            xyz=1;
        else
            xyz=0;
            break
        end        
    end
    if xyz==0
        break
    end
end

if xyz==1
    for i=1:row    
        for j=1:col
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])        
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])        
        end
    end
end  

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    %delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')

                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end        
            
            clear k abc
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
        
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end                    
        end
    end
end

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))])
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))])
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))])
        end
    end
end

%clear rr cc
%coloring_w(X1,Y1,row,col,x,y,x1,y1)
%coloring_c(X,Y,row,col,x,y,x1,y1)



%pause    
end

Code 6 – Function M File –bully2.m

function [X,Y]=bully2(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1)
    
[rr cc]=size(x);
[r3 c3]=size(deadnodeset);

X=x;
Y=y;

for i=1:rr
    for j=1:cc     
        for k=1:r3
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)               
                a1=x1(((2*i)-1),((2*j)-1));
                a2=y1(((2*i)-1),((2*j)-1));
                b1=x1(((2*i)-1),(2*j));
                b2=y1(((2*i)-1),(2*j));
                c1=x1((2*i),((2*j)-1));
                c2=y1((2*i),((2*j)-1));
                d1=x1((2*i),(2*j));
                d2=y1((2*i),(2*j));
                
                a=[a1 a2];
                b=[b1 b2];
                c=[c1 c2];
                d=[d1 d2];

                for l=1:r3 
                    if a(1)==deadnodeset(l,1) && a(2)==deadnodeset(l,2)
                        a=[];
                        break
                    else
                        X(i,j)=a(1); %a becomes the new head
                        Y(i,j)=a(2);
                    end
                end
                    
                if isempty(a)
                   for l=1:r3                       
                       if b(1)==deadnodeset(l,1) && b(2)==deadnodeset(l,2)
                           b=[];
                           break
                       else
                            X(i,j)=b(1); %b becomes the new head
                            Y(i,j)=b(2);
                       end
                   end 
                end
                
                if isempty(b)
                   for l=1:r3                       
                       if c(1)==deadnodeset(l,1) && c(2)==deadnodeset(l,2)
                           c=[];
                          break
                       else
                            X(i,j)=c(1); %c becomes the new head
                            Y(i,j)=c(2);                            
                       end
                   end 
                end
               
                if isempty(c)
                   for l=1:r3                       
                       if d(1)==deadnodeset(l,1) && d(2)==deadnodeset(l,2)
                           d=[];
                           break
                       else
                            X(i,j)=d(1); %d becomes the new head
                            Y(i,j)=d(2);
                       end
                   end 
                end
                
                if isempty(d)
                    X(i,j)=0; %the whole cluster is burnt
                    Y(i,j)=0;
                end
            end
        end
    end
end
% new head nodes obtained (up)
for i=1:rr
    for j=1:cc
        xyz=0;
        if x(i,j)==X(i,j) && y(i,j)==Y(i,j)
            xyz=1;
        else
            xyz=0;
            break
        end        
    end
    if xyz==0
        break
    end
end

if xyz==1
    for i=1:row    
        for j=1:col
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])        
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])        
        end
    end
end  

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    %delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')

                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end        
            
            clear k abc
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
        
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end                    
        end
    end
end

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))])
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))])
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))])
        end
    end
end
%pause    
end

Code 7 – Function M File –  firedetection2.m

%function [axis,time,burntnodes,basetime]=firedetection2(hn,bp,x,y,x1,y1,row,col)
function firedetection2(hn,x,y,x1,y1,row,col)
%[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%figure(4)
[r c]=size(x);
[r1 c1]=size(x1);
dh=sqrt((x(1,1)-x(1,c))^2 + (y(1,1)-y(1,c))^2); %horizontal distance
dv=sqrt((x(1,1)-x(r,1))^2 + (y(1,1)-y(r,1))^2); %vertical distance
start_fire=[((dh/2)+x(1,1)) ((dv/2)+4)]; %origin of fire
%plot(start_fire(1),start_fire(2),'ko')
div=row/col;
% time=0; %time in sec for the fire to reach at the edge of forest
% axis=[]; %values of axis of ellipse from the start point to the boundry of forest
% burntnodes=[]; % number of burnt nodes
% basetime=[]; 
if (start_fire(2)-y(1,1))<(start_fire(1)-x(1,1)) %vertical distance < horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
%    n=0;    
    X1=x;
    Y1=y;
    while ii<=(start_fire(2)-y(1,1)) || ij<=(start_fire(2)-y(1,1)) % detect vertical radius first, and then horizontal               
%        subplot(1,2,1)        
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div); % to detect and mark the dead nodes              
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
        [X,Y]=scene2(deadnodeset,unik_deadnodes,x,y,x1,y1,row,col,X1,Y1,hn);
%        flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
%         [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
        %flowinfo1(row,col,hn,x,y,deadnodeset_a,ra)       
        %checkdeadnode1(deadnodeset,x,y,x1,y1,bp)
          
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes       
%         %deadnodeset_a=deadnodeset;        
%         %checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
        
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
%         time=time+1;
%         axis=[axis; ii ij];
%         [ra ca]=size(deadnodeset);
%         burntnodes=[burntnodes ra];
%         n=n+ii;
%         basetime=[basetime n];        
    end    
elseif (start_fire(2)-y(1,1))>(start_fire(1)-x(1,1)) %vertical distance > horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
%     n=0;
    X1=x;
    Y1=y;
    while ii<=(start_fire(1)-x(1,1)) || ij<=(start_fire(1)-x(1,1))     % detect horizontal radius first, and then vertical
%        subplot(1,2,1)
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div);  % to detect and mark the dead nodes
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
        [X,Y]=scene2(deadnodeset,unik_deadnodes,x,y,x1,y1,row,col,X1,Y1,hn);
%         %flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
%         [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;
%         %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes                
%         %deadnodeset_a=deadnodeset;        
%         % checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
       
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
%         time=time+1;
%         axis=[axis; ii ij];
%         [ra ca]=size(deadnodeset);
%         burntnodes=[burntnodes ra];
%         n=n+ii;
%         basetime=[basetime n];
    end    
end
end

Code 8 – Function M File –scene2.m

function [X,Y]=scene2(deadnodeset,unik_deadnodes,x,y,x1,y1,row,col,X1,Y1,hn)
bars=0;
while bars>=0
    subplot(1,2,1)
    bar(bars)
    axis([0 3 0 110])
    grid on
    ylabel('Temperature')
    set(gca,'XTick',[])

    bars=bars+randi(10,1,1);
    
    subplot(1,2,2)
    rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
    text(1,((row*4)+8),'BASE')
    hold on
    cp=[5 ((row*4)+6)]; % connectiong point of the base station
    
    plot(x,y,'go','LineWidth',5)
    plot(x1,y1,'g*')
    [r c]=size(deadnodeset);
    for i=1:r
        plot(deadnodeset(i,1),deadnodeset(i,2),'ko','LineWidth',5)
    end    
    set(gca,'XTick',[])
    set(gca,'YTick',[])
    
    [X,Y]=bully2(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);

    [ra ca]=size(unik_deadnodes);
    for i=1:ra
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ro','LineWidth',3)    
        plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'c');
    end
    pause(0.5)
    for i=1:ra    
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'yo','LineWidth',3)
        plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'c');
    end
    pause(0.5)    
    if bars>=90
        break
    end
end
    
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ko','LineWidth',3)
    plot([unik_deadnodes(i,1) cp(1)],[unik_deadnodes(i,2) cp(2)],'w')
end

Code 9 – Function M File –coloring_b.m

function coloring_b(X,Y,row,col,x,y,x1,y1)

%coloring blue
[rr cc]=size(X);
for i=1:rr
    a=X(i,:);
    b=Y(i,:);    
    nn1=[];
    nn2=[];
    for j=1:length(a)
        if a(j)==0
            nn1=[nn1 j];
        end        
        if b(j)==0
            nn2=[nn2 j];
        end
    end
    a(nn1)=[];
    b(nn2)=[];    
    for k=1:length(a)-1
        a1=a(k);
        a2=b(k);
        b1=a(k+1);
        b2=b(k+1);        
        plot([a1 b1],[a2 b2],'b','LineWidth',2) %horizontal 
    end
    %pause(0.3)       
    if ~isequal(i,rr)
        c=X(i+1,:);
        d=Y(i+1,:);
        c(nn1)=[];
        d(nn2)=[];        
        for k=1:length(a)
            a1=a(k);
            a2=b(k);
            b1=c(k);
            b2=d(k);            
            if ~isequal(b1,0) || ~isequal(b2,0)
                plot([a1 b1],[a2 b2],'b','LineWidth',2) %vertical
            end
        end
    end    
    if isequal(i,rr)
        for k=1:length(a)
            plot([a(k) 5],[b(k) ((row*4)+6)],'b','LineWidth',2)
        end
    end    
end

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))])
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))])
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))])
        end
    end
end

Code 10 – Function M File – bully.m

function [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1)
bars=0;
while bars>=0
    subplot(1,2,1)
    bar(bars)
    axis([0 3 0 110])
    grid on
    ylabel('Temperature')
    set(gca,'XTick',[])

    bars=bars+10;
    
    subplot(1,2,2)
    rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
    text(1,((row*4)+8),'BASE')
    hold on
    coloring_b(X1,Y1,row,col,x,y,x1,y1)

    plot(x,y,'go','LineWidth',5)
    plot(x1,y1,'g*')
    set(gca,'XTick',[])
    set(gca,'YTick',[])

    [r c]=size(deadnodeset);
    for i=1:r
        plot(deadnodeset(i,1),deadnodeset(i,2),'ko','LineWidth',3)
    end    
    [ra ca]=size(unik_deadnodes);
    for i=1:ra
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ro','LineWidth',3)    
    end
    pause(0.5)
    for i=1:ra    
        plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'yo','LineWidth',3)
    end
    pause(0.5)    
    if bars>=90
        %bars=1;
        break
    end
end
    
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ko','LineWidth',3)
end

[rr cc]=size(x);
[r3 c3]=size(deadnodeset);

X=x;
Y=y;

for i=1:rr
    for j=1:cc     
        for k=1:r3
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)               
                a1=x1(((2*i)-1),((2*j)-1));
                a2=y1(((2*i)-1),((2*j)-1));
                b1=x1(((2*i)-1),(2*j));
                b2=y1(((2*i)-1),(2*j));
                c1=x1((2*i),((2*j)-1));
                c2=y1((2*i),((2*j)-1));
                d1=x1((2*i),(2*j));
                d2=y1((2*i),(2*j));
                
                a=[a1 a2];
                b=[b1 b2];
                c=[c1 c2];
                d=[d1 d2];

                for l=1:r3 
                    if a(1)==deadnodeset(l,1) && a(2)==deadnodeset(l,2)
                        a=[];
                        break
                    else
                        X(i,j)=a(1); %a becomes the new head
                        Y(i,j)=a(2);
                    end
                end
                    
                if isempty(a)
                   for l=1:r3                       
                       if b(1)==deadnodeset(l,1) && b(2)==deadnodeset(l,2)
                           b=[];
                           break
                       else
                            X(i,j)=b(1); %b becomes the new head
                            Y(i,j)=b(2);
                       end
                   end 
                end
                
                if isempty(b)
                   for l=1:r3                       
                       if c(1)==deadnodeset(l,1) && c(2)==deadnodeset(l,2)
                           c=[];
                          break
                       else
                            X(i,j)=c(1); %c becomes the new head
                            Y(i,j)=c(2);                            
                       end
                   end 
                end
               
                if isempty(c)
                   for l=1:r3                       
                       if d(1)==deadnodeset(l,1) && d(2)==deadnodeset(l,2)
                           d=[];
                           break
                       else
                            X(i,j)=d(1); %d becomes the new head
                            Y(i,j)=d(2);
                       end
                   end 
                end
                
                if isempty(d)
                    X(i,j)=0; %the whole cluster is burnt
                    Y(i,j)=0;
                end
            end
        end
    end
end
% new head nodes obtained (up)
for i=1:rr
    for j=1:cc
        xyz=0;
        if x(i,j)==X(i,j) && y(i,j)==Y(i,j)
            xyz=1;
        else
            xyz=0;
            break
        end        
    end
    if xyz==0
        break
    end
end

if xyz==1
    for i=1:row    
        for j=1:col
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])        
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])        
        end
    end
end  

[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    %delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')

                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end        
            
            clear k abc
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot(X(i,j),Y(i,j),'go','LineWidth',5)
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
                    abc=plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),((2*j)-1))) && isequal(deadnodeset(k,2),y1(((2*i)-1),((2*j)-1)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && ~isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
                    abc=plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1(((2*i)-1),(2*j))) && isequal(deadnodeset(k,2),y1(((2*i)-1),(2*j)))
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))],'w')
                    break
                end
            end
            
            clear k abc
            
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && ~isequal(deadnodeset(k,2),y1((2*i),((2*j)-1)))
                    abc=plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),((2*j)-1))) && isequal(deadnodeset(k,2),y1((2*i),((2*j)-1))) 
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))],'w')
                    break
                end
            end
            
            clear k abc
        
            for k=1:r3
                if ~isequal(deadnodeset(k,1),x1((2*i),(2*j))) && ~isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
                    abc=plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))]);
                elseif isequal(deadnodeset(k,1),x1((2*i),(2*j))) && isequal(deadnodeset(k,2),y1((2*i),(2*j)))                
%                    delete(abc)
%                    disp('br')
                    plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))],'w')        
                    break
                end
            end                    
        end
    end
end

%clear rr cc
coloring_w(X1,Y1,row,col,x,y,x1,y1)
coloring_c(X,Y,row,col,x,y,x1,y1)



%pause    
end

Code 11 – Function M File – fire_detection.m

function [axis,time,burntnodes,basetime]=fire_detection(hn,bp,x,y,x1,y1,row,col)

%[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%figure(4)
[r c]=size(x);
[r1 c1]=size(x1);
dh=sqrt((x(1,1)-x(1,c))^2 + (y(1,1)-y(1,c))^2); %horizontal distance
dv=sqrt((x(1,1)-x(r,1))^2 + (y(1,1)-y(r,1))^2); %vertical distance
start_fire=[((dh/2)+x(1,1)) ((dv/2)+4)]; %origin of fire
%plot(start_fire(1),start_fire(2),'ko')
div=row/col;
time=0; %time in sec for the fire to reach at the edge of forest
axis=[]; %values of axis of ellipse from the start point to the boundry of forest
burntnodes=[]; % number of burnt nodes
basetime=[]; 
if (start_fire(2)-y(1,1))<(start_fire(1)-x(1,1)) %vertical distance < horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
    n=0;    
    X1=x;
    Y1=y;
    while ii<=(start_fire(2)-y(1,1)) || ij<=(start_fire(2)-y(1,1)) % detect vertical radius first, and then horizontal               
%        subplot(1,2,1)        
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div); % to detect and mark the dead nodes       
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
%        flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
        [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
        %flowinfo1(row,col,hn,x,y,deadnodeset_a,ra)       
        %checkdeadnode1(deadnodeset,x,y,x1,y1,bp)
          
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes       
%         %deadnodeset_a=deadnodeset;        
%         %checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
        
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
        time=time+1;
        axis=[axis; ii ij];
        [ra ca]=size(deadnodeset);
        burntnodes=[burntnodes ra];
        n=n+ii;
        basetime=[basetime n];        
    end    
elseif (start_fire(2)-y(1,1))>(start_fire(1)-x(1,1)) %vertical distance > horizontal distance
    ii=1; %horizontal axis
    ij=1; %vertical axis
    deadnodeset_b=[];
    deadnodeset_a=[];
    n=0;
    X1=x;
    Y1=y;
    while ii<=(start_fire(1)-x(1,1)) || ij<=(start_fire(1)-x(1,1))     % detect horizontal radius first, and then vertical
%        subplot(1,2,1)
        ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
        deadnodeset_b=deadnodeset_a;
        %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);       
        deadnodeset=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div);  % to detect and mark the dead nodes
        deadnodeset_a=deadnodeset;        
        unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b); % to delete the dead connections        
        %flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)
        [X,Y]=bully(deadnodeset,x,y,x1,y1,hn,unik_deadnodes,row,col,X1,Y1);
        X1=X;
        Y1=Y;
%         subplot(1,2,2)
%         ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);
%         [x,y,x1,y1,row,col,bp]=placing_sensors(hn);
%         % deadnodeset_b=deadnodeset_a;
%         %ellipse((ii),(ij),0,start_fire(1),start_fire(2),100);                
%         %deadnodeset1=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij); % to detect and mark the dead nodes                
%         %deadnodeset_a=deadnodeset;        
%         % checkdeadnode(deadnodeset1,x,y,x1,y1,bp)
%         checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)
       
        pause(0.3)
        ii=ii+1;
        ij=ii*div;
        
        time=time+1;
        axis=[axis; ii ij];
        [ra ca]=size(deadnodeset);
        burntnodes=[burntnodes ra];
        n=n+ii;
        basetime=[basetime n];
    end    
end
end

Code 12 – Function M File – coloring_w.m

function coloring_w(X,Y,row,col,x,y,x1,y1)

%coloring blue
[rr cc]=size(X);
for i=1:rr
    a=X(i,:);
    b=Y(i,:);    
    nn1=[];
    nn2=[];
    for j=1:length(a)
        if a(j)==0
            nn1=[nn1 j];
        end        
        if b(j)==0
            nn2=[nn2 j];
        end
    end
    a(nn1)=[];
    b(nn2)=[];    
    for k=1:length(a)-1
        a1=a(k);
        a2=b(k);
        b1=a(k+1);
        b2=b(k+1);        
        plot([a1 b1],[a2 b2],'w','LineWidth',2) %horizontal white
    end
    %pause(0.3)       
    if ~isequal(i,rr)
        c=X(i+1,:);
        d=Y(i+1,:);
        c(nn1)=[];
        d(nn2)=[];        
        for k=1:length(a)
            a1=a(k);
            a2=b(k);
            b1=c(k);
            b2=d(k);            
            if ~isequal(b1,0) || ~isequal(b2,0)
                plot([a1 b1],[a2 b2],'w','LineWidth',2) %vertical white
            end
        end
    end    
    if isequal(i,rr)
        for k=1:length(a)
            plot([a(k) 5],[b(k) ((row*4)+6)],'w','LineWidth',2)
        end
    end    
end
[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))],'w')
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))],'w')
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))],'w')
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))],'w')
        end
    end
end

Code 13 – Function M File – coloring_c.m

function coloring_c(X,Y,row,col,x,y,x1,y1)

%coloring blue
[rr cc]=size(X);
for i=1:rr
    a=X(i,:);
    b=Y(i,:);    
    nn1=[];
    nn2=[];
    for j=1:length(a)
        if a(j)==0
            nn1=[nn1 j];
        end        
        if b(j)==0
            nn2=[nn2 j];
        end
    end
    a(nn1)=[];
    b(nn2)=[];    
    for k=1:length(a)-1
        a1=a(k);
        a2=b(k);
        b1=a(k+1);
        b2=b(k+1);        
        plot([a1 b1],[a2 b2],'c','LineWidth',2) %horizontal white
    end
    pause(0.3)       
    if ~isequal(i,rr)
        c=X(i+1,:);
        d=Y(i+1,:);
        c(nn1)=[];
        d(nn2)=[];        
        for k=1:length(a)
            a1=a(k);
            a2=b(k);
            b1=c(k);
            b2=d(k);            
            if ~isequal(b1,0) || ~isequal(b2,0)
                plot([a1 b1],[a2 b2],'c','LineWidth',2) %vertical white
            end
        end
    end    
    pause(0.3)
    if isequal(i,rr)
        for k=1:length(a)
            plot([a(k) 5],[b(k) ((row*4)+6)],'c','LineWidth',2)
        end
    end    
end
[r c]=size(X);
for i=1:r
    for j=1:c
        if X(i,j)==x(i,j) && Y(i,j)==y(i,j)
            plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))])
            plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))])
            plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))])
            
        elseif X(i,j)~=0 && Y(i,j)~=0
            plot([X(i,j) x1(((2*i)-1),((2*j)-1))],[Y(i,j) y1(((2*i)-1),((2*j)-1))])
            plot([X(i,j) x1(((2*i)-1),(2*j))],[Y(i,j) y1(((2*i)-1),(2*j))])
            plot([X(i,j) x1((2*i),((2*j)-1))],[Y(i,j) y1((2*i),((2*j)-1))])
            plot([X(i,j) x1((2*i),(2*j))],[Y(i,j) y1((2*i),(2*j))])
        end
    end
end

Code 14 – Function M File – normal_condition.m

function normal_condition(hn)

[x,y,x1,y1,row,col,bp]=placing_sensors(hn);

for i=1:row
    column=col;
%     if i==1 && mod(hn,col)~=0           
%         column=hn-((row-1)*col);        
%     end
    
    a1=[x(i,1) x(i,column)];
    b1=[y(i,1) y(i,column)];
    
    abc=plot(a1,b1,'c','LineWidth',2);
    pause(0.3)
        
    if i==row
        break
    end
    
    a2=x(i,1:column);
    b2=y(i,1:column);
    a3=x(i+1,1:column);
    b3=y(i+1,1:column);
    
    for j=1:length(a2)
        plot([a2(j) a3(j)],[b2(j) b3(j)],'c','LineWidth',2)
    end      
    pause(0.3)
end

a4=x(row,:);
b4=y(row,:);

for i=1:length(a4)
    plot([bp(1) a4(i)],[bp(2) b4(i)],'c','LineWidth',2)
end

end

Code 15 – Function M File –flowcontrol2.m

function flowcontrol2(unik_deadnodes,deadnodeset_a,hn,deadnodeset_b)

%pause(0.3)
%set(gca,'Color','w')

% col=5*ceil(hn/30); %0-30hn=5col, 30-60hn=10col...
% row=ceil(hn/col); 
% 
% rectangle('Position',[0 ((row*4)+6) 5 4],'Facecolor','r')
% text(1,((row*4)+8),'BASE')
% hold on
% 
% plot(x,y,'go','LineWidth',5)
% plot(x1,y1,'g*')
% axis([-5 ((col*4)+5) 0 ((row*4)+10)])
% 
% bp=[5 ((4*row)+6)]; % base station's reciever point
% a=x(1,:);
% b=y(end,:);
% for i=1:length(a)
%     plot([a(i) bp(1)],[b(i) bp(2)],'LineWidth',2);
% end
% 
% difference=(row*col)-hn;
% if difference~=0
%     for i=1:difference
%         plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
%     end
% end
% clear difference

[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
% fresh=freshnodes(deadnodeset_a,deadnodeset_b);

% [ra ca]=size(fresh);
% for i=1:ra
%     plot(fresh(i,1),fresh(i,2),'ro','LineWidth',3)    
% end
% pause(0.3)
% for i=1:ra    
%     plot(fresh(i,1),fresh(i,2),'yo','LineWidth',3)
% end
% pause(0.3)
% for i=1:ra    
%     plot(fresh(i,1),fresh(i,2),'ko','LineWidth',3)
% end

[ra ca]=size(unik_deadnodes);
for i=1:ra
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ro','LineWidth',3)    
end
pause(0.5)
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'yo','LineWidth',3)
end
pause(0.5)
for i=1:ra    
    plot(unik_deadnodes(i,1),unik_deadnodes(i,2),'ko','LineWidth',3)
end



% [r c]=size(unik_deadnodes);
% for i=1:r
%     plot(unik_deadnodes(i,1),unik_deadnodes(1,2),'ko')
% end

for i=1:row
    column=col;
    if i==1 && mod(hn,col)~=0           
        column=hn-((row-1)*col);        
    end
    
    a1=[x(i,1) x(i,column)];
    b1=[y(i,1) y(i,column)];
    
    abc=plot(a1,b1,'c','LineWidth',2);
    
%     dead=[];
%     for j=1:column
%         for k=1:ra
%             if x(i,j)==deadnodeset_a(k,1) && y(i,j)==deadnodeset_a(k,2)                
%                 dead=[dead; x(i,j) y(i,j)];                
%             end
%         end       
%     end
%     if ~isempty(dead)
%         plot([dead(1,1) dead(end,1)],[dead(1,2) dead(end,2)],'w','LineWidth',2)       
%     end
[r1 c1]=size(x);
[r2 c2]=size(x1);
[r3 c3]=size(deadnodeset_a);

for ii=1:r1
    for j=1:c1
        for k=1:r3
            if x(ii,j)==deadnodeset_a(k,1) && y(ii,j)==deadnodeset_a(k,2)
               if ii+1<=r1
                    plot([x(ii,j) x(ii+1,j)],[y(ii,j) y(ii+1,j)],'w','LineWidth',2)                    
               end
                if ii-1>=1
                    plot([x(ii,j) x(ii-1,j)],[y(ii,j) y(ii-1,j)],'w','LineWidth',2)
                end
                if j+1<=c1
                    plot([x(ii,j) x(ii,j+1)],[y(ii,j) y(ii,j+1)],'w','LineWidth',2)
                end
                if j-1>=1
                    plot([x(ii,j) x(ii,j-1)],[y(ii,j) y(ii,j-1)],'w','LineWidth',2)
                end
                
                if ii==r1
                    plot([bp(1) x(ii,j)],[bp(2) y(ii,j)],'w','LineWidth',2)
                end
            end
        end
    end
end

        
    pause(0.3)
        
    if i==row
        break
    end
    
    a2=x(i,1:column);
    b2=y(i,1:column);
    a3=x(i+1,1:column);
    b3=y(i+1,1:column);
    
    for j=1:length(a2)
        plot([a2(j) a3(j)],[b2(j) b3(j)],'c','LineWidth',2)
    end      
    pause(0.3)
end

a4=x(row,:);
b4=y(row,:);

for i=1:length(a4)
    plot([bp(1) a4(i)],[bp(2) b4(i)],'c','LineWidth',2)
end

[r1 c1]=size(deadnodeset_a);
for i=1:r1
    plot(deadnodeset_a(i,1),deadnodeset_a(i,2),'ko','LineWidth',3)    
end

end

Code 16 – Function M File –ellipse.m

function  [x,y] = ellipse(a,b,phi,x0,y0,n)

n_dflt = 100;   % Default for number of points

if nargin < 6, n = n_dflt; end
if nargin < 5, y0 = 0; end
if nargin < 4, x0 = 0; end
if nargin < 3, phi = 0; end
if nargin < 2, b = 1; end
if nargin < 1, a = 1; end

th = linspace(0,2*pi,n+1);
x = a*cos(th);
y = b*sin(th);

c = cos(phi);
s = sin(phi);

th = x*c-y*s+x0;
y = x*s+y*c+y0;
x = th;

if nargout==0, plot(x,y,'r'); end

end

Code 17 – Function M File –checkdeadnode.m

function checkdeadnode(deadnodeset,x,y,x1,y1,bp,unik_deadnodes)

[r c]=size(x);
[r1 c1]=size(x1);
[r2 c2]=size(deadnodeset);

% for i=1:r1
%     for j=1:c1
%         for k=1:r2
%             if x1(i,j)==deadnodeset(k,1) && y1(i,j)==deadnodeset(k,2)
%                 plot([x1(i,j) x(ceil(i/2),ceil(j/2))],[y1(i,j) y(ceil(i/2),ceil(j/2))],'w')
%                 %plot(deadnodeset(k,1),deadnodeset(k,2),'ko','LineWidth',3)
%                 %[ra ca]=size(deadnodeset);
%                 
%             end
%         end
%     end
% end

[rr cc]=size(unik_deadnodes);
for ii=1:rr
    plot(unik_deadnodes(ii,1),unik_deadnodes(ii,2),'ro','LineWidth',3)    
end
pause(0.5)
for ii=1:rr    
    plot(unik_deadnodes(ii,1),unik_deadnodes(ii,2),'yo','LineWidth',3)
end
pause(0.5)
for ii=1:rr    
    plot(unik_deadnodes(ii,1),unik_deadnodes(ii,2),'ko','LineWidth',3)
end

for i=1:r
    for j=1:c
        for k=1:r2 
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)
                plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')
                plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')                       
                              
                if (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2)) && (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2)) % all four subnodes of the head are out of fire range. 
                    %plot(x1(((2*i)-1),((2*j)-1)),y1(((2*i)-1),((2*j)-1)),'go','LineWidth',5)
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i)-1)],[y1(((2*i)-1),((2*j)-1)) y1(((2*i)-1),(2*j))],'r')
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),((2*j)-1))],[y1(((2*i)-1),((2*j)-1)) y1((2*i),((2*j)-1))],'r')
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),(2*j))],[y1(((2*i)-1),((2*j)-1)) y1((2*i),(2*j))],'r')
                    
                    % a is head
                    if i+1<=r && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i+1,j)],[y1(((2*i)-1),((2*j)-1)) y(i+1,j)],'b','LineWidth',2)
                    end
                    if i-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i-1,j)],[y1(((2*i)-1),((2*j)-1)) y(i-1,j)],'b','LineWidth',2)
                    end
                    if j+1<=c && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j+1)],[y1(((2*i)-1),((2*j)-1)) y(i,j+1)],'b','LineWidth',2)
                    end
                    if j-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j-1)],[y1(((2*i)-1),((2*j)-1)) y(i,j-1)],'b','LineWidth',2)
                    end                   
                    
                elseif (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2)) && (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2))
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i)-1)],[y1(((2*i)-1),((2*j)-1)) y1(((2*i)-1),(2*j))],'r')
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),((2*j)-1))],[y1(((2*i)-1),((2*j)-1)) y1((2*i),((2*j)-1))],'r')
                    
                    % a is head
                    if i+1<=r && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i+1,j)],[y1(((2*i)-1),((2*j)-1)) y(i+1,j)],'b','LineWidth',2)
                    end
                    if i-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i-1,j)],[y1(((2*i)-1),((2*j)-1)) y(i-1,j)],'b','LineWidth',2)
                    end
                    if j+1<=c && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j+1)],[y1(((2*i)-1),((2*j)-1)) y(i,j+1)],'b','LineWidth',2)
                    end
                    if j-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j-1)],[y1(((2*i)-1),((2*j)-1)) y(i,j-1)],'b','LineWidth',2)
                    end                   
                    
                elseif (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2)) && (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2))
                    plot([x1((2*i)-1) x1(2*i)],[y1(((2*i)-1),(2*j)) y1((2*i),((2*j)-1))],'r')
                    plot([x1((2*i)-1) x1((2*i),(2*j))],[y1(((2*i)-1),(2*j)) y1((2*i),(2*j))],'r')
                    
                    %b is head
                    if i+1<=r && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i+1,j)],[y1(((2*i)-1),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i-1,j)],[y1(((2*i)-1),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j+1)],[y1(((2*i)-1),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j-1)],[y1(((2*i)-1),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2)) && (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) 
                    plot([x1((2*i),((2*j)-1)) x1((2*i),(2*j))],[y1((2*i),((2*j)-1)) y1((2*i),(2*j))],'r')
                    plot([x1((2*i),((2*j)-1)) x1(((2*i)-1),((2*j)-1))],[y1((2*i),((2*j)-1)) y1(((2*i)-1),((2*j)-1))],'r')
                    
                    % c is head
                    if i+1<=r && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i+1,j)],[y1((2*i),((2*j)-1)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i-1,j)],[y1((2*i),((2*j)-1)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j+1)],[y1((2*i),((2*j)-1)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j-1)],[y1((2*i),((2*j)-1)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2)) && (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2))
                    plot([x1((2*i),(2*j)) x1(((2*i)-1),((2*j)-1))],[y1((2*i),(2*j)) y1(((2*i)-1),((2*j)-1))],'r')
                    plot([x1((2*i),(2*j)) x1((2*i)-1)],[y1((2*i),(2*j)) y1(((2*i)-1),(2*j))],'r')
                    
                    % d is head
                    if i+1<=r && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i+1,j)],[y1((2*i),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i-1,j)],[y1((2*i),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j+1)],[y1((2*i),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j-1)],[y1((2*i),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2))
                    plot([x1(((2*i)-1),((2*j)-1)) x1((2*i)-1)],[y1(((2*i)-1),((2*j)-1)) y1(((2*i)-1),(2*j))],'r')
                    
                    % a is head
                    if i+1<=r && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i+1,j)],[y1(((2*i)-1),((2*j)-1)) y(i+1,j)],'b','LineWidth',2)
                    end
                    if i-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i-1,j)],[y1(((2*i)-1),((2*j)-1)) y(i-1,j)],'b','LineWidth',2)
                    end
                    if j+1<=c && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j+1)],[y1(((2*i)-1),((2*j)-1)) y(i,j+1)],'b','LineWidth',2)
                    end
                    if j-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j-1)],[y1(((2*i)-1),((2*j)-1)) y(i,j-1)],'b','LineWidth',2)
                    end                   
                    
                elseif (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2)) && (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2))
                    plot([x1((2*i)-1) x1((2*i),((2*j)-1))],[y1(((2*i)-1),(2*j)) y1((2*i),((2*j)-1))],'r')              
                    
                    %b is head
                    if i+1<=r && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i+1,j)],[y1(((2*i)-1),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i-1,j)],[y1(((2*i)-1),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j+1)],[y1(((2*i)-1),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j-1)],[y1(((2*i)-1),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2)) && (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2))
                    plot([(x1((2*i),((2*j)-1))) x1((2*i),(2*j))],[y1((2*i),((2*j)-1)) y1((2*i),(2*j))],'r')
                    
                    % c is head
                    if i+1<=r && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i+1,j)],[y1((2*i),((2*j)-1)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i-1,j)],[y1((2*i),((2*j)-1)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j+1)],[y1((2*i),((2*j)-1)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j-1)],[y1((2*i),((2*j)-1)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2)) && (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2))
                    plot([x1((2*i),(2*j)) x1(((2*i)-1),((2*j)-1))],[y1((2*i),(2*j)) y1(((2*i)-1),((2*j)-1))],'r')
                    
                    % d is head
                    if i+1<=r && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i+1,j)],[y1((2*i),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i-1,j)],[y1((2*i),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j+1)],[y1((2*i),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j-1)],[y1((2*i),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) 
                    % a is head
                    if i+1<=r && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i+1,j)],[y1(((2*i)-1),((2*j)-1)) y(i+1,j)],'b','LineWidth',2)
                    end
                    if i-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i-1,j)],[y1(((2*i)-1),((2*j)-1)) y(i-1,j)],'b','LineWidth',2)
                    end
                    if j+1<=c && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j+1)],[y1(((2*i)-1),((2*j)-1)) y(i,j+1)],'b','LineWidth',2)
                    end
                    if j-1>=1 && x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1(((2*i)-1),((2*j)-1)) x(i,j-1)],[y1(((2*i)-1),((2*j)-1)) y(i,j-1)],'b','LineWidth',2)
                    end                   
                    
                elseif (x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2))
                    %b is head
                    if i+1<=r && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i+1,j)],[y1(((2*i)-1),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i-1,j)],[y1(((2*i)-1),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j+1)],[y1(((2*i)-1),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i)-1)~=deadnodeset(k,1) && y1(((2*i)-1),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i)-1) x(i,j-1)],[y1(((2*i)-1),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2))
                    % c is head
                    if i+1<=r && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i+1,j)],[y1((2*i),((2*j)-1)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i-1,j)],[y1((2*i),((2*j)-1)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j+1)],[y1((2*i),((2*j)-1)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),((2*j)-1))~=deadnodeset(k,1) && y1((2*i),((2*j)-1))~=deadnodeset(k,2)
                        plot([x1((2*i),((2*j)-1)) x(i,j-1)],[y1((2*i),((2*j)-1)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
                elseif (x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2))
                    % d is head
                    if i+1<=r && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i+1,j)],[y1((2*i),(2*j)) y(i+1,j)],'b','LineWidth',3)                    
                    end
                    if i-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i-1,j)],[y1((2*i),(2*j)) y(i-1,j)],'b','LineWidth',3)
                    end
                    if j+1<=c && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j+1)],[y1((2*i),(2*j)) y(i,j+1)],'b','LineWidth',3)
                    end
                    if j-1>=1 && x1((2*i),(2*j))~=deadnodeset(k,1) && y1((2*i),(2*j))~=deadnodeset(k,2)
                        plot([x1((2*i),(2*j)) x(i,j-1)],[y1((2*i),(2*j)) y(i,j-1)],'b','LineWidth',3)
                    end                                                          
                    
%                 a=(x1(((2*i)-1),((2*j)-1))~=deadnodeset(k,1)) && (y1(((2*i)-1),((2*j)-1))~=deadnodeset(k,2)) 
%                 b=(x1((2*i)-1)~=deadnodeset(k,1)) &&  (y1(((2*i)-1),(2*j))~=deadnodeset(k,2))
%                 c=(x1((2*i),((2*j)-1))~=deadnodeset(k,1)) &&  (y1((2*i),((2*j)-1))~=deadnodeset(k,2))
%                 d=(x1((2*i),(2*j))~=deadnodeset(k,1)) &&  (y1((2*i),(2*j))~=deadnodeset(k,2))

                    
                                   
                end         
                
                 
                if i+1<=r
                    plot([x(i,j) x(i+1,j)],[y(i,j) y(i+1,j)],'w','LineWidth',3)                    
                end
                if i-1>=1
                    plot([x(i,j) x(i-1,j)],[y(i,j) y(i-1,j)],'w','LineWidth',3)
                end
                if j+1<=c
                    plot([x(i,j) x(i,j+1)],[y(i,j) y(i,j+1)],'w','LineWidth',3)
                end
                if j-1>=1
                    plot([x(i,j) x(i,j-1)],[y(i,j) y(i,j-1)],'w','LineWidth',3)
                end
                                
                if i==r
                    plot([bp(1) x(i,j)],[bp(2) y(i,j)],'w','LineWidth',2)
                end
                                
                plot(deadnodeset(k,1),deadnodeset(k,2),'ko','LineWidth',3)
            end
        end
    end
end

Code 18 – Function M File –checkdeadnode2.m

function unik_deadnodes=checkdeadnode2(deadnodeset_a,deadnodeset_b)

[ra ca]=size(deadnodeset_a);
[rb cb]=size(deadnodeset_b);
unik_deadnodes=deadnodeset_a;
x=[];
% deadnodeset_a
% deadnodeset_b

for i=1:ra
    for j=1:rb
        if deadnodeset_b(j,1)==deadnodeset_a(i,1) && deadnodeset_b(j,2)==deadnodeset_a(i,2)
            x=[x i];
        end
    end
end

unik_deadnodes(x,:)=[];

% is=[];
% for i=1:ra
%     for j=1:rb
%         if deadnodeset_b(j,1)==deadnodeset_a(i,1) && deadnodeset_b(j,2)==deadnodeset_a(i,2)
%             is=[is i];
%         end
%     end
% end
% %is
% if isempty(is)
%     unik_deadnodes=deadnodeset_a;
% else    
%     ind=1;
%     unik_deadnodes=[];
%     for i=1:ra        
%         if i<=length(is)
%             if i==is(ind)
%                 ind=ind+1;
%                 continue        
%             end
%         end
%         unik_deadnodes=[unik_deadnodes; deadnodeset_a(i,1) deadnodeset_a(i,2)];
%     end
% end




% [r1 c1]=size(x);
% [r2 c2]=size(x1);
% [r3 c3]=size(unik_deadnodes);
% 
% for i=1:r1
%     for j=1:c1
%         for k=1:r3
%             if x(i,j)==unik_deadnodes(k,1) && y(i,j)==unik_deadnodes(k,2)
%                 if i+1<=r1
%                     plot([x(i,j) x(i+1,j)],[y(i,j) y(i+1,j)],'w','LineWidth',2)                    
%                 end
%                 if i-1>=1
%                     plot([x(i,j) x(i-1,j)],[y(i,j) y(i-1,j)],'w','LineWidth',2)
%                 end
%                 if j+1<=c1
%                     plot([x(i,j) x(i,j+1)],[y(i,j) y(i,j+1)],'w','LineWidth',2)
%                 end
%                 if j-1>=1
%                     plot([x(i,j) x(i,j-1)],[y(i,j) y(i,j-1)],'w','LineWidth',2)
%                 end
%                 
%                 if i==r1
%                     plot([bp(1) x(i,j)],[bp(2) y(i,j)],'w','LineWidth',2)
%                 end
%             end
%         end
%     end
% end
% 
% for i=1:r2
%     for j=1:c2
%         for k=1:r3
%             if x1(i,j)==unik_deadnodes(k,1) && y1(i,j)==unik_deadnodes(k,2)
%                 plot([x1(i,j) x(ceil(i/2),ceil(j/2))],[y1(i,j) y(ceil(i/2),ceil(j/2))],'w')
%             end
%         end
%     end
% end

% for i=1:ra
%     plot(deadnodeset_a(i,1),deadnodeset_a(i,2),'ko','LineWidth',3)
% end

end

Code 19 – Function M File –dead_nodes.m

function [deadnodeset]=dead_nodes(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij,div)

deadnodeset=[];
for i=1:r
    for j=1:c                
        if (((x(i,j)-start_fire(1))^2)/(ii^2)) + (((y(i,j)-start_fire(2))^2)/(ij^2)) <= 1
            deadnodeset=[deadnodeset ; x(i,j) y(i,j)];
        end                    
    end
end
        
for i=1:r1
    for j=1:c1
        if (((x1(i,j)-start_fire(1))^2)/(ii^2)) + (((y1(i,j)-start_fire(2))^2)/(ij^2)) <= 1
            deadnodeset=[deadnodeset ; x1(i,j) y1(i,j)];
        end                                    
    end
end
        
%         difference=(r*c)-hn;
%         if difference~=0
%             for i=1:difference
%                 plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
%             end
%         end
%         clear difference
%         difference=(r1*c1)-(hn*4);
%         if ~isempty(difference)
%         d=difference/2;
%             for i=0:d-1
%                 plot(x1(1,c1-i),y1(1,c1-i),'wo','LineWidth',5)
%                 plot(x1(2,c1-i),y1(2,c1-i),'wo','LineWidth',5)        
%             end
%         end
        
end

Code 20 – Function M File –freshnodes.m

function fresh=freshnodes(deadnodeset_a,deadnodeset_b)

fresh=[];
fresh2=[];
if isempty(deadnodeset_b)
    fresh=deadnodeset_a;
else
    [r1 c1]=size(deadnodeset_a);
    [r2 c2]=size(deadnodeset_b);
    
%     for i=1:r1
%         for j=1:r2    
%             if deadnodeset_a(i,1)~=deadnodeset_b(j,1) && deadnodeset_a(i,2)~=deadnodeset_b(j,2)
%                 fresh=[fresh; deadnodeset_a(i,1) deadnodeset_a(i,2)];
%             end 
%         end
%     end               
end

end

Code 21 – Function M File –dead_nodes1.m

function [deadnodeset]=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij)

deadnodeset=[];
        for i=1:r
            for j=1:c                
                a1=sqrt(((start_fire(1)-x(i,j))^2) + ((start_fire(2)-y(i,j))^2));
                if a1<ii || a1<ij                    
                    deadnodeset=[deadnodeset ; x(i,j) y(i,j)];
                    plot(x(i,j),y(i,j),'ko','LineWidth',3)
                end
            end
        end
        
        for i=1:r1
            for j=1:c1
                a2=sqrt(((start_fire(1)-x1(i,j))^2) + ((start_fire(2)-y1(i,j))^2));
                if a2<ii || a2<ij                    
                    deadnodeset=[deadnodeset ; x1(i,j) y1(i,j)];
                    plot(x1(i,j),y1(i,j),'ko','LineWidth',3)
                end
            end
        end
        
        difference=(r*c)-hn;
        if difference~=0
            for i=1:difference
                plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
            end
        end
        clear difference
        difference=(r1*c1)-(hn*4);
        if ~isempty(difference)
        d=difference/2;
            for i=0:d-1
                plot(x1(1,c1-i),y1(1,c1-i),'wo','LineWidth',5)
                plot(x1(2,c1-i),y1(2,c1-i),'wo','LineWidth',5)        
            end
        end
        
end

Code 22 – Function M File –dead_nodes1.m

function [deadnodeset]=dead_nodes1(r,c,r1,c1,start_fire,x,y,x1,y1,hn,ii,ij)

deadnodeset=[];
        for i=1:r
            for j=1:c                
                a1=sqrt(((start_fire(1)-x(i,j))^2) + ((start_fire(2)-y(i,j))^2));
                if a1<ii || a1<ij                    
                    deadnodeset=[deadnodeset ; x(i,j) y(i,j)];
                    plot(x(i,j),y(i,j),'ko','LineWidth',3)
                end
            end
        end
        
        for i=1:r1
            for j=1:c1
                a2=sqrt(((start_fire(1)-x1(i,j))^2) + ((start_fire(2)-y1(i,j))^2));
                if a2<ii || a2<ij                    
                    deadnodeset=[deadnodeset ; x1(i,j) y1(i,j)];
                    plot(x1(i,j),y1(i,j),'ko','LineWidth',3)
                end
            end
        end
        
        difference=(r*c)-hn;
        if difference~=0
            for i=1:difference
                plot(x(1,end-(i-1)) , y(1,end-(i-1)) , 'wo','LineWidth',5)
            end
        end
        clear difference
        difference=(r1*c1)-(hn*4);
        if ~isempty(difference)
        d=difference/2;
            for i=0:d-1
                plot(x1(1,c1-i),y1(1,c1-i),'wo','LineWidth',5)
                plot(x1(2,c1-i),y1(2,c1-i),'wo','LineWidth',5)        
            end
        end
        
end

Code 23 – Function M File –flowinfo1.m

function flowinfo1(row,col,hn,x,y,deadnodeset_a,ra)

% flow of info during fire

for i=1:row
    column=col;
    if i==1 && mod(hn,col)~=0           
        column=hn-((row-1)*col);        
    end 
    
    a1=[x(i,1) x(i,column)];
    b1=[y(i,1) y(i,column)];
    
    abc=plot(a1,b1,'c','LineWidth',2);
    blanks=[];
    for j=1:col
        for k=1:ra
            if deadnodeset_a(k,1)==x(i,j) && deadnodeset_a(k,2)==y(i,j)
                blanks=[blanks;  x(i,j) y(i,j)];
            end
        end
    end
    if ~isempty(blanks)
        plot([blanks(1,1) blanks(end,1)],[blanks(1,2) blanks(end,2)],'w','LineWidth',2)
        
    end
            
    pause(0.3)
        
    if i==row
        break
    end
    
    a2=x(i,1:column);
    b2=y(i,1:column);
    a3=x(i+1,1:column);
    b3=y(i+1,1:column);
    
    for j=1:length(a2)
        plot([a2(j) a3(j)],[b2(j) b3(j)],'c','LineWidth',2)
    end      
    pause(0.3)
end

a4=x(row,:);
b4=y(row,:);

for i=1:length(a4)
    plot([bp(1) a4(i)],[bp(2) b4(i)],'c','LineWidth',2)
end

Code 24 – Function M File –test_scene.m

function test_scene(hn)

[x,y,x1,y1,row,col,bp]=placing_sensors(hn);
% stage 1
plot([x(3,3) x(4,3)],[y(3,3) y(4,3)],'w','LineWidth',2) %up
plot([x(3,3) x(2,3)],[y(3,3) y(2,3)],'w','LineWidth',2) %down
plot([x(3,3) x(3,2)],[y(3,3) y(3,2)],'w','LineWidth',2) %left
plot([x(3,3) x(3,4)],[y(3,3) y(3,4)],'w','LineWidth',2) %right
plot(x(3,3),y(3,3),'ko','LineWidth',5)

plot([x(4,3) x(5,3)],[y(4,3) y(5,3)],'w','LineWidth',2) %up
plot([x(4,3) x(3,3)],[y(4,3) y(3,3)],'w','LineWidth',2) %down
plot([x(4,3) x(4,2)],[y(4,3) y(4,2)],'w','LineWidth',2) %left
plot([x(4,3) x(4,4)],[y(4,3) y(4,4)],'w','LineWidth',2) %right
plot(x(4,3),y(4,3),'ko','LineWidth',5)

plot(x1(6,5),y1(6,5),'k*')
plot(x1(6,6),y1(6,6),'k*')
plot([x1(6,5) x(3,3)],[y1(6,5) y(3,3)],'w')
plot([x1(6,6) x(3,3)],[y1(6,6) y(3,3)],'w')
plot([x1(5,5) x(3,3)],[y1(5,5) y(3,3)],'w')
plot([x1(5,6) x(3,3)],[y1(5,6) y(3,3)],'w')


plot(x1(7,5),y1(7,5),'k*')
plot(x1(7,6),y1(7,6),'k*')
plot([x1(7,5) x(4,3)],[y1(7,5) y(4,3)],'w')
plot([x1(7,6) x(4,3)],[y1(7,6) y(4,3)],'w')
plot([x1(8,5) x(4,3)],[y1(8,5) y(4,3)],'w')
plot([x1(8,6) x(4,3)],[y1(8,6) y(4,3)],'w')

plot([x1(8,5) x(4,2)],[y1(8,5) y(4,2)],'LineWidth',2)
plot([x1(8,5) x(4,4)],[y1(8,5) y(4,4)],'LineWidth',2)
plot([x1(8,5) x(5,3)],[y1(8,5) y(5,3)],'LineWidth',2)
plot(x1(8,5),y1(8,5),'go','LineWidth',5)
plot([x1(8,5) x1(8,6)],[y1(8,5) y1(8,6)])

plot([x1(5,5) x(3,2)],[y1(5,5) y(3,2)],'LineWidth',2)
plot([x1(5,5) x(3,4)],[y1(5,5) y(3,4)],'LineWidth',2)
plot([x1(5,5) x(2,3)],[y1(5,5) y(2,3)],'LineWidth',2)
plot(x1(5,5),y1(5,5),'go','LineWidth',5)
plot([x1(5,5) x1(5,6)],[y1(5,5) y1(5,6)])

Code 25 – Function M File – checkdeadnode1.m

function checkdeadnode1(deadnodeset,x,y,x1,y1,bp)

[r c]=size(x);
[r1 c1]=size(x1);
[r2 c2]=size(deadnodeset);

for i=1:r1
    for j=1:c1
        for k=1:r2
            if x1(i,j)==deadnodeset(k,1) && y1(i,j)==deadnodeset(k,2)
                plot([x1(i,j) x(ceil(i/2),ceil(j/2))],[y1(i,j) y(ceil(i/2),ceil(j/2))],'w')
                plot(deadnodeset(k,1),deadnodeset(k,2),'ko','LineWidth',3)
            end
        end
    end
end

for i=1:r
    for j=1:c
        for k=1:r2 
            if x(i,j)==deadnodeset(k,1) && y(i,j)==deadnodeset(k,2)
                plot([x(i,j) x1(((2*i)-1),((2*j)-1))],[y(i,j) y1(((2*i)-1),((2*j)-1))],'w')        
                plot([x(i,j) x1(((2*i)-1),(2*j))],[y(i,j) y1(((2*i)-1),(2*j))],'w')
                plot([x(i,j) x1((2*i),((2*j)-1))],[y(i,j) y1((2*i),((2*j)-1))],'w')
                plot([x(i,j) x1((2*i),(2*j))],[y(i,j) y1((2*i),(2*j))],'w')                       
                
                for l=1:r2
                    if x1(((2*i)-1),((2*j)-1))~=deadnodeset(l,1) && y1(((2*i)-1),((2*j)-1))~=deadnodeset(l,2) % a is not dead and a is head
                        for m=1:r2
                            if x1((2*i)-1)~=deadnodeset(m,1) && y1(((2*i)-1),(2*j))~=deadnodeset(m,2) %b is not dead
                                plot([x1(((2*i)-1),((2*j)-1)) x1((2*i)-1)],[y1(((2*i)-1),((2*j)-1)) y1(((2*i)-1),(2*j))],'r') %ab
                            end
                        end
                        for m=1:r2
                            if x1((2*i),((2*j)-1))~=deadnodeset(m,1) && y1((2*i),((2*j)-1))~=deadnodeset(m,2)%c is not dead
                                plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),((2*j)-1))],[y1(((2*i)-1),((2*j)-1))  y1((2*i),((2*j)-1))],'r') %ac
                            end
                        end
                        for m=1:r2
                           if x1((2*i),(2*j))~=deadnodeset(m,1) && y1((2*i),(2*j))~=deadnodeset(m,2) %d is not dead
                                plot([x1(((2*i)-1),((2*j)-1)) x1((2*i),(2*j))],[y1(((2*i)-1),((2*j)-1))  y1((2*i),(2*j))],'r') %ad
                            end
                        end 
                        
                        
                        
                    else %a is dead
                        if x1((2*i)-1)~=deadnodeset(l,1) && y1(((2*i)-1),(2*j))~=deadnodeset(l,2) %b is not dead
                            for m=1:r2
                                if x1((2*i),((2*j)-1))~=deadnodeset(m,1) && y1((2*i),((2*j)-1))~=deadnodeset(m,2) %c is not dead
                                    plot([x1((2*i)-1) x1((2*i),((2*j)-1))],[y1(((2*i)-1),(2*j))  y1((2*i),((2*j)-1))],'r') %bc
                                end
                            end
                            for m=1:r2
                                if x1((2*i),(2*j))~=deadnodeset(m,1) && y1((2*i),(2*j))~=deadnodeset(m,2) % d is not dead
                                    plot([x1((2*i)-1) x1((2*i),(2*j))],[y1(((2*i)-1),(2*j))  y1((2*i),(2*j))],'r') %bd
                                end
                            end 
                        else % b is dead
                            if x1((2*i),((2*j)-1))~=deadnodeset(l,1) && y1((2*i),((2*j)-1))~=deadnodeset(l,2)% c is not dead
                                for m=1:r2
                                    if x1((2*i),(2*j))~=deadnodeset(m,1) && y1((2*i),(2*j))~=deadnodeset(m,2) % d is not dead
                                        plot([x1((2*i),((2*j)-1)) x1((2*i),(2*j))],[y1((2*i),((2*j)-1))  y1((2*i),(2*j))],'r') %cd
                                    end
                                end 
                            else % c is dead
                                if x1((2*i),(2*j))~=deadnodeset(l,1) && y1((2*i),(2*j))~=deadnodeset(l,2) %d is not dead
                                    
                                end
                            end
                        end
                    end
                end
            
                                    
                                
                            
                                   
%                 a=x1(((2*i)-1),((2*j)-1))    y1(((2*i)-1),((2*j)-1)) 
%                 b=x1((2*i)-1)       y1(((2*i)-1),(2*j))
%                 c=x1((2*i),((2*j)-1))      y1((2*i),((2*j)-1))
%                 d=x1((2*i),(2*j))       y1((2*i),(2*j))
                if i+1<=r
                    plot([x(i,j) x(i+1,j)],[y(i,j) y(i+1,j)],'w','LineWidth',3)                    
                end
                if i-1>=1
                    plot([x(i,j) x(i-1,j)],[y(i,j) y(i-1,j)],'w','LineWidth',3)
                end
                if j+1<=c
                    plot([x(i,j) x(i,j+1)],[y(i,j) y(i,j+1)],'w','LineWidth',3)
                end
                if j-1>=1
                    plot([x(i,j) x(i,j-1)],[y(i,j) y(i,j-1)],'w','LineWidth',3)
                end
                                
                if i==r
                    plot([bp(1) x(i,j)],[bp(2) y(i,j)],'w','LineWidth',2)
                end
                                
                plot(deadnodeset(k,1),deadnodeset(k,2),'ko','LineWidth',3)
            end
        end
    end
end

        

Code 26 – Function M File – graphs.m

function graphs(axis,time,burntnodes,x1,y1,basetime,x,y,times,unik,start_fire)

figure(5)
subplot(2,2,1)
area=[];
for i=1:time
    a=(axis(i,1))*axis(i,2)*pi;
    area=[area a];
end

plot(1:time,area)
title('Area of fire VS Time')
xlabel('Time(hr)')
ylabel('Area(sq mts)')

%figure(6)
subplot(2,2,2)
plot(1:time,burntnodes,'r*','LineWidth',4)
title('Number of nodes burnt VS Time')
xlabel('Time(hrs)')
ylabel('Number of nodes')

%figure(7)
subplot(2,2,3)
base_distance=200; %distance of base station from the origin of fire (in this case centre of forest)
rem_time=[];
for i=1:length(basetime)
    rem=base_distance-basetime(i);
    rem_time=[rem_time rem];
end
plot(1:time,rem_time)
title('Time taken for fire to reach the base station')
xlabel('Levels of fire advancement')
ylabel('Time taken to reach base')

[r1 c1]=size(x);
[r2 c2]=size(x1);
totalnodes=(r1*c1)+(r2*c2);

%figure(8)
subplot(2,2,4)
pktratio=[];
for i=1:length(burntnodes)
    pktratio=[pktratio (totalnodes-burntnodes(i))];    
end

plot(1:time,pktratio)
title('Packet Ratio VS Time')
xlabel('Time')
ylabel('Packet Ratio')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Press enter to continue..')
pause
disp('Number of nodes burnt along with time taken for them to burn')
disp('Shown in the excel file')

%mat={'Nodes burnt at time t' 'Total nodes burnt' 'Year' 'Month' 'Date' 'Hrs' 'Minutes' 'Seconds'};
tot=[];
t=0;
for i=1:length(unik)
    t=t+unik(i);
    tot=[tot t];
end    
% unik
% tot
% times
answers=[unik' tot' times];

[num,txt,raw]=xlsread('Timeframe');
[row col]=size(num);
blank_mat=[];
for i=1:row
    blank_mat(i,:)= zeros(1,col);
end
xlswrite('Timeframe',blank_mat,1,'A3')

xlswrite('Timeframe',answers,1,'A3')
% answers
% mat1=[mat; num2cell(answers)];
% disp(mat1)

figure(6)
long=sqrt((x1(1,1)-x1(end,end))^2+(y1(1,1)-y1(end,end))^2); %longest distance between 2 sensors
short=sqrt((x1(1,1)-x(1,1))^2+(y1(1,1)-y(1,1))^2); %shortest distance between 2 sensors

dist=linspace(short,long,10); %distances between the sensors from "short" to "long"
e=linspace(90,20,10); %remaining energy levels
gap=e(2)-e(1);
energy=[];
for i=1:length(e)
    a=e(i);
    b=e(i)+gap;
    x=a + (b-a).*rand(1,1);
    energy=[energy x]; %declining energy of the nodes as distance between them increases
end
plot(dist,energy)
title('Remaining energy level of deployed nodes')
xlabel('Average distance between sensors')
ylabel('Remaining energy level (%)')

clear long short dist
figure(7)
long=sqrt((start_fire(1)-x1(1,1))^2+((start_fire(2)-y1(1,1))^2)); %longest distance between the fire start point and a sensor
dist1=[];
[r c]=size(x1);
for i=1:r
    for j=1:c
        d=sqrt((start_fire(1)-x1(i,j))^2+((start_fire(2)-y1(i,j))^2));
        dist1=[dist1;i j d]; %distances of start point with all the sub nodes
    end
end
dist2=[];
[r c]=size(x);
for i=1:r
    for j=1:c
        d=sqrt((start_fire(1)-x(i,j))^2+((start_fire(2)-y(i,j))^2));
        dist2=[dist2;i j d]; %distances of start point with all the head nodes
    end
end

[a,ind1]=sort(dist1(:,3));
[b,ind2]=sort(dist2(:,3));
if a(1)<b(1)
    i=dist1(ind1(1),1);
    j=dist1(ind1(1),2);
    short=sqrt((start_fire(1)-x1(i,j))^2+((start_fire(2)-y1(i,j))^2)); %shortest distance between the start point and a sub node
elseif a(1)>b(1)
    i=dist2(ind1(1),1);
    j=dist2(ind1(1),2);
    short=sqrt((start_fire(1)-x(i,j))^2+((start_fire(2)-y(i,j))^2)); %shortest distance between the start point and a head node
elseif a(1)==b(1)
    i=dist2(ind1(1),1);
    j=dist2(ind1(1),2);
    short=sqrt((start_fire(1)-x(i,j))^2+((start_fire(2)-y(i,j))^2)); %shortest distance between the start point and a head node
end

e=linspace(short,long,10); %making partitions from short to long
plot(e)
title('Time required for the sensor nodes to sense the fire threat from various distances')
xlabel('Average distance between fire ignition location and sensor node')
ylabel('Time required for the sensor node to sense threat')

 

 

Recent Posts

Tags

ad-hoc networks AODV boundary detection process classification clustering clustering algorithm Colour Information computer vision Decryption Encryption EZRP ICM (Iterated Conditional Modes) image denoising image enhancement IMAGE PROCESSING image segmentation Imaging and image processing MANET Markov Random Fields neutrosophic logic optical network proposed method PSNR QLab system region growing Robert’s operator Seed point selection segmentation semi-automatic algorithm Shadow Detection shadow removal wall motion wireless communication Wireless network wireless networks Wireless Sensor Network wireless sensor networks ZRP