mgilbert
mgilbert

Reputation: 3645

Sympy algebraic solution to series summation

I am trying to solve for C in the following equation

enter image description here

I can do this with sympy for an enumrated number of x's, e.g x0, x2, ..., x4 but cannot seem to figure out how to do this for i=0 to t. E.g. for a limited number

from sympy import summation, symbols, solve

x0, x1, x2, x3, x4, alpha, C = symbols('x0, x1, x2, x3, x4, alpha, C')

e1 = ((x0 + alpha * x1 + alpha**(2) * x2 + alpha**(3) * x3 + alpha**(4) * x4)
      / (1 + alpha + alpha**(2) + alpha**(3) + alpha**(4)))
e2 = (x3 + alpha * x4) / (1 + alpha)
rhs = (x0 + alpha * x1 + alpha**(2) * x2)  / (1 + alpha + alpha**(2))

soln_C = solve(e1 - C*e2 - rhs, C)

Any insight would be much appreciated.

Upvotes: 3

Views: 2586

Answers (2)

I'm not sure if this can be catalogued as more "concise", but it could be useful too, when you know the upper limit of the summatory. Let's suppose that we want to evaluate this expression:

We can express it, and solve it, in sympy as follows:

from sympy import init_session
init_session(use_latex=True)
n = 4
As = symbols('A_1:' + str(n+1))
x = symbols('x')
exp = 0
for i in range(n):
    exp += As[i]/(1+x)**(i+1)
Ec = Eq(exp,0)
sol = solve(Ec,x)
#print(sol)
#sol    #Or, if you're working on jupyter...

Upvotes: 0

mgilbert
mgilbert

Reputation: 3645

Thanks to @bryans for pointing me in the direction of Sum. Elaborating on his comment, here is one solution that seems to work. As I am fairly new to sympy if anyone has a more concise approach please share.

from sympy import summation, symbols, solve, Function, Sum

alpha, C, t, i = symbols('alpha, C, t, i')
x = Function('x')

s1 = Sum(alpha**i * x(t-i), (i, 0, t)) / Sum(alpha**i, (i, 0, t))
s2 = Sum(alpha**i * x(t-3-i), (i, 0, t-3)) / Sum(alpha**i, (i, 0, t-3)) 
rhs = (x(0) + alpha * x(1) + alpha**(2) * x(2))  / (1 + alpha + alpha**(2))

enter image description here

soln_C = solve(s1 - C*s2 - rhs, C)

enter image description here

Upvotes: 1

Related Questions