【问题标题】:Why I can't solve these equations using some specific parameters? (Sympy)为什么我不能使用某些特定参数来求解这些方程? (同情)
【发布时间】:2021-03-12 06:51:00
【问题描述】:

这是我尝试使用 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])

我的问题是基于最后一个参数t。对于当前值,该函数应该是可解的,但它不返回任何结果。如果我将 t 的值更改为其他值,那么我可以得到结果。例如,如果t=99,则结果如下所示

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

为什么我无法从第一个 t 值得到结果?我该如何解决这个问题?谢谢!!

【问题讨论】:

  • 在您的示例中ny 是什么?
  • 最好贴一段完整的代码来演示问题
  • @Oscar Benjamin 抱歉,我的原始代码中有错字,刚刚修复
  • 您将beta 创建为symbol,然后替换为数字。
  • @hpaulj 感谢您的评论!在我当前的设置中,必须指定 beta 以使 delta 有意义。

标签: python sympy equation equation-solving


【解决方案1】:
 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⎠⎠⎠⎦

现在用特定的theta 值评估一些result 术语。

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⋅π

对于ttheta 的某些值,为什么result 会为空(无解),我看不出任何线索。但是使用这个代数解决方案应该更容易探索这些问题。

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

acos 这个词可能是问题所在:

acos(0.707106781186547⋅√2)

这大约是acos(1),但如果acos arg 完全大于1,则nan

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

所以acos 参数在代数上是 1,但在数值上可能会大一些,导致nan

【讨论】:

  • 非常感谢您的详细解答!这非常有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多