Search code examples
pythondifferenceequation

How to solve Difference equations in python symbolically


I solved the equation y_n+1 = -5*y_n - 1 like that:

import numpy as np
import matplotlib.pyplot as plt
N = 10
index_set = range(N+1)
x = zeros(len(index_set))
x[0] = 7/4
for n in index_set[1:]:
    x[n] = -5*x[n-1] 
plt.plot(index_set, x)

But How can I get a symbolic solution for this?

The solution is: 2*(5)^t - 1/4


Solution

  • I guess, this can be handled in this way:

    from sympy import Function, rsolve
    from sympy.abc import n
    x = Function("x");
    f = x(n+1) - 5*x(n) - 1 ;
    sol = rsolve(f, x(n), {x(0):7/4});
    sol