title: Solving RC circuit with python/sympy
body: <p>I'm working independently out the the book Computational Physics by Mark Newman, exercise 6.5 to be specific (it can be downloaded <a href="http://www-personal.umich.edu/~mejn/cp/exercises.html" rel="nofollow">here</a>). The problem is to solve for node voltages in an RC circuit driven by a complex exponential source voltage.</p>
  
  <p>What I would like to do is turn my node equations into the form <strong>Av</strong> = <strong>B</strong>. Here is what I'm doing so far.</p>
  
  <pre><code>import numpy as np
  import sympy
  
  # Define constants
  R1=R3=R5 = 1e3
  R2=R4=R6 = 2e3
  C1 = 1e-6
  C2 = 0.5e-6
  x_plus = 3
  x_minus = 0
  w = 1000
  
  # Define relations
  V_plus = sympy.symbols('V_plus')
  t = sympy.symbols('t')
  
  V_plus = x_plus*sympy.exp(1j*w*t)
  V_minus = 0
  
  V1,V2,V3 = sympy.symbols('V1 V2 V3')
  
  V1_eqn = (V1-V_plus)/R1 + (V1-V2)/(1/(1j*w*C1)) + (V1-V_minus)/R4
  V2_eqn = (V2-V_plus)/R2 + (V2-V1)/(1/(1j*w*C1)) + (V2-V_minus)/R5 + (V2-V3)/(1/(1j*w*C2))
  V3_eqn = (V3-V_plus)/R3 + (V3-V2)/(1/(1j*w*C2)) + (V3-V_minus)/R6
  
  # Prepare
  V = [V1,V2,V3]
  V_vec = np.matrix(V)
  
  A = np.matrix([
      [V1_eqn.expand().coeff(x) for x in V],
      [V2_eqn.expand().coeff(x) for x in V],
      [V3_eqn.expand().coeff(x) for x in V]
      ])
  print(A)
  </code></pre>
  
  <p>Now, the problem I'm having is extracting the constant terms of the three equations so I can load them into <strong>B</strong>. Among other things, one of the issues is that the "constant term" isn't actually constant. It's a complex exponential function of 't'. Since this is a fairly straightforward problem with pen and paper, I know exactly what the constant term is going to look like, so I can extract it. My goal though is to solve this in a way that doesn't require knowing the answer ;).</p>
  
  <p>One method I've considered is taking each equation and subtracting off all the terms that I've already put in <strong>Av</strong>, but this seems a little sloppy and not very pythonic. Does anyone know of a better method for extracting constant terms that aren't technically constant? Thanks.</p>