ho won la
ho won la

Reputation: 21

why the sympy.solve does not return any solution?

I'm trying to solve below equation using sympy.solve

import sympy as sp

theta = sp.symbols('theta')
x = 0

eq = sp.cos(theta)**2 - sp.sin(theta)**2 - sp.sin(theta)*sp.sqrt(sp.sin(theta)**2 + x) + sp.sin(theta)*sp.cos(theta)**2 / sp.sqrt(sp.sin(theta)**2 + x)

soln = sp.solve(eq, theta)

print(soln)

Actually, if x = 0, eq will be cos^2(theta) - sin^2(theta) and the solutions are pi/4, 3pi/4, ... . But, above code does not return any number, only [] return. If x is not 0, this code does work. Why this code return [] when x = 0?

Upvotes: 2

Views: 837

Answers (1)

ELinda
ELinda

Reputation: 2821

Not sure exactly why solve does not work in this case, but solveset seems to work better. The docs suggest that there is an assumption of the domain being made, which defaults to Complex numbers when solveset is used.

import sympy as sp

theta = sp.Symbol('theta', real=False)
x = 0.0

eq = sp.cos(theta)**2 - sp.sin(theta)**2 - sp.sin(theta)*sp.sqrt(sp.sin(theta)**2 + x) + sp.sin(theta)*sp.cos(theta)**2 / sp.sqrt(sp.sin(theta)**2 + x)

soln = sp.solveset(eq, theta)

Resulting soln:

$\displaystyle \left{2 n \pi + \frac{5 \pi}{4}; |; n \in \mathbb{Z}\right} \cup \left{2 n \pi + \frac{3 \pi}{4}; |; n \in \mathbb{Z}\right} \cup \left{2 n \pi + \frac{7 \pi}{4}; |; n \in \mathbb{Z}\right} \cup \left{2 n \pi + \frac{\pi}{4}; |; n \in \mathbb{Z}\right}$

enter image description here

Upvotes: 1

Related Questions