为什么Mathematica不会在数字上评估此RecurrenceTable?

时间:2011-10-14 04:50:25

标签: wolfram-mathematica recurrence

我正在尝试在Mathematica中创建一个带有条件的RecurrenceTable,并且递归的东西正常工作,但它不会完全评估它。

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]}}

这些是正确的结果,但我需要它是数字形式,即{{0.25, 0.}, {0.25, 0.5625} ...

有办法做到这一点吗?谢谢!

2 个答案:

答案 0 :(得分:8)

通常,您应该使用Piecewise来表示数学函数,并为编程流保留If

您可以使用PiecewiseExpand转换多个If语句:

If[Mod[n, 2] == 0, x[n - 1], y[n - 1]] // PiecewiseExpand
If[Mod[n, 2] == 0, r*x[n - 1] (1 - x[n - 1]), y[n - 1]] // PiecewiseExpand

最终代码可能如下所示:

r = 3;
x0 = 0.25;
RecurrenceTable[
 {x[n] == Piecewise[{{x[n - 1], Mod[n, 2] == 0}}, y[n - 1]],
  y[n] == Piecewise[{{r*x[n - 1] (1 - x[n - 1]), Mod[n, 2] == 0}}, y[n - 1]],
  x[1] == x0,
  y[1] == 0},
 {x, y},
 {n, 10}
]
{{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}}

几个相关要点:

  1. 最好不要在符号名称中使用大写字母,因为这些可能会与内置函数冲突。

  2. 如果您愿意,可以考虑Divisible[n, 2]代替Mod[n, 2] == 0

答案 1 :(得分:3)

RecurrenceTable[{
 x[n] == Boole[ Mod[n,2] == 0 ] x[n-1] +
         Boole[ Mod[n,2] != 0 ] y[n-1],
 y[n] == Boole[ Mod[n,2] == 0 ] 3 x[n-1] (1-x[n-1]) + 
         Boole[ Mod[n,2] != 0 ] y[n-1],
 x[1] == .25, y[1] == 0},
 {x, y}, {n, 1, 10}]
编辑R = 3x0 = .25

可提供您期望的输出。