Reputation: 115
I have the following recurrence relation:
an = 0.996 · an–1 + 0.004 · an–10 + 0.04
with initial conditions: an = 0 for all n ≤ 9.
Code:
import sympy as sp
n = sp.symbols('n')
y = sp.Function('y')
recurrence = sp.rsolve(
y(n) - sp.Rational(0.996) * y(n-1) - sp.Rational(0.004) * y(n-10) - sp.Rational(0.04),
y(n),
{
y(0): 0,
y(1): 0,
y(2): 0,
y(3): 0,
y(4): 0,
y(5): 0,
y(6): 0,
y(7): 0,
y(8): 0,
y(9): 0,
}
)
The wrong result I get is just a constant number. Why do I not get an expression that depends on the variable n
?
Upvotes: 0
Views: 66
Reputation: 1122
The recurrence
first argument of rsolve
is not an equality, it is the expression that the nth term is equal to, see docs.
So rsolve
call should be
sp.rsolve(
sp.Rational('0.996') * y(n-1) - sp.Rational('0.004') * y(n-10) -
sp.Rational('0.04'),
y(n), [0,]*10)
Seems like sympy cannot solve this recursion (note that you probably meant Rational('0.996')
and not Rational(0.996)
).
For getting a general element in this recurssion you can do the following:
from sympy import Function, symbols, Rational
from sympy.series.sequences import RecursiveSeq
y = Function('y')
n = symbols('n')
seq = RecursiveSeq(Rational('0.996')*y(n - 1) - Rational('0.004') * y(n-10) - Rational('0.04'), y(n), n, [0,]*10)
# y(30) =
seq.coeff(30)
Upvotes: 3