I want to iteratively run a set of scenarios. There are two notebooks NB1 and NB2. The former includes the following system of 2 equations, and the latter includes data and parameter specifications for scenarios. NB1 is:

a X1 + b Y1 == A1;
c X1 + d Y1 == B1;

where {A1, B1} is the set of constants, and {a, b, c, d} is the set of parameters calculated in NB2.

In the notebook NB2, I keep the data and parameters (scenarios):

benchmarkData = {A1=3, B1=7, a=2, b=9, c=5, d=4};
scenario[i]   = {A1=1, B1=0, a=1, b=8, c=5, d=4};

In NB1, I also created a matrix of 8 by 6 in order to store the solutions from i=6 scenarios. The first column is reserved for storing the solution from i=1 in the order of sol[1] = {X1, Y1, A1, B1, a, b, c, d}. I first run NB2 to define benchmarkData and scenario[i], and then go to NB1 to Get["file director of NB2"] the defined benchmark and scenario variables into NB1 and run the model. When the solution is obtained, store it to the first column of the scenario matrix created in NB1. I repeat the same procedure for each new scenario: design a new scenario in NB2, go to NB1 to run the model and store the solution in the 2nd column of the matrix, etc.

I know how to do this operation in a single notebook by using:

While[z<7, {the model and the matrix}; z++]

but I could not do the same iteration using two separate notebooks. This is a simple version of a very big model with a large dataset. That is why I had to divide the code into two notebooks to iteratively run the scenarios and get a matrix of solutions.

I tried several ways to achieve the task such as creating an index z in NB2 and store the solution in NB1 with an indexed variable but it did not work because the code always kept the very last scenario run not the earlier ones.

I would appreciate your suggestions.


2 Answers 2


I am likely missing the point, but why not store the scenarios (NB2) in a simple text file, one record per line.

3 7 2 9 5 4
1 0 1 8 5 4

You can then read the whole file from NB1 as a list, an assign values to variables in a loop.

a X1 + b Y1 == A1;
c X1 + d Y1 == B1;

data = ReadList[FileNameJoin[{$UserDocumentsDirectory, "dataN2.txt"}],
    Number, RecordLists -> True, 
   RecordSeparators -> {"\n", "\r\n", "\r"}];

z = 1;
While[z <= Length[data],
 A1 = data[[z, 1]];
 B1 = data[[z, 2]];
 a = data[[z, 3]];
 b = data[[z, 4]];
 c = data[[z, 5]];
 d = data[[z, 6]];

 Echo[Solve[a X1 + b Y1 == A1 && c X1 + d Y1 == B1, {X1, Y1}]];


Here are the results for these two entries:


  • 1
    $\begingroup$ One of the reasons why I like to run an iterative procedure is that I have many different scenarios and I did not want to keep all these scenarios in a separate text file. Instead, I just want to change few parameters in NB1 and run the model in NB2 and see the results immediately in the predefined table. Another reason is that I wanted to store the results from each scenario as a column vector of an output matrix created in NB2. Thank you so much for your suggestion. $\endgroup$ May 7, 2020 at 23:28

Here is a solution to the above problem.

When you add the following code at the end of NB2, each time you run the model with a different parameter set, the results are saved in a file created by OpenAppend[...]. New runs are appended to the previous set of results.

(*First create a file to keep the results obtained from each run*)

ESTgrowth = FileNameJoin[{"directory to keep the file", "ESTgrowth"}];
open = OpenAppend[ESTgrowth];
Write[open, newsolution];  (*`newsolution` is the results from a model run that you want to keep.*)
EST = FilePrint[ESTgrowth]
(*use DeleteFile[ESTgrowth] to delete the file from the   
directory concerned*)

This is a very convenient way of running a given model for different parameters without making any change in the model variables. Moreover, the results are nicely kept in a separate file that can be used in other calculations without needing to run the model again.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.