# List output as a variable within a forloop function changes

The above image shows my code. The graph plots my results for two different values of the variable "guards" (1000 and 200), having first produced lists of these figures. What I want is for mathematica to produce a series of these output lists for every value of "guards" between 0 and 1000 (they don't have to be graphed). Any idea how I automate this?

• Please post your code (not image). Jan 4, 2017 at 19:36
• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Jan 4, 2017 at 19:38

Few remarks first:

1. Don't use For for various reasons.
2. Don't start variable name with uppercase (could be internal MMA symbol).

It looks like your code calculates a recursive sequence of $S_n$ and then plots it vs $n$. Your $S_n$ depends on some parameters and $S_{n-1}$, so $S_n=f(q,S_{n-1})$. For simplicity I assume that you have just one parameter $q$. I will also assume that your $f(q,s)=sq+q$. You can plug your real function with lovely ArcTan later.

When dealing with recursive sequences FoldList and NestList are often useful. Consider following code:

f[q_, s_] := s q + q;
g[q_][{t_, s_}] := {t + 1, f[q, s]};
pop[q_, T_] := NestList[g[q], {0, 0}, T];


Function $g(q)$ has parameter $q$ and acts on pair $(t,S_n) \rightarrow (t+1,S_{n+1})$. After plugging $g$ into NestList we generate a list of pairs that you want to plot.

Thus pop[q_,T_] is a function that returns a list of pairs with $T+1$ elements. Now plotting is easy, since we just call pop with different parameters.

ListPlot[Table[pop[i, 50], {i, 0.1, 0.8, 0.1}]]


• Thanks a lot, that's worked like a charm. Jan 5, 2017 at 0:51