为什么 Mathematica 不对这个 RecurrenceTable 进行数值计算?

发布于 2024-12-09 22:19:46 字数 647 浏览 1 评论 0原文

我正在尝试在 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} ...

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2024-12-16 22:19:46

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

您可以使用 PiecewiseExpandIf 语句>

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

Typically, you should use Piecewise for mathematical functions, and reserve If for programming flow.

You can convert many If statements using 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

The final code may look something like this:

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

A couple of related points:

  1. It is best not to use capital letters for your symbol names, as these may conflict with built-in functions.

  2. You may consider Divisible[n, 2] in place of Mod[n, 2] == 0 if you wish.

羞稚 2024-12-16 22:19:46
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 给出您期望的输出。

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

with edits R = 3 and x0 = .25 gives the output you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文