如何计算递归函数的显式形式?

发布于 2024-11-02 18:18:55 字数 488 浏览 1 评论 0原文

我有这个递归函数:

f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
f(1) = 2
f(2) = 8

我从经验中知道它的明确形式是:

f(n) = 3 ^ n - 1  // pow(3, n) - 1

我想知道是否有任何方法可以证明这一点。我用谷歌搜索了一下,但没有找到任何简单易懂的东西。我已经知道生成函数可能可以解决这个问题,它们太复杂了,我不想深入研究它们。我正在寻找一种更简单的方法。

聚苯乙烯 如果它有帮助,我记得这样的事情解决了它:

f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
// consider f(n) = x ^ n
x ^ n = 2 * x ^ (n-1) + 3 * x ^ (n-2) + 4

然后你以某种方式计算了 x ,导致递归公式的显式形式,但我不太记得

I have this recursive function:

f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
f(1) = 2
f(2) = 8

I know from experience that explicit form of it would be:

f(n) = 3 ^ n - 1  // pow(3, n) - 1

I wanna know if there's any way to prove that. I googled a bit, yet didn't find anything simple to understand. I already know that generation functions probably solve it, they're too complex, I'd rather not get into them. I'm looking for a simpler way.

P.S.
If it helps I remember something like this solved it:

f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
// consider f(n) = x ^ n
x ^ n = 2 * x ^ (n-1) + 3 * x ^ (n-2) + 4

And then you somehow computed x that lead to explicit form of the recursive formula, yet I can't quite remember

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

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

发布评论

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

