Solving relapse with sympy

I tried to solve the recurrence relation of a Fibonacci series using sympy. I received an answer different from the text of the textbook. I don’t know where I made a mistake.

My simplex code

from sympy import *
f=Function('f')
var('y')
var('n',integer=True)
f=y(n)-y(n-1)+(n-2)
rsolve(f,y(n))

And conclusion

C0 + (-n + 1) * (n / 2 - 1)

+4
source share
1 answer

Here is the complete code for solving Fibonacci recursion. Please pay attention to the correct use Functionand symbols.

from sympy import *
y = Function('y')
n = symbols('n',integer=True)
f = y(n)-y(n-1)-y(n-2)
rsolve(f,y(n),{y(0):0, y(1):1})

sqrt(5)*(1/2 + sqrt(5)/2)**n/5 - sqrt(5)*(-sqrt(5)/2 + 1/2)**n/5

+5
source

Source: https://habr.com/ru/post/1653468/


All Articles