Reputation: 11
I am setting up an equilibrium simulation for a chemical reaction A + B <---> C + D and want to do a calculation of CD/AB (number of turtles) after each tick and add that value to a variable and divide that by the number of ticks run to get an average and then plot that running average. I am able to do the calculation using the Monitor button on the interface screen but would like to plot and average value vs a static value. How do you perform a calculation in Netlogo and store the value?
Upvotes: 1
Views: 43
Reputation: 17678
As soon as you want memory (that is, access to previous values of something), then the easiest way is to use a variable to store the values. You can store the whole series as a list. Let's call the variable 'equilibrium'.
At the top of your code you need:
globals [equilibrium]
to tell NetLogo that the variable exists. Somewhere in your setup you should let NetLogo know this is going to be a list, using:
set equilibrium []
Then when you calculate you can store the current value at the front of the list with something like this (or lput
if you want it at the end):
let calc count C * count D / (count A * count B)
set equilibrium fput calc equilibrium
The average can be found with mean
, as for any list. The advantage of having it at the front of the list is that it is always item 0
for the plot.
In this particular case, since you just want the average you don't need the whole list. You could simply create a variable for the total (say cum-calc) and add the current value each tick:
set cum-calc cum-calc + calc
Upvotes: 2