Reputation: 3
So, i got this piece of code for my electric circuits problem (coding on spyder
):
from sympy.solvers import solve
from sympy import Symbol, simplify
vo = Symbol("vo") #amplitude of output voltage
vr = Symbol("vr") #amplitude of voltage r
ir = Symbol("ir") #amplitude of R-current
ic = Symbol("ic") #amplitude of C-current
il = Symbol("il") #amplitude of L-current
r = Symbol("r") #resistance
omega = Symbol("omega") #angular frequency
c = Symbol("c") #capacitance
l = Symbol("l") #inductance
eq1 = (vr + vo - 1,
ir - ic - il,
vr - ir*r,
vo - ic/(1j*omega*c),
vo - 1j*omega*l*il)
sol = solve(eq1, (vo, vr, ir, ic, il))
vos = simplify(sol[vo])
print(vos)
print(sol[vo])
that has an output of:
l*omega/(I*c*l*omega**2*r + l*omega - I*r)
l*omega/(I*c*l*omega**2*r + l*omega - I*r)
which is a Vo
solution of my current circuit
Then I changed some parts of eq1
since im going to solve the Vo
again of another circuit which goes like:
eq1 = (ir*(r + 1/ic + il) + vo - 1,
ir - ic - il - vr/r,
vr - ir*r,
vo - ic/(1j*omega*c),
vo - 1j*omega*l*il)
eq1
is the only part that ive changed, but when I run it, this error shows up:
TypeError: list indices must be integers or slices, not Symbol
anyone knows how to fix this issue?
Upvotes: 0
Views: 1044
Reputation: 231540
I added a print(sol)
line. With the original eq1
, sol
is a dictionary, and vo
is a valid key:
0936:~/mypy$ python3 stack52932207.py
{vo: l*omega/(I*c*l*omega**2*r + l*omega - I*r), vr: I*r*(c*l*omega**2 - 1.0)/(I*c*l*omega**2*r + l*omega - I*r), ir: I*(c*l*omega**2 - 1.0)/(I*c*l*omega**2*r + l*omega - I*r), ic: I*c*l*omega**2/(I*c*l*omega**2*r + l*omega - I*r), il: -I/(I*c*l*omega**2*r + l*omega - I*r)}
l*omega/(I*c*l*omega**2*r + l*omega - I*r)
l*omega/(I*c*l*omega**2*r + l*omega - I*r)
With the newer eq1
, sol
is a an empty list, so indexing with vo
is no longer valid.
0937:~/mypy$ python3 stack52932207.py
[]
Traceback (most recent call last):
File "stack52932207.py", line 28, in <module>
vos = simplify(sol[vo])
TypeError: list indices must be integers or slices, not Symbol
I'm not that familiar with sympy
and solve
, but my guess is that the new eq1
no longer has a solution. In any case, that's what you need to focus on - why does solve
return an empty list?
And please, when asking about an error, include the traceback. We need to know where the error occurs, not just what is says. Once you have an idea of where the error occurs you can add diagnostic prints. In this case, both location and error message were relevant.
Upvotes: 2
Reputation: 619
From what I can see, the only place where you're trying to index a list is when you call:
sol[vo]
vo
is an object of type Symbol, but lists require you to use integers (or slices), as the error suggests. Eg:
sol[5]
sol[1:3]
So you need to convert vo
to an integer before using it to index a list.
Upvotes: 1