solve equation with abs using sympy

68 views Asked by At

I'm trying to solve the following equation using sympy:

enter image description here.

The code I am using to find the solution to the equation for Vr is the following:

import sympy as sp

Vr, Vs, gamma, l, Zc, P, Q = sp.symbols('Vr Vs gamma l Zc P Q', complex=true)

eqn = sp.Eq(abs(Vr)**2 - sp.conjugate(Vr) * Vs/sp.cosh(gamma * l) + Zc*(P - 1j*Q)*sp.tanh(gamma * l), 0)

S = sp.solve(eqn, Vr)

print(S)

When I run it I get the following error:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-1-4eabc418c1ea> in <cell line: 10>()
      8 
      9 # Resuelve la ecuación
---> 10 S = sp.solve(eqn, Vr)
     11 
     12 # Imprime la solución

/usr/local/lib/python3.10/dist-packages/sympy/solvers/solvers.py in solve(f, *symbols, **flags)
   1005         for e in fi.find(Abs):
   1006             if e.has(*symbols):
-> 1007                 raise NotImplementedError('solving %s when the argument '
   1008                     'is not real or imaginary.' % e)
   1009 

NotImplementedError: solving Abs(Vr) when the argument is not real or imaginary.

The problem seems to be related to the fact that the equation is complex and as I tried by removing the magnitude of the term |Vr|^2 the code solves the equation.

I want to know if there is another library that allows me to solve the equation or if I am implementing the code incorrectly.

1

There are 1 answers

1
Oscar Benjamin On

If you use V*conjugate(V) rather than abs(V) then you can get an answer from solve. It is much faster if you disable checking:

In [88]: eq = Eq(Vr*conjugate(Vr) - Vr*Vs/cosh(gamma*l) + Zc*(P - I*Q)*tanh(gamma*l), 0)

In [89]: s1, s2 = solve(eq, Vr, check=False)

In [90]: s1
Out[90]: 
             ____________________________________________________________
    γ⋅l     ╱         4⋅γ⋅l                  4⋅γ⋅l              2  2⋅γ⋅l 
Vs⋅ℯ    - ╲╱  - P⋅Zc⋅ℯ      + P⋅Zc + ⅈ⋅Q⋅Zc⋅ℯ      - ⅈ⋅Q⋅Zc + Vs ⋅ℯ      
─────────────────────────────────────────────────────────────────────────
                                2⋅γ⋅l                                    
                               ℯ      + 1                                

In [91]: s2
Out[91]: 
             ____________________________________________________________
    γ⋅l     ╱         4⋅γ⋅l                  4⋅γ⋅l              2  2⋅γ⋅l 
Vs⋅ℯ    + ╲╱  - P⋅Zc⋅ℯ      + P⋅Zc + ⅈ⋅Q⋅Zc⋅ℯ      - ⅈ⋅Q⋅Zc + Vs ⋅ℯ      
─────────────────────────────────────────────────────────────────────────
                                2⋅γ⋅l                                    
                               ℯ      + 1