# Correctly defining the return of a procedure

I am sort of new to mathematica and I am struggling with how to correctly set the return of a specific function which is a procedure.

I am give the following task: Given the map $$x_{n+1} = r x_n (1-x_n)$$:

1- set $$x_0 = 0.5$$

2- write a function $$f(r)$$ which evaluates the first 1000 terms fo the sequence, takes the last 100 and then selects distinct elements.

This is what I came up with so far

x = 0.5
f[r_]:={
l = Table[0,1000]; (*init table of 1000 elems*)
l[] = x; (*set x_0*)
For[n=1,n<1000,n++,
{
x[n] = r * x[n-1] *(1-x[n-1]); (*evaluate x_n*)
l[[n+1]] = x[n];               (*set nth elem of list*)
}
];
l= Union[Take[l, -100]]         (*modify list*)
Return[l]                       (*return list*)
}


but this does not work at all. thanks tho everyone who is keen to partecipate and give some suggestion :)

• Curly braces are for lists, not for code blocks. Use Module, Block, or With for code blocks. And try not to use For. – Roman Apr 8 '19 at 10:45
• Why should I avoid the For loop in Mathematica? – corey979 Apr 8 '19 at 11:34
• Return doesn't do what you think it does. Don't use it until you understand it. Once you understand it, you'll probably never use it. – John Doty Apr 8 '19 at 13:17
• You might also look into Nest and NestList as an alternative, built-in way to iterate this map. – Chris K Apr 8 '19 at 13:20
• Don't just guess at the syntax, read a tutorial. Plenty can be found with a simple websearch. – Szabolcs Apr 12 '19 at 9:24

Try this:

f[r_] := Union[
Take[
RecurrenceTable[
{a[n + 1] == r*(1 - a[n])*a[n], a == .5},
a, {n, 1, 1000}
],
-100]
]

• Or, a little more efficiently, f[r_] := Union[ RecurrenceTable[{a[n + 1] == r*(1 - a[n])*a[n], a == 0.5}, a, {n, 901, 1000}]] – Bob Hanlon Apr 8 '19 at 13:50

For a specific value of $$r$$ you can do

With[{r = 3.7},
NestList[r*#*(1-#) &, 0.5, 1000][[-100 ;;]]]


{0.783499, 0.627626, 0.864733, 0.432788, 0.908285, 0.308222, 0.788918, 0.616148, 0.875086, 0.404449, 0.891219, 0.358706, 0.851134, 0.468809, 0.9214, 0.26796, 0.725783, 0.736382, 0.718257, 0.748746, 0.696065, 0.782767, 0.629159, 0.863277, 0.436711, 0.910179, 0.302485, 0.780655, 0.63356, 0.858998, 0.448145, 0.915051, 0.287611, 0.758096, 0.67853, 0.80707, 0.576119, 0.903562, 0.32241, 0.808309, 0.573299, 0.905121, 0.317745, 0.802098, 0.587326, 0.896784, 0.34248, 0.833193, 0.514234, 0.92425, 0.259043, 0.710177, 0.761555, 0.67188, 0.815692, 0.556253, 0.913292, 0.293003, 0.766463, 0.662291, 0.827548, 0.528036, 0.922092, 0.265802, 0.722061, 0.74255, 0.707328, 0.765957, 0.663288, 0.826347, 0.530942, 0.921458, 0.267782, 0.725477, 0.736893, 0.717362, 0.750189, 0.6934, 0.786607, 0.621069, 0.870766, 0.41637, 0.899122, 0.335595, 0.824992, 0.534206, 0.920671, 0.270233, 0.729667, 0.729837, 0.729548, 0.730039, 0.729203, 0.730623, 0.728207, 0.732309, 0.72532, 0.737154, 0.716905, 0.750923}

I don't think there are any duplicates in this list ($$r$$ is in the chaotic region). For other values of $$r$$ there are indeed duplicates:

With[{r = 3.5},
NestList[r*#*(1 - #) &, 0.5, 1000][[-100 ;;]]] // DeleteDuplicates // Sort


{0.38282, 0.500884, 0.826941, 0.874997}

// Union does the same thing as // DeleteDuplicates // Sort if you prefer.