In CrossValidated, the answer to a question on prediction of number of viewers of Gangnam Style is given in R. I have mapped the answer directly to Mathematica:
S0 = 10000000; a = 0.00000005; b = 0.01; k = 1.2;
views = {{0, 0}}; S = S0; inf = 1;
For[t = 1, t < 365, t++, dS = -a*inf*S;
dinf = a*inf*S - b*inf;
S = S + dS;
inf = inf + dinf;
noviews = Last[Last[views]] + k inf;
views = Append[views, {t, noviews}]];
ListPlot[views]
However, this doesn't really feel or look like good Mathematica code. I tried to follow the advice in Alternatives to procedural loops and iterating over lists in Mathematica. But I get stuck in trying to create the function that the FoldList
should use. It seems like the solution would be to have FoldList
create a number of lists, one for each "state" variable.
So my question is: how do I rewrite the For
loop that has a number of "state" variables and where the output depends on both these variables and earlier values of the output?