评论(3

暖伴 2024-11-09 18:18:56

好吧,我知道你不想要生成函数(从现在开始)和所有复杂的东西,但我的问题结果是非线性的,简单的线性方法似乎不起作用。经过一整天的搜索,我找到了答案,希望这些发现对其他人有帮助。

我的问题: a[n+1]= a[n]/(1+a[n]) (即不是线性的(也不是多项式),但也不是完全非线性的 - 它是一个有理差分方程)

  1. 如果你的递归是线性的(或多项式),wikihow 有分步说明(有或没有 GF)
  2. 如果你想读点什么关于 GF,请访问 这个 wiki,但直到我开始做示例时我才明白(见下)
  3. 斐波那契GF使用示例
  4. 如果前面的示例没有意义,请下载GF 书 并阅读最简单的 GF 示例(第 1.1 节,即 a[n+1 ]= 2 a[n]+1,然后 1.2,a[n+1]= 2 a[n]+1,然后 1.3 - 斐波那契)
  5. (当我在讨论书本主题时) templatetypedef提到了具体数学,下载这里,但我对此了解不多,除了它有递归、求和和 GF 章节(其中其他)和第 335 页上的简单 GF 表
  6. ,当我更深入地研究非线性内容时,我看到 这个页面,使用它我在 z 变换方法上失败了,并且没有尝试线性代数,但是到有理差分方程的链接是最好的(参见下一步)
  7. 因此,根据此页面,有理函数很好,因为您可以将它们转换为多项式并使用线性方法上面的步骤 1.3. 和 4. 是我手写的,可能犯了一些错误,因为(参见 8)
  8. Mathematica (甚至是免费的 WolframAlpha) 有一个递归求解器,其中 RSolve[{a[n + 1] == a[n]/(1 + a[n]), a[1] == A}, a[n], n] 给了我一个简单的 {{a[n] -> A/(1 - A + A n)}}。所以我想我会回去寻找手工计算中的错误(它们有助于理解整个转换过程的工作原理)。

无论如何,希望这会有所帮助。

Ok, I know you didn't want generating functions (GF from now on) and all the complicated stuff, but my problem turned out to be nonlinear and simple linear methods didn't seem to work. So after a full day of searching, I found the answer and hopefully these findings will be of help to others.

My problem: a[n+1]= a[n]/(1+a[n]) (i.e. not linear (nor polynomial), but also not completely nonlinear - it is a rational difference equation)

  1. if your recurrence is linear (or polynomial), wikihow has step-by-step instructions (with and without GF)
  2. if you want to read something about GF, go to this wiki, but I didn't get it till I started doing examples (see next)
  3. GF usage example on Fibonacci
  4. if the previous example didn't make sense, download GF book and read the simplest GF example (section 1.1, ie a[n+1]= 2 a[n]+1, then 1.2, a[n+1]= 2 a[n]+1, then 1.3 - Fibonacci)
  5. (while I'm on the book topic) templatetypedef mentioned Concrete Mathematics, download here, but I don't know much about it except it has a recurrence, sums, and GF chapter (among others) and a table of simple GFs on page 335
  6. as I dove deeper for nonlinear stuff, I saw this page, using which I failed at z-transforms approach and didn't try linear algebra, but the link to rational difference eqn was the best (see next step)
  7. so as per this page, rational functions are nice because you can transform them into polynomials and use linear methods of step 1. 3. and 4. above, which I wrote out by hand and probably made some mistake, because (see 8)
  8. Mathematica (or even the free WolframAlpha) has a recurrence solver, which with RSolve[{a[n + 1] == a[n]/(1 + a[n]), a[1] == A}, a[n], n] got me a simple {{a[n] -> A/(1 - A + A n)}}. So I guess I'll go back and look for mistake in hand-calculations (they are good for understanding how the whole conversion process works).

Anyways, hope this helps.

佞臣 2024-11-09 18:18:56

一般来说,没有一种算法可以将递归形式转换为迭代形式。这个问题是无法判定的。作为一个例子,考虑这个递归函数定义,它定义了 Collat​​z 序列:

f(1) = 0
f(2n) = 1 + f(n)
f(2n + 1) = 1 + f(6n + 4)

甚至不知道这是否是一个定义良好的函数。如果存在一种可以将其转换为封闭形式的算法,我们就可以决定它是否定义良好。

然而,对于许多常见情况,可以将递归定义转换为迭代定义。优秀的教科书《具体数学》用了很多篇幅来展示如何做到这一点。当您猜测答案是什么时,一种非常有效的常用技术是使用归纳法。作为您的案例的示例,假设您相信您的递归定义确实给出了 3^n - 1。为了证明这一点,请尝试证明它对于基本情况成立,然后证明这些知识可以让您向上推广解决方案。您没有在帖子中添加基本案例,但我假设

f(0) = 0
f(1) = 2

鉴于此,让我们看看您的预感是否正确。对于 0 和 1 的特定输入,您可以通过检查来验证该函数是否计算了 3^n - 1。对于归纳步​​骤,我们假设对于所有 n' < n f(n) = 3^n - 1。那么我们就得到了

f(n) = 2f(n - 1) + 3f(n - 2) + 4
     = 2 * (3^{n-1} - 1) + 3 * (3^{n-2} - 1) + 4
     = 2 * 3^{n-1} - 2 + 3^{n-1} - 3 + 4
     = 3 * 3^{n-1} - 5 + 4
     = 3^n - 1

所以我们刚刚证明了这个递归函数确实产生了 3^n - 1。

In general, there is no algorithm for converting a recursive form into an iterative one. This problem is undecidable. As an example, consider this recursive function definition, which defines the Collatz sequence:

f(1) = 0
f(2n) = 1 + f(n)
f(2n + 1) = 1 + f(6n + 4)

It's not known whether or not this is even a well-defined function or not. Were an algorithm to exist that could convert this into a closed-form, we could decide whether or not it was well-defined.

However, for many common cases, it is possible to convert a recursive definition into an iterative one. The excellent textbook Concrete Mathematics spends much of its pages showing how to do this. One common technique that works quite well when you have a guess of what the answer is is to use induction. As an example for your case, suppose that you believe that your recursive definition does indeed give 3^n - 1. To prove this, try proving that it holds true for the base cases, then show that this knowledge lets you generalize the solution upward. You didn't put a base case in your post, but I'm assuming that

f(0) = 0
f(1) = 2

Given this, let's see whether your hunch is correct. For the specific inputs of 0 and 1, you can verify by inspection that the function does compute 3^n - 1. For the inductive step, let's assume that for all n' < n that f(n) = 3^n - 1. Then we have that

f(n) = 2f(n - 1) + 3f(n - 2) + 4
     = 2 * (3^{n-1} - 1) + 3 * (3^{n-2} - 1) + 4
     = 2 * 3^{n-1} - 2 + 3^{n-1} - 3 + 4
     = 3 * 3^{n-1} - 5 + 4
     = 3^n - 1

So we have just proven that this recursive function does indeed produce 3^n - 1.

留蓝 2024-11-09 18:18:55
f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
f(n+1) = 2 * f(n) + 3 * f(n-1) + 4

f(n+1)-f(n) = 2 * f(n) - 2 * f(n-1) + 3 * f(n-1) - 3 * f(n-2)
f(n+1) = 3 * f(n) + f(n-1) - 3 * f(n-2)

现在4已经没了。
正如您所说,下一步是让 f(n) = x ^ n

x^(n+1) = 3 * x^n + x^(n-1) - 3 * x^(n-2)

除以 x^(n-2)

x^3 = 3 * x^2 + x - 3
x^3 - 3 * x^2 - x + 3 = 0

因式分解来找到 x

(x-3)(x-1)(x+1) = 0
x = -1 or 1 or 3

f(n) = A * (-1)^n + B * 1^n + C * 3^n
f(n) = A * (-1)^n + B + C * 3^n

现在使用您求解 A、B 和 C 的值找到

f(1) = 2; f(2) = 8; f(3) = 26

f(1) = 2 = -A + B + 3C
f(2) = 8 = A + B + 9C
f(3) = 26 = -A + B + 27C

A、B 和 C:

f(3)-f(1) = 24 = 24C      => C = 1
f(2)-f(1) = 6 = 2A + 6    => A = 0
2 = B + 3                 => B = -1

最后

f(n) = 3^n - 1
f(n) = 2 * f(n-1) + 3 * f(n-2) + 4
f(n+1) = 2 * f(n) + 3 * f(n-1) + 4

f(n+1)-f(n) = 2 * f(n) - 2 * f(n-1) + 3 * f(n-1) - 3 * f(n-2)
f(n+1) = 3 * f(n) + f(n-1) - 3 * f(n-2)

Now the 4 is gone.
As you said the next step is letting f(n) = x ^ n

x^(n+1) = 3 * x^n + x^(n-1) - 3 * x^(n-2)

divide by x^(n-2)

x^3 = 3 * x^2 + x - 3
x^3 - 3 * x^2 - x + 3 = 0

factorise to find x

(x-3)(x-1)(x+1) = 0
x = -1 or 1 or 3

f(n) = A * (-1)^n + B * 1^n + C * 3^n
f(n) = A * (-1)^n + B + C * 3^n

Now find A,B and C using the values you have

f(1) = 2; f(2) = 8; f(3) = 26

f(1) = 2 = -A + B + 3C
f(2) = 8 = A + B + 9C
f(3) = 26 = -A + B + 27C

solving for A,B and C:

f(3)-f(1) = 24 = 24C      => C = 1
f(2)-f(1) = 6 = 2A + 6    => A = 0
2 = B + 3                 => B = -1

Finally

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