使用 sympy 求解带有 abs 的方程

问题描述 投票:0回答:1

我正在尝试使用 sympy 求解以下方程:

enter image description here

我用来求 Vr 方程解的代码如下:

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)

当我运行它时,出现以下错误:

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.

这个问题似乎与方程很复杂这一事实有关,当我尝试通过删除项 |Vr|^2 的大小时,代码解决了方程。

我想知道是否有另一个库可以让我解方程,或者我是否错误地实现了代码。

python sympy symbolic-math complex-numbers equation-solving
1个回答
0
投票

如果你使用

V*conjugate(V)
而不是
abs(V)
那么你可以从solve中得到答案。如果禁用检查,速度会快得多:

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 
© www.soinside.com 2019 - 2024. All rights reserved.