# How can I make many runs of a simulation and plot all the results with one expression evaluation?

I have many cells, starting with this first cell:

mcRun = 20;
randomWalk[n_] := RandomVariate[NormalDistribution[0, 1], n];

RandVar = Table[randomWalk[5], {mcRun}];
Length[RandVar]
MatrixForm[RandVar[[1 ;; 10]]] (* only display first 10 *)
ListPlot[RandVar]


Then later in a 2nd cell:

CorRandVar = RandVar * 10;


Still later in a 3rd cell:

InvCor = CDF[NormalDistribution[], CorRandVar];


And there are many more cells that depend on the value of the initial parameter mcRun that I defined in the first cell.

At the moment I use 'Evaluate Notebook' to get my results. I wait for one 'Evaluate Notebook' to complete, note down the result from running with mcRun = 20 (20 monte carlo simulations), edit mcRun = 20 to another value and repeat.

So how could I automate what 'Evaluate Notebook' does and plot, for example, InvCor (or any function/value) as a function of the input parameter mcRun, say, from 20 to 20,000? ? Or better, how could I incorporate my simulation into a Manipulate to add interactivity?

-
I am having hard time understanding the question. What do you mean by how to do a Evaluate Notebook? Since this option is in the main menu, under Evaluation? (ps, not a good idea to start variable names with UpperCase in Mathematica. Start all variables with lowerCase) – Nasser Nov 17 '12 at 3:08
Hi @Nasser M.Abbasi I know how to do an Evaluate Notebook command as it calculates every cells within the Notebook. I want to Evaluate Notebook with mcRun=20, plot the result of InvCor at this setting, then mcRun=200 again, and plot the result of InvCor and so on. Maybe some sort of parametric list because I have lots of dependencies in my code, so I have to do Evaluate Notebook many times...or maybe someone knows an easier way than having to manually do Evaluate Notebook many times? – sebastian c. Nov 17 '12 at 4:56
You should think in terms of functions as your basic units of work. Not in terms of collection of cells or notebooks. So, all what you have to do is make function(s) to do your basic unit of work (simulation, etc...) with well defined API, then you could call it with arguments or put the function in a loop or do as Glodberg suggested below and many other things so it is more easy to manage things. – Nasser Nov 17 '12 at 8:22

You might write a master function, call it runSim, that evaluates all the functions you have already defined and takes the parameter needed to make a single run as it argument. Then evaluate

Map[runSim, {p1, p2, ... }]


to get several runs at once. If several parameters need to be given for a single run, then you would evaluate

MapThread[runSim, {{p11,p12, ...}, {p21, p22, ...}, ... }


### Edit

As a concrete example, I have converted part of your code into a runSim function. I hope it will start you down the path to automating your evaluations.

randomWalk[n_] := RandomVariate[NormalDistribution[0, 1], n]

runSim[runs_Integer] /; runs >= 10 :=
Module[{randVar},
randVar = Table[randomWalk[5], {runs}];
Column[{MatrixForm[randVar[[1 ;; 10]]] ,
ListPlot[randVar]}]]

runSim[10]


The output looks like this:

-
 Hi @m_goldberg thanks – sebastian c. Nov 17 '12 at 19:37 Hi @m_goldberg, but what is by Evaluate NoteBook and I have to do this many times, how to plot something in this case? – sebastian c. Nov 18 '12 at 2:22 @sebastianc. The function that automates your simulation runs can also plot the data created by the runs. It can plot the results one at a time as each simulation completes or it can collect the data into a data set where esch record is the data returned from one plot. It can then, if this is appropriate, plot all the records in one combined plot. The possibilities are endless. Mathematica can do almost anything with data. But I can't advise on how to handle your data without knowing what you want to do with it. Just saying you want to plot it isn't enough. – m_goldberg Nov 18 '12 at 3:38