How to plot multiple graphs in one figure ?
I have two codes. Each code has four graphs. I want to plot two graphs in one figure. For example: Dead nodes vs Round graph of two should be in one figure. In the same way other graphs also. I tried hold on function but still not getting. How to merge the two codes in order to get the graphs ?
Code 1:
clc
clear all;
close all;
xm=100;
ym=100;
x=0;
y=0;
sink.x=0.5*xm; %location of sink on x-axis
sink.y=0.9*ym; %location of sink on y-axis
n=100 %nodes
P=0.1 ; %probability of cluster heads
Eo=0.5; %initial energy
ETX=50*0.000000001; %tx energy
ERX=50*0.000000001; %rx energy
Efs=10*0.000000000001; %free space loss
Emp=0.0013*0.000000000001; %multi path loss
%Data Aggregation Energy
EDA=5*0.000000001; %compression energy
a=1; %fraction of energy enhancment of advance nodes
rmax=3000 %maximum number of rounds
do=sqrt(Efs/Emp); %distance do is measured
Et=0; %variable just use below
A=0;
for i=1:1:n
S(i).xd=rand(1,1)*xm; %generates a random no. use to randomly distibutes nodes on x axis
XR(i)=S(i).xd;
S(i).yd=rand(1,1)*ym; %generates a random no. use to randomly distibutes nodes on y axis
YR(i)=S(i).yd;
S(i).G=0; %node is elegible to become cluster head
talha=rand*a;
S(i).E=Eo*(1+talha);
E(i)= S(i).E;
A=A+talha;
Et=Et+E(i); %estimating total energy of the network
%initially there are no cluster heads only nodes
S(i).type='N';
figure(10)
plot(S(i).xd,S(i).yd,'bo');
text(S(i).xd+1,S(i).yd-0.5,num2str(i));
title 'Node Deployment';
xlabel 'X-Coordinate(m)';
ylabel 'Y-Coordinate(m)';
hold on;
end
d1=0.765*xm/2; %distance between cluster head and base station
K=sqrt(0.5*n*do/pi)*xm/d1^2; %optimal no. of cluster heads
d2=xm/sqrt(2*pi*K); %distance between cluster members and cluster head
Er=4000*(2*n*ETX+n*EDA+K*Emp*d1^4+n*Efs*d2^2); %energy desipated in a round
S(n+1).xd=sink.x; %sink is a n+1 node, x-axis postion of a node
S(n+1).yd=sink.y; %sink is a n+1 node, y-axis postion of a node
countCHs=0; %variable, counts the cluster head
cluster=1; %cluster is initialized as 1
flag_first_dead=0; %flag tells the first node dead
flag_tenth_dead=0; %flag tells the 10th node dead
flag_all_dead=0; %flag tells all nodes dead
dead=0; %dead nodes count initialized to 0
first_dead=0;
tenth_dead=0;
all_dead=0;
allive=n;
%counter for bit transmitted to Bases Station and to Cluster Heads
packets_TO_BS=0;
packets_TO_CH=0;
for r=0:1:rmax
r
if(mod(r, round(1/P) )==0)
for i=1:1:n
S(i).G=0;
S(i).cl=0;
end
end
Ea=Et*(1-r/rmax)/n;
dead=0;
for i=1:1:n
if (S(i).E<=0)
dead=dead+1;
if (dead==1)
if(flag_first_dead==0)
first_dead=r;
flag_first_dead=1;
end
end
if(dead==0.1*n)
if(flag_tenth_dead==0)
tenth_dead=r;
flag_tenth_dead=1;
end
end
if(dead==n)
if(flag_all_dead==0)
all_dead=r;
flag_all_dead=1;
end
end
end
if S(i).E>0
S(i).type='N';
end
end
STATISTICS.DEAD(r+1)=dead;
STATISTICS.ALLIVE(r+1)=allive-dead;
countCHs=0;
cluster=1;
for i=1:1:n
if Ea>0
p(i)=P*n*(1+a)*E(i)/(n+A)*(Ea);
%p(i)=P*n*S(i).E*E(i)/(Et*Ea);
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0)
if(temp_rand<= (p(i)/(1-p(i)*mod(r,round(1/p(i))))))
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
S(i).type='C';
S(i).G=round(1/p(i))-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=distance;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
cluster=cluster+1;
distance;
if (distance>do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance ));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance ));
end
end
end
end
end
end
STATISTICS.COUNTCHS(r+1)=countCHs;
for i=1:1:n
if ( S(i).type=='N' && S(i).E>0 )
if(cluster-1>=1)
min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
min_dis_cluster=0;
for c=1:1:cluster-1
temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) );
if ( tempdo)
S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis * min_dis));
end
if (min_dis<=do)
S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis));
end
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000 );
packets_TO_CH=packets_TO_CH+1;
else
min_dis;
if (min_dis>do)
S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis * min_dis));
end
if (min_dis<=do)
S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis));
end
packets_TO_BS=packets_TO_BS+1;
end
S(i).min_dis=min_dis;
S(i).min_dis_cluster=min_dis_cluster;
else
min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
if (min_dis>do)
S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis * min_dis));
end
if (min_dis<=do)
S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis));
end
packets_TO_BS=packets_TO_BS+1;
end
end
end
Code 2:
clear;
close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%Modified LEACH%%%%%%%%%%%%%%%%%%%%%%
xm=100; %diameters of sensor network
ym=100;
sink.x=0.5*xm; %distance of base station from the network
sink.y=0.5*ym;n = 100; %no of nodesp=0.05; %probibilty of a node to become cluster headEo=0.5; %energy supplied to each node
ch=n/10;
ETX=50*0.000000001; %transmiter energy per node
ERX=50*0.000000001; %reciever energy per modeEfs=10*0.000000000001; %amplification energy when d is less than d0
Emp=0.0013*0.000000000001; %amplification energy when d is greater than d0
Efs1=Efs/10; % amp energy just for intra cluster communication.
Emp1=Emp/10;
%Data Aggregation Energy
EDA=5*0.000000001;a=Eo/2; %?rmax=3000; %no of rounds
%temprature range
tempi=50;
tempf=200;%Thresholod for transmiting data to the cluster head
h=100; %%%%%%Hard Thres%%%%hold H(t)
s=2; %%%%%%Soft thres%%%%hold S(t)sv=0; %%%%%%previously Sensed value S(v)
do=sqrt(Efs/Emp); %distance between cluster head and base station
do1=sqrt(Efs1/Emp1);
for i=1:1:n
S(i).xd=rand(1,1)*xm; %it will distribute the nodes in 1 dimension in x axis randomly.
XR(i)=S(i).xd; %we store its value in xr
S(i).yd=rand(1,1)*ym; %it will distribute the nodes in 1 dimension in y axis randomly
YR(i)=S(i).yd;
S(i).G=0; % as the no of node that have been cluster head is zero 0
S(i).E=Eo%%*(1+rand*a); %?
%ch.E=x; % initial energy of all cluster heads in network
%initially there are no cluster heads only nodes
S(i).type='N';
figure(11)
plot(S(i).xd,S(i).yd,'bo');
text(S(i).xd+1,S(i).yd-0.5,num2str(i));
title 'Wireless Sensor Network';
xlabel 'X-Coordinate(m)';
ylabel 'Y-Coordinate(m)';
hold on;
endS(n+1).xd=sink.x; %assume that base station is also a node sp total no of nodes is n and with base station it is n+1
S(n+1).yd=sink.y;countCHs=0; %the number of Stateflow objects in the current context.
cluster=1; %first cluster is selected
flag_first_dead=0;
flag_teenth_dead=0;
flag_all_dead=0;dead=0;
first_dead=0;
teenth_dead=0;
all_dead=0;allive=n;
%counter for bit transmitted to Bases Station and to Cluster Heads
packets_TO_BS=0;
packets_TO_CH=0;
for r=0:1:rmax
cv = tempi + (tempf-tempi).*rand(1,1); %%%%%%Current sensing value C(v)
if(mod(r, round(1/p) )==0) %remainder
for i=1:1:n
S(i).G=0; % it will assign to the nodes that have not been cluster head .
%%S(i).cl=0;
end
enddead=0;
for i=1:1:n if (S(i).E<=0)
dead=dead+1; if (dead==1)
if(flag_first_dead==0)
first_dead=r;
flag_first_dead=1;
end
end if(dead==0.1*n)
if(flag_teenth_dead==0)
teenth_dead=r;
flag_teenth_dead=1;
end
end
if(dead==n)
if(flag_all_dead==0)
all_dead=r;
flag_all_dead=1;
end
end
end
if S(i).E>0
S(i).type='N';
end
end
STATISTICS.DEAD(r+1)=dead;
STATISTICS.ALLIVE(r+1)=allive-dead;countCHs=0;
cluster=1;if S(i).type=='C' && S(i).E>a
for j=1:1:ch
countCHs=countCHs+1;
S(i).type='C';
S(i).G=round(1/p)-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=distance;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
cluster=cluster+1;
distance;
if (distance>do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*(distance*distance*distance*distance ));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*(distance * distance ));
end
%end
%packets_TO_BS=packets_TO_BS+1;
%PACKETS_TO_BS(r+1)=packets_TO_BS;
% packets_TO_CH=packets_TO_CH+1;
end
else
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0) if(temp_rand<= (p/(1-p*mod(r,round(1/p)))))
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
S(i).type='C';
S(i).G=round(1/p)-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=distance;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
cluster=cluster+1;
% if (cv >= h)
distance;
if (distance>do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*(distance*distance*distance*distance ));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*(distance * distance ));
end
end
end end
end
end
STATISTICS.COUNTCHS(r+1)=countCHs;for i=1:1:n
if ( S(i).type=='N' && S(i).E>0 )
if(cluster-1>=1)
min_dis=Inf;
min_dis_cluster=0;
for c=1:1:cluster-1
temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) );
if ( temp= h)
%test = cv-sv;
%if (test >= s)
min_dis;
if (min_dis>do1)
S(i).E=S(i).E- ( ETX*(4000) + Emp1*4000*( min_dis *min_dis * min_dis * min_dis));
end
if (min_dis<=do1)
S(i).E=S(i).E- ( ETX*(4000) + Efs1*4000*( min_dis * min_dis));
end
S(C(min_dis_cluster).id).E =S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000 );
packets_TO_CH=packets_TO_CH+1;
S(i).min_dis=min_dis;
S(i).min_dis_cluster=min_dis_cluster;
else
min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
if (min_dis>do)
S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis *min_dis * min_dis * min_dis));
end
if (min_dis<=do)
S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis));
end
packets_TO_BS=packets_TO_BS+1;
sv=cv;
end
end
end
STATISTICS.PACKETS_TO_CH(r+1)=packets_TO_CH;
STATISTICS.PACKETS_TO_BS(r+1)=packets_TO_BS;
end
first_dead;
teenth_dead;
all_dead;
STATISTICS.DEAD(r+1)
STATISTICS.ALLIVE(r+1)
STATISTICS.PACKETS_TO_CH(r+1)
STATISTICS.PACKETS_TO_BS(r+1)
STATISTICS.COUNTCHS(r+1)r=0:rmax;
figure (1);
plot(r,STATISTICS.DEAD);
xlabel('Rounds');
ylabel('Dead Nodes');
title('MODLEACH');
figure (2);
plot(r,STATISTICS.PACKETS_TO_BS);
xlabel('Rounds');
ylabel('Packets to BS');
title('MODLEACH');
figure (4);
plot(r,STATISTICS.PACKETS_TO_CH);
xlabel('Rounds');
ylabel('Packets to CH')
title('MODLEACH');
figure (5);
plot(r,STATISTICS.ALLIVE);
xlabel('Rounds');
ylabel('Allive nodes')
title('MODLEACH');
NOTE:-
Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.
> I want to plot two graphs in one figure
Options are
- subplot
- tiledlayout with nexttile — preferred, starting in R2019b
subplot(m,n,i) creates an axes in the i^th position of an m-by-n grid.
tiledlayout(m,n) creates an m-by-n grid upon which axes can be added using nexttile.
See documentation links for details.
Benefits to using tiledlayout
The following features are great improvements available in tiledlayout.
- Flexible grid sizes (TileArrangment property)
- Control spacing (TileSpacing and Padding properties, Community Highlight)
- Row-wise or column-wise order of axes (TileIndexing property, Community Highlight)
- Global labels such as a title, subtitle, xlabel and ylabel (labels properties)
- Global legends, starting in R2020b (legend layout property, documentation example, more demos)
- Global colorbars, starting in R2020b (colorbar layout property, documentation example, demo)
- Specify the span of an axes within the grid using nexttile(span)
Demo
tcl = tiledlayout(2,3,'TileSpacing','compact');
nexttile
plot(magic(5))
axis tight
SEE COMPLETE ANSWER CLICK THE LINK
https://www.matlabsolutions.com/resources/how-to-plot-multiple-graphs-in-one-figure-.php
https://matlabarticlesworld.blogspot.com/2024/08/how-to-plot-multiple-graphs-in-one.html