I am trying to model a tank with discontinuous flow into the tank and discontinuous flow out of the tank. I do it numerically. You can imagine a tank to which I feed blue balls at timepoint t0, red balls at timepoint t1 and so on. I assume instantaneous mixing of the solutions in the tank. When I take out part of the solution in the tank, I will remove same proportions of the different colored balls. After some time, I will have removed more blue balls (i.e. balls that have been longer in the tank) than balls of a color that I added to a later point. I want to calculate the residence time distribution of let's say the blue balls over time.
I post here the first iteration steps to give you an idea. My problem is that I don't manage to program the iteration. I tried to use an approach with Do and Reap/Sow to get the results of each individual iteration, but I didn't manage to solve it.
I have two example lists needed for the problem (of same length): My real lists are much longer.
timeVolIn={{0,5},{1,5},{2,2},{3,1},{4,2}}(*this as the form of a nested list with {time,volIn}*)
volOut={1,5,3,2}(*volOut at each time*)
the first iteration:
input1=timeVolIn[[1;;2]]
currentVolTank=input1[[All,2]]//Total
volOutFactor=volOut[[2]]/currentVolTank
result1=If[volOutFactor==0,#*{1,1},#*{1,volOutFactor}]/@input1 (*the "If" is necessary because my volOut[[i]] can be zero. In this case I don't want anything to happen, therefore the *{1,1}*)
the second iteration:
input2=List[result1,timeVolIn[[3]]}
currentVolTank=input2[[All,2]]//Total
volOutFactor=volOut[[3]]/currentVolTank
result2=If[volOutFactor==0,#*{1,1},#*{1,volOutFactor}]/@input2
the third iteration:
input3=List[result2,timeVolIn[[4]]}
currentVolTank=input3[[All,2]]//Total
volOutFactor=volOut[[4]]/currentVolTank
result3=If[volOutFactor==0,#*{1,1},#*{1,volOutFactor}]/@input3
I think you get the kind of iteration I need from that. I want to collect all results. This would call for NestList, but I can't manage it. The output has to be a nested list in which each the first sublist (=result of the first iteration) is of length 2, the second sublist (=result of the second itereation is of length 3) and so on.
Thanks in advance!!
Fold
/FoldList
for this purpose if you have to pass a second argument to your iteration function, in addition to the result of the previous iteration. $\endgroup$NestList
documentation.NestList
in fact does exactly what you describe. For instance:NestList[function, startingValue, numberOfIterations]
will return a list containing the starting value first, and then the results of repeatedly applyingfunction
to it. Perhaps you might want to look at the results ofNestList[2#&, 3, 4]
: this applies a doubling function four times, starting on the value 3, to give{3, 6, 12, 24, 48}
. Is this not along the lines of what you need? $\endgroup$