Home > front end >  symbolic solution of linear equations using Sympy
symbolic solution of linear equations using Sympy

Time:02-11

Basically I have [5x5][5x1]=[0] and would like to have the symbolic expression of the solution.

Here is my code.

from sympy import symbols, solve
gm1, gm2, gm4                   = symbols(['gm1', 'gm2', 'gm4'])
gds1, gds2, gds3, gds4, gds5    = symbols(['gds1', 'gds2', 'gds3', 'gds4', 'gds5'])
s                               = symbols(['s'])
Cd, CF , Cin, Ct                = symbols(['Cd', 'CF', 'Cin', 'Ct'])
K                               = symbols(['K'])
vb, vc, ve, vout, iin           = symbols(['vb', 'vc', 've', 'vout', 'iin'])

sol = solve([-(gds1 gds3 (s*Cd))*vb   (gm1 gds1)*ve   -gm1*vout, \
          -gm4*vb   (gds4-gds2-(s*Cin)-(s*CF))*vc   (gds2 gm2)*ve   s*CF*vout   iin, \
        gds1*vb   gds2*vc   (-(s*Ct)-gds5-gds1-gm1-gm2-gds2)*ve   gm1*vout, \
        K*vc   vout], [vout])
print(sol)

but, I got this error

TypeError: can't multiply sequence by non-int of type 'Symbol'

From here, symbolic multiplication seems working just fine.

I am not sure whether I describe my problem in a way that does not comply with Sympy or something else.

What did I miss here?

CodePudding user response:

The problem is in the assignment of the single symbols s and K. If instead you do:

s, K = symbols(['s', 'K'])

Or:

s = symbols('s')
K = symbols('K')

Whether you get the right answer or not is another matter :)

CodePudding user response:

When you pass a list to symbols you get a list back. You can unpack that like [s] = symbols(['s']) or you can just pass a string of space or comma separated strings like x, y = symbols('x y') or x, y = symbols(','.join(['x', 'y']).

If you select manual=True you will get a solution vout=K*vc which sets the 4th equation to 0. But that was almost obvious, right? And you didn't need the other 3 equations to tell you that. So go ahead and pick up to 3 other variables for which you want to solve. There are lots of possibilities:

>>> from sympy.functions.combinatorial.numbers import nC
>>> allsym = Tuple(*eqs).free_symbols
>>> nfree = len(allsym) - 1  # always take vout
>>> print(nC(nfree, 3))  # want 3 others
816

For example, selecting (vout, gds4, gm1, gds5) gives a solution of

[{gds4: (CF*K*s*vc   CF*s*vc   Cin*s*vc   gds2*vc -
         gds2*ve - gm2*ve   gm4*vb - iin)/vc, 
  gm1: (Cd*s*vb   gds1*vb - gds1*ve   gds3*vb)/(K*vc   ve), 
  gds5: -(Cd*s*vb   Ct*s*ve - gds2*vc   gds2*ve   gds3*vb   gm2*ve)/ve, 
  vout: -K*vc}]
  • Related