Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SymPy cannot solve an equation that Matlab can

Tags:

python

sympy

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.

like image 792
nathanDonaldson Avatar asked Nov 13 '12 12:11

nathanDonaldson


1 Answers

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

like image 199
silvado Avatar answered Oct 22 '22 21:10

silvado