user20844327
user20844327

Reputation: 3

Solving equations with sympy

everyone!

I'm trying to find the solutions for this equation using sympy.

import numpy as np 
import sympy as sp
from sympy import *
mu = 0
sigma = 1
n = 5

v_mu, v_sigma, v_n = mu, sigma, n
mu, sigma, x, s, n = symbols("mu sigma x s n")
eq =n*log((s**2+(x-mu)**2)/sigma**2)+n-(n*s**2+n*(x-mu)**2)/sigma**2 +2
resp = solve(eq, s)
s1, s2 = solve(eq, s)

s1 = s1.subs({mu: v_mu, sigma: v_sigma, n: v_n})
s2 = s2.subs({mu: v_mu, sigma: v_sigma, n: v_n})
resp, s1.evalf(),s2.evalf()

However, although I'm able to find those 2, solving with Wolfram|Alpha results in 4 as you can see in the following image. I know that sympy has some limitations, but is there another way to get those 4 solutions? What am I missing?

Upvotes: 0

Views: 101

Answers (1)

smichr
smichr

Reputation: 19047

Do your replacements/subs first, then replace s**2 with y - x**2 and solve for y, subtract x**2 and take +/-sqrt of each result to give 4 answers

>>> neq = eq.subs({mu: v_mu, sigma: v_sigma, n: v_n})
>>> [si*sqrt(i-x**2) for i in solve(neq.subs(s**2, y - x**2),y) for si in (-1,1)]
[-sqrt(-x**2 - LambertW(-exp(-7/5))), 
sqrt(-x**2 - LambertW(-exp(-7/5))), 
-sqrt(-x**2 - LambertW(-exp(-7/5), -1)), 
sqrt(-x**2 - LambertW(-exp(-7/5), -1))]

Upvotes: 1

Related Questions