Reputation: 13
I want to solve a system of equations symbolically such as A = ax + by
and B = cx + dy
, for x
and y
explicitly on sympy.
I tried the solve function of sympy as
solve([A, B], [x, y])
, but isn't working. It's returning an empty list, []
.
How can I solve it using sympy?
This is the actual equation I'm trying to solve:
from sympy import*
i,j,phi, p, e_phi, e_rho = symbols(r'\hat{i} \hat{j} \phi \rho e_\phi e_\rho')
e_rho = cos(phi)*i + sin(phi)*j
e_phi = -p*sin(phi)*i + p*cos(phi)*j
solve([e_rho,e_phi], [i,j])
Upvotes: 0
Views: 207
Reputation: 14530
I don't know what version of SymPy you're using but I just tried with the latest version and I get an answer:
In [4]: from sympy import*
...: i,j,phi, p, e_phi, e_rho = symbols(r'i j phi rho e_phi e_rho')
...: e_rho = cos(phi)*i + sin(phi)*j
...: e_phi = -p*sin(phi)*i + p*cos(phi)*j
...: solve([e_rho,e_phi], [i,j])
Out[4]: {i: 0, j: 0}
That's the correct answer to your equations (provided rho
is nonzero):
In [5]: e_rho
Out[5]: i⋅cos(φ) + j⋅sin(φ)
In [6]: e_phi
Out[6]: -i⋅ρ⋅sin(φ) + j⋅ρ⋅cos(φ)
If you meant to solve for e_rho
and e_phi
to be equal to something other than zero then you should include a right hand side either by subtracting it from the expressions or by using Eq
:
In [2]: A, B = symbols('A, B')
In [3]: solve([Eq(e_rho, A), Eq(e_phi, B)], [i, j])
Out[3]:
⎧ A⋅ρ⋅cos(φ) B⋅sin(φ) A⋅ρ⋅sin(φ) B⋅cos(φ) ⎫
⎪i: ───────────────────── - ─────────────────────, j: ───────────────────── + ─────────────────────⎪
⎨ 2 2 2 2 2 2 2 2 ⎬
⎪ ρ⋅sin (φ) + ρ⋅cos (φ) ρ⋅sin (φ) + ρ⋅cos (φ) ρ⋅sin (φ) + ρ⋅cos (φ) ρ⋅sin (φ) + ρ⋅cos (φ)⎪
⎩ ⎭
In [4]: solve([Eq(e_rho, A), Eq(e_phi, B)], [i, j], simplify=True)
Out[4]:
⎧ B⋅sin(φ) B⋅cos(φ)⎫
⎨i: A⋅cos(φ) - ────────, j: A⋅sin(φ) + ────────⎬
⎩ ρ ρ ⎭
Again that's the correct answer (assuming rho != 0
).
Upvotes: 1