wolfram-mathematica - 为什么 Mathematica 不对这个 RecurrenceTable 进行数值评估?

标签 wolfram-mathematica recurrence

我正在尝试制作 RecurrenceTable在 Mathematica 中使用条件,并且递归的东西工作正常,但它不会完全评估它。

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} ...
有没有办法做到这一点?谢谢!

最佳答案

通常,您应该使用 Piecewise 用于数学函数,并保留 If用于编程流程。

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

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

几个相关的点:
  • 最好不要对符号名称使用大写字母,因为它们可能与内置函数冲突。
  • 你可以考虑Divisible[n, 2]代替 Mod[n, 2] == 0如果你希望。
  • 关于wolfram-mathematica - 为什么 Mathematica 不对这个 RecurrenceTable 进行数值评估?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7763152/

    相关文章:

    algorithm - 重复 T(n) = T(n^(1/2)) + 1

    wolfram-mathematica - 我如何从 Mathematica 中椭圆图形内的列表图中收集数据点?

    wolfram-mathematica - 在 Mathematica 中,ListPlot 使用什么插值函数?

    Python 前缀 后缀 中缀,无括号

    wolfram-mathematica - 数学; ND求解;我可以使用 InterpolatingFunction 作为初始条件吗?

    matlab - 确定一个集合是否是另一个集合的子集的有效代码

    algorithm - 与以 n 表示的变量的递归关系

    algorithm - Q : Recurrence relation by Substitution

    algorithm - 理解适用于主定理的 lambda

    algorithm - 递归的复杂度 : T(n) = T(n-1) + T(n-2) + C