# Generating by plots by rerunning code

I am new to Mathematica. I created a code where I can evaluate a parameter (denoted by PoA) for a random instance of a directed acyclic graph. The code is below.

     an = 10;
al = 1;
nodes = 10;
id = an + al*VertexInDegree[s];
od = al*VertexOutDegree[s];
wd = -Log[1 - (od/id)];
wd // MatrixForm
x = (DiagonalMatrix[id] - M).wd;
poa = N[od - x, 4]
PoA = Total@poa;


I now want to generate plots of average PoA vs. nodes, by varying 'nodes' from 5 to 50 and finding average of 1000 instances of PoA. Similarly I want to set nodes to 35 and vary connectivity (defined as links/nodes) and get a plot to find average PoA. How to reuse and extend this code to do the same? Thanks!

Based on your code, we can first create a function PoAGen to generate mean PoA values as follows:

Clear[PoAGen]
PoAGen[nodes_, links_, n_: 1000] := Module[
{an = 10, al = 1, s, M, id, od, wd, x, poa, PoA},
Cases[_?NumericQ]@
id = an + al*VertexInDegree[s];
od = al*VertexOutDegree[s];
wd = -Log[1 - (od/id)];
x = (DiagonalMatrix[id] - M).wd;
poa = N[od - x, 4];
PoA = Total@poa, {n}] // Mean
]


Then we can produce the data to plot with Table as follows:

dataPlot = Table[{nodes, links, PoAGen[nodes, links]}, {nodes, 10, 20}, {links, 20, 25}];


Here we go the plot:

ListPlot[dataPlot[[All, 1, {1, 3}]], Frame -> True, FrameLabel -> {"No. of Nodes", "POA"}, PlotLabel -> "No. of Links: " ~~ ToString[dataPlot[[1, 1, 2]]]]


and

ListPlot[dataPlot[[2, All, {2, 3}]], Frame -> True, FrameLabel -> {"No. of Links", "POA"}, PlotLabel -> "No. of Nodes: " ~~ ToString[dataPlot[[1, 1, 1]]]]


Looks you are doing very interesting research! Just modify the dataPlot to produce more samples as you want.

Have fun in research with Mathematica!

• Thanks. I have a small question: What does Cases[_?NumericQ]@ do? Especially the symbols _? and @ - what do they stand for? – Bravo Apr 20 '15 at 12:33
• @Bravo, it seems the RandomGraph cannot be transformed to DirectredGraph sometimes, whereby PoA is not numeric all the time. As such, I put a Cases there to avoid the non-numeric values that might cause the Mean to fail. – sunt05 Apr 20 '15 at 12:36
• As for the _?NumericQ, it is a pattern that restricts the selected elements to be numeric. You may refer to the documentation for a thorough explanation: reference.wolfram.com/language/tutorial/… – sunt05 Apr 20 '15 at 12:38
• Thanks. Helped me get an understanding of how to write functions :) – Bravo Apr 20 '15 at 12:43
• Enjoy Mathematica:) – sunt05 Apr 20 '15 at 13:07