为什么 Mathematica 不对这个 RecurrenceTable 进行数值计算?
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您应该使用
Piecewise
来表示数学函数,并为编程流程保留If
。您可以使用
PiecewiseExpand
If 语句>:最终代码可能如下所示:
一些相关点:
最好不要使用大写字母作为符号名称,因为它们可能与内置函数冲突。
如果您愿意,您可以考虑用
Divisible[n, 2]
代替Mod[n, 2] == 0
。Typically, you should use
Piecewise
for mathematical functions, and reserveIf
for programming flow.You can convert many
If
statements usingPiecewiseExpand
:The final code may look something like this:
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] == 0
if you wish.编辑
R = 3
和x0 = .25
给出您期望的输出。with edits
R = 3
andx0 = .25
gives the output you expect.