I’m trying to make a RecurrenceTable with conditionals in Mathematica, and the recursive stuff is working right, but it won’t evaluate it completely.
In:= RecurrenceTable[{x[n] == If[Mod[n, 2] == 0, x[n - 1], y[n - 1]],
y[n] == If[Mod[n, 2] == 0, R x[n - 1] (1 - x[n - 1]), y[n - 1]],
x[1] == x0, y[1] == 0}, {x, y}, {n, 1, 10}]
Out:= {{0.25, 0.}, {x[1], 3 (1 - x[1]) x[1]}, {y[2], y[2]}, {x[3],
3 (1 - x[3]) x[3]}, {y[4], y[4]}, {x[5], 3 (1 - x[5]) x[5]}, {y[6],
y[6]}, {x[7], 3 (1 - x[7]) x[7]}, {y[8], y[8]}, {x[9],
3 (1 - x[9]) x[9]}}
These are the right results, but I need it to be in numeric form, i.e. {{0.25, 0.}, {0.25, 0.5625} ...
Is there a way to do this? Thanks!
Typically, you should use
Piecewisefor mathematical functions, and reserveIffor programming flow.You can convert many
Ifstatements usingPiecewiseExpand:The final code may look something like this:
{{0.25, 0.}, {0.25, 0.5625}, {0.5625, 0.5625}, {0.5625, 0.738281}, {0.738281, 0.738281}, {0.738281, 0.579666}, {0.579666, 0.579666}, {0.579666, 0.73096}, {0.73096, 0.73096}, {0.73096, 0.589973}}A couple of related points:
It is best not to use capital letters for your symbol names, as these may conflict with built-in functions.
You may consider
Divisible[n, 2]in place ofMod[n, 2] == 0if you wish.