I need to write a script that solves for a variable inside a complex numbers-trigonometric equation, how do i solve it?
So far I tried using the Eq function from sympy to input the equation from which I need to isolate x, and then using the solve/solveset functions, however this is not working. I have already isolated by hand through algebra, but i wanted to know if it's possible in anyway to calculate its value without having to input the isolated equation for the variable.
import cmath
from sympy import *
x = symbols('x')
Zl = complex(20, -10)
Z0 = 75
pl = abs((Zl - Z0)/(Zl + Z0))
SWR = (1 + pl)/(1 - pl)
eq = Eq(SWR, (Zl + 1j*(Z0*SWR*cmath.tan(2*cmath.pi*x)))/(Z0 + 1j*(Zl*SWR*cmath.tan(2*cmath.pi*x))))
m = solve(eq)
print(m)
I expect the output to be (-0.22734187163019368+7.138384643986063e-18j)
, which is what i got by solving it by hand. (m = cmath.atan(1j*(Z0*SWR-Zl)/(Zl*SWR - Z0))/(2*cmath.pi)
)
I'm getting the following errors:
Traceback (most recent call last):
File "C:/Users/juank/PycharmProjects/TXScripts/solveM_givenSWR.py", line 16, in <module>
eq = Eq(SWR, (Zl + 1j*(Z0*SWR*cmath.tan(2*cmath.pi*x)))/(Z0 + 1j*(Zl*SWR*cmath.tan(2*cmath.pi*x))))
File "C:\Users\juank\PycharmProjects\TXScripts\venv\lib\site-packages\sympy\core\expr.py", line 285, in __complex__
return complex(float(re), float(im))
File "C:\Users\juank\PycharmProjects\TXScripts\venv\lib\site-packages\sympy\core\expr.py", line 280, in __float__
raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float
Try not using cmath
with sympy
. Replace all cmath
functions with their sympy
equivalent. sympy
has its own trigonometric functions. The cmath
versions are expecting a float.
I prefer to explicitly import so I know which library I'm using such as:
from sympy import tan as sy_tan, symbols, complex
(the as
is optional)
Namespace issues with commonly named functions will always create issues.