user2211319
user2211319

Reputation:

Solving an equation with Sympy symbols

Is the following code not solvable by Sympy? I've executed this code a couple of minutes ago, but it printed n = 5 on the screen and it stuck.

import sympy

Wmin = 31
m = 8

p = sympy.symbols('p')

for n in range(5, 10):
    print 'n = %3d' % n

    denominator = (1 + Wmin + p * Wmin * ((1 - (2 * p) ** m) / (1 - 2 * p)))
    right = 1 - (1 - 2 / denominator) ** (n - 1)

    p_solve = sympy.solve(sympy.Eq(p, right))

    print p_solve

Actually, I've solved the equation with bisection method in MATLAB and I'm currently modifying without bisection method and porting in Python.

Upvotes: 3

Views: 691

Answers (1)

smichr
smichr

Reputation: 19135

You could use nsolve to solve a problem like this -- but you need a guess as to where the solution(s) might be:

>>> for n in range(5, 10):
...     print 'n = %3d' % n
...     denominator = (1 + Wmin + p * Wmin * ((1 - (2 * p) ** m) / (1 - 2 * p)))

...     right = 1 - (1 - 2 / denominator) ** (n - 1)
...     p_solve = nsolve(sympy.Eq(p, right),p,0)
...     print p_solve
...
n =   5
0.181881594898608
n =   6
0.210681675044646
n =   7
0.235278433487669
n =   8
0.256480923202426
n =   9
0.27492972759045

Upvotes: 1

Related Questions