How can I call multiple M functions?

4 views (last 30 days)
Hello guys,
I'm trying to run the function "WattsStrogatz" 100 times and in each iteration I want to run the function "Test_Healing" 300 times, kindly see the attached screenshots. When I run my script, MATLAB says that variable G is not defined. Could you please teach me how to do it.
Thanks!
function fos=Test_Healing(Nodes,k)
% Generating a spanning tree graph from Small World architecture
%G = WattsStrogatz(Nodes,2,.25);
%Nodes=100;
E=G.Edges{:,:};
%figure;
%p = plot(G);
[T,Pred]=minspantree(G);
Ea=T.Edges{:,:};
%highlight(p,T);
%figure;
%pt=plot(T);
Ed=E;
[ia, ib] = ismember(Ed, Ea, 'rows'); %E-Ea, Ed here is E
Ed(ia, :) = []; %Dormant Edges
edge=randperm(Nodes-1,k);
Tf=rmedge(T,edge);
%figure;
%Pf=plot(Tf);
% Egf(:,3)=[];
Egf=Tf.Edges{:,:};
Ef=Ea;
E_fail=[]; %Constructing Efail i.e. contains all isolated edges
[iu, iv] = ismember(Ef, Egf, 'rows'); %E-Ea, Ef here is Ea
Ef(iu, :) = []; % first failed Edge
E_fail=Ef;
[bins,binsizes]=conncomp(Tf);% no. of nodes in each component
Components=length(binsizes);%no of components in the graph after the failure happens
idx_u=find(bins~=1);%Unserved Nodes (U)
idx=find(bins==1);% V'=V-U
eff=ismember(Ea,idx_u);
E_fail=vertcat(E_fail,Ea(eff(:,1),:)); % Now all fail edges are in E_fail (Ef like in paper)
%%%%%%%%%%%%%%%%%% Algorithm %%%%%%%%%%%%%%%%%%
% Ea'=Ea=Ef
Ea_prime=Ea;
[ix, iy]=ismember(Ea_prime,E_fail,'rows');
Ea_prime(ix,:)=[];
% Ed'=Ed-Ef
Ed_prime=Ed;
[ixd iyd]=ismember(Ed_prime,E_fail,'rows');
Ed_prime(ixd,:)=[];
U=idx_u;
idxx=idx;
loopCounter = 1
while ~isempty(U)
for jj=length(U):-1:1
a=neighbors(G,idx_u(jj));
[nx ny]=ismember( a,neighbors(T,idx_u(jj)),'rows');
a(nx,:)=[];
if numel(a)==1
av=a;
elseif isempty(a)
break
else
av=randsample(a,1);
end
if ~isempty(a)
idxx(end+1)=idx_u(jj); %V'=V'+{v}
U(idx_u==idx_u(jj))=[]; %U=U-{v}
Ea_prime=vertcat(Ea_prime,[idx_u(jj) av]);
end
end
if loopCounter >100
break;
end
loopCounter = loopCounter + 1;
end
fos=(numel(idxx)/Nodes); %Resiliency Metric
end
Script:
for i=1:100
G= WattsStrogatz(400,2,0.25);
k=1:300;
fos=zeros(1,length(k));
for jj=1:length(k)
fos(jj)=Test_Healing(400,jj);
end
rs(i,:)=fos;
end
rs_avg=mean(rs,1);
figure;
plot(k,rs_avg)
  2 Comments
Walter Roberson
Walter Roberson on 27 Nov 2020
Please post the actual code instead of an image of the code. Sorry, but I am not going to type in all those lines by hand to be able to modify them for you.
Your Test_Healing function does not use its k input. It is not clear what the purpose of it is. Based on the function name, my guess is that the intention is that it should take a copy of the graph, and disconnect node #k, and then proceed to produce the spanning tree on what remains.
Your Test_Healing function indicates that it should return fos but the code you posted does not assign to fos .
Your function Test_Healing extracts the Edges from G into E, but does not use E . There does not appear to be any purpose in that.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Nov 2020
for i=1:100
G= WattsStrogatz(400,2,0.25);
k=1:300;
fos=zeros(1,length(k));
for jj=1:length(k)
fos(jj) = Test_Healing(G,jj); %CHANGED
end
rs(i,:)=fos;
end
rs_avg=mean(rs,1);
figure;
plot(k,rs_avg)
function fos=Test_Healing(G,k) %CHANGED
E=G.Edges{:,:};
[T,Pred]=minspantree(G);
Ea=T.Edges{:,:};
Ed=E;
[ia, ib] = ismember(Ed, Ea, 'rows'); %E-Ea, Ed here is E
Ed(ia, :) = []; %Dormant Edges
edge=randperm(Nodes-1,k);
Tf=rmedge(T,edge);
Egf=Tf.Edges{:,:};
Ef=Ea;
E_fail=[]; %Constructing Efail i.e. contains all isolated edges
[iu, iv] = ismember(Ef, Egf, 'rows'); %E-Ea, Ef here is Ea
Ef(iu, :) = []; % first failed Edge
E_fail=Ef;
[bins,binsizes]=conncomp(Tf);% no. of nodes in each component
Components=length(binsizes);%no of components in the graph after the failure happens
idx_u=find(bins~=1);%Unserved Nodes (U)
idx=find(bins==1);% V'=V-U
eff=ismember(Ea,idx_u);
E_fail=vertcat(E_fail,Ea(eff(:,1),:)); % Now all fail edges are in E_fail (Ef like in paper)
%%%%%%%%%%%%%%%%%% Algorithm %%%%%%%%%%%%%%%%%%
% Ea'=Ea=Ef
Ea_prime=Ea;
[ix, iy]=ismember(Ea_prime,E_fail,'rows');
Ea_prime(ix,:)=[];
% Ed'=Ed-Ef
Ed_prime=Ed;
[ixd iyd]=ismember(Ed_prime,E_fail,'rows');
Ed_prime(ixd,:)=[];
U=idx_u;
idxx=idx;
loopCounter = 1
while ~isempty(U)
for jj=length(U):-1:1
a=neighbors(G,idx_u(jj));
[nx ny]=ismember( a,neighbors(T,idx_u(jj)),'rows');
a(nx,:)=[];
if numel(a)==1
av=a;
elseif isempty(a)
break
else
av=randsample(a,1);
end
if ~isempty(a)
idxx(end+1)=idx_u(jj); %V'=V'+{v}
U(idx_u==idx_u(jj))=[]; %U=U-{v}
Ea_prime=vertcat(Ea_prime,[idx_u(jj) av]);
end
end
if loopCounter >100
break;
end
loopCounter = loopCounter + 1;
end
fos=(numel(idxx)/Nodes); %Resiliency Metric
end
  1 Comment
Waseem AL Aqqad
Waseem AL Aqqad on 27 Nov 2020
Thank you very much, Walter!
I admire your beautiful mind.

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!