ZR-
ZR-

Reputation: 819

Why I can't solve these equations using some specific parameters? (Sympy)

Here's the code with equations I'm trying to solve using sympy:

# Import sympy
from math import remainder, tau
from sympy import *
from sympy.solvers import solve

# Define the parameters
nx, ny, nz = [0,0,1]
a,b,c = [0,0,0]
d = 1
t = 9

# Solve the equations
theta = remainder(2*t*sqrt(b**2+c**2+d**2), tau)
delta, beta, gamma = symbols('delta beta gamma')
beta = 7 # Randomly assigned. 
eq1 = Eq(tan((delta+beta)/2),nz*tan(theta/2))
eq2 = Eq(ny*tan((delta-beta)/2),nx)
eq3 = Eq(cos((delta+beta)/2)*cos(gamma/2),cos(theta/2))
result = solve([eq1, eq2, eq3], [delta, beta, gamma])

My question is based on the last parameter t. For the current value, the function should be solvable but it doesn't return any results. If I change the value of t to some other values, then I can get the result. For instance, if t=99, then the results look like

[(-12.0619298297468, 9.00000000000000, 7.46580816699663e-8*I), 
(-12.0619298297468, 9.00000000000000, 12.5663706143592 - 7.46580045560101e-8*I)]

Why I'm unable to get the result from the first t value? How can I fix the issue? Thanks!!

Upvotes: 0

Views: 209

Answers (1)

hpaulj
hpaulj

Reputation: 231510

 In [59]: 
    ...: delta, gamma = symbols('delta gamma')
    ...: theta = symbols('theta')
    ...: beta = 9
    ...: eq1 = Eq(tan((delta+beta)/2),nz*tan(theta/2))
    ...: #eq2 = Eq(ny*tan((delta-beta)/2),nx)
    ...: eq3 = Eq(cos((delta+beta)/2)*cos(gamma/2),cos(theta/2))
In [60]: eq1
Out[60]: 
   ⎛δ   9⎞      ⎛θ⎞
tan⎜─ + ─⎟ = tan⎜─⎟
   ⎝2   2⎠      ⎝2⎠
In [61]: eq3
Out[61]: 
   ⎛γ⎞    ⎛δ   9⎞      ⎛θ⎞
cos⎜─⎟⋅cos⎜─ + ─⎟ = cos⎜─⎟
   ⎝2⎠    ⎝2   2⎠      ⎝2⎠
In [62]: result = solve([eq1, eq3], [delta, gamma])
In [63]: result
Out[63]: 
⎡⎛                            ⎛       ____________       ⎞      ⎞  ⎛                          ⎛       
⎢⎜      ⎛   ⎛θ⎞⎞              ⎜      ╱     1          ⎛θ⎞⎟      ⎟  ⎜      ⎛   ⎛θ⎞⎞            ⎜      ╱
⎢⎜2⋅atan⎜tan⎜─⎟⎟ - 9, - 2⋅acos⎜√2⋅  ╱  ────────── ⋅cos⎜─⎟⎟ + 4⋅π⎟, ⎜2⋅atan⎜tan⎜─⎟⎟ - 9, 2⋅acos⎜√2⋅  ╱ 
⎣⎝      ⎝   ⎝2⎠⎠              ⎝   ╲╱   cos(θ) + 1     ⎝2⎠⎠      ⎠  ⎝      ⎝   ⎝2⎠⎠            ⎝   ╲╱  

____________       ⎞⎞⎤
     1          ⎛θ⎞⎟⎟⎥
 ────────── ⋅cos⎜─⎟⎟⎟⎥
 cos(θ) + 1     ⎝2⎠⎠⎠⎦

Now evaluate some of the result terms with specific theta values.

In [64]: result[0][0].subs({theta:-0.849})
Out[64]: -9.84900000000000
In [65]: result[0][1].subs({theta:-0.849})
Out[65]: -2⋅acos(0.707106781186547⋅√2) + 4⋅π

I don't see any clues as to why result would be empty (no solution) for certain values of t, and hence theta. But it should be easier to explore those issues with this algebraic solution.

In [66]: t=9
In [67]: remainder(2*t*sqrt(b**2+c**2+d**2), tau)
Out[67]: -0.8495559215387587

That acos term might be the problem:

acos(0.707106781186547⋅√2)

That's approximately acos(1), but nan if the acos arg is at all larger than 1.

With numpy:

In [299]: def foo(theta):
     ...:     return np.sqrt(2)*np.sqrt(1/(np.cos(theta)+1))*np.cos(theta/2)
     ...: 
In [300]: foo(0)
Out[300]: 1.0000000000000002
In [301]: foo(-1)
Out[301]: 1.0000000000000002
In [302]: foo(-.89)
Out[302]: 1.0000000000000002
In [303]: foo(-3.0)
Out[303]: 0.999999999999998

So the acos argument is algebraically 1, but numerically may be a bit larger, resulting in a nan.

Upvotes: 1

Related Questions