开发者

Recursive function definition in Mathematica

Mathematica can solve recursive equations using RSolve. Is it possible to have a funct开发者_运维知识库ion defined by a recurrence, regardless whether the recurrence can or cannot be solved analytically?


Yes. Look at RecurrenceTable. One also can program to define a function by its recurrence equation, factorial being the simplest example.

In[94]:= fac[1] = 1;
fac[k_Integer?Positive] := k*fac[k - 1]

In[96]:= fac[10]

Out[96]= 3628800

In[97]:= Function[If[#1 == 1, 1, #1*#0[#1 - 1]]][10]

Out[97]= 3628800

In[100]:= RecurrenceTable[
 f[k] == k f[k - 1] && f[1] == 1, f, {k, 1, 10}]

Out[100]= {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}


I wondered for a moment what RecurrenceTable is good for, until I rewrote Sasha's example using NestList:

Rest@NestList[{1, 0} + First@# {1, Last@#} &, {1, 1}, 10][[All, -1]]

{1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}

If the involvement of k (First@#) is complicated, RecurrenceTable could be far simpler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