9

I have an equation which is related to the sun-synchronous resonance condition in orbital mechanics. I'm learning Python at the moment, so I attempted to solve it in SymPy using the following code:

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_))

The expression can be successfully rearranged for five of the variables, but when the variable i_ is used in the solve command (as above), an error is produced:

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

However, when the same expression is entered into Matlab and the solve command is called, it is rearranged correctly. I realise that the error mentions a non-implemented feature and that the two functions will no doubt differ, but it would still be nice to know if there's a more appropriate SymPy function that I can use. Any help would be greatly appreciated.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
nathanDonaldson
  • 353
  • 1
  • 9
  • What version of SymPy are you using? I get a solution in 0.7.2 (it's a bit complicated, but it is a solution). – asmeurer Nov 14 '12 at 05:43

1 Answers1

9
  1. Use the sympy version of Pi.
  2. Substitute cos(i_) by a new variable ci_, replace sin(i_)**2 by 1-ci_**2, and solve for ci_.

This should do it:

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_))

(Edited because I only wrote half of the solution in the first attempt...)

silvado
  • 17,202
  • 2
  • 30
  • 46
  • I tried this and unfortunately it didn't work - the same error was generated. I even resorted to using `from sympy import *` just to be sure. Thanks again. – nathanDonaldson Nov 13 '12 at 13:05
  • Superlative, sir! Many thanks. I'm curious though: is SymPy always this sensitive to trig functions, or is it something about the equation itself that caused the original error? – nathanDonaldson Nov 13 '12 at 13:25
  • @user1820611, not sure about this, but easy trigonometric equations like `solve(sin(x)-0.5)` work without problems. – silvado Nov 13 '12 at 13:35
  • 7
    Support for solving trigonometric expressions is limited in SymPy right now, unfortunately. – asmeurer Nov 14 '12 at 05:44