SymPy无法解决Matlab可以解决的等式

时间:2012-11-13 12:09:09

标签: python sympy

我有一个与轨道力学中的太阳同步共振条件有关的方程式。我目前正在学习Python,所以我尝试使用以下代码在SymPy中解决它:

from sympy import symbols,solve

[n_,Re_,p_,i_,J2_,Pe_] = symbols(['n_','Re_','p_','i_','J2_','Pe_'])

del_ss = -((3*n_*(Re_**2)*J2_/(4*(p_**2)))*(4-5*(sin(i_)**2)))-((3*n_*(Re_**2)*J2_/(2*(p_**2)))*cos(i_))-((2*pi)/Pe_)

pprint(solve(del_ss,i_))

可以为五个变量成功重新排列表达式,但是当在i_命令中使用变量solve时(如上所述),会产生错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 479, in runfile
    execfile(filename, namespace)
  File "C:\Users\Nathan\Python\sympy_test_1.py", line 22, in <module>
    pprint(solve(del_ss,i_))
  File "C:\Python27\lib\site-packages\sympy\solvers\solvers.py", line 484, in solve
    solution = _solve(f, *symbols, **flags)
  File "C:\Python27\lib\site-packages\sympy\solvers\solvers.py", line 700, in _solve
    soln = tsolve(f_num, symbol)
  File "C:\Python27\lib\site-packages\sympy\solvers\solvers.py", line 1143, in tsolve
    "(tsolve: at least one Function expected at this point")
NotImplementedError: Unable to solve the equation(tsolve: at least one Function expected at this point

但是,当在Matlab中输入相同的表达式并调用solve命令时,它会被正确重新排列。我意识到错误提到了一个未实现的功能,并且这两个函数无疑会有所不同,但是知道我是否可以使用更合适的SymPy函数仍然会很好。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:9)

  1. 使用Pi的同情版本。
  2. 用新变量cos(i_)替换ci_,将sin(i_)**2替换为1-ci_**2,然后求解ci_
  3. 这应该这样做:

    from sympy import symbols,solve,sin,cos,pi
    
    [n_,Re_,p_,ci_,J2_,Pe_] = symbols(['n_','Re_','p_','ci_','J2_','Pe_'])
    
    del_ss = -((3*n_*(Re_**2)*J2_/(4*(p_**2)))*(4-5*(1-ci_**2)))-((3*n_*(Re_**2)*J2_/(2*(p_**2)))*ci_)-((2*pi)/Pe_)
    
    pprint(solve(del_ss,ci_))
    

    (编辑,因为我在第一次尝试中只写了一半的解决方案......)