Simulating circuits with SPICE
About this lesson
SPICE solves circuits from a text description called a netlist. Follow one RC circuit, a 10 V source, R1 = R2 = 1 kΩ and C1 = 1 µF, from netlist to answer. See how SPICE numbers the nodes, stamps each part into a modified-nodal-analysis matrix, finds the DC operating point (v(2) = 5 V, I(V1) = −5 mA), and then steps through time by turning the capacitor into a resistor and a current source at every step. It builds on Nodal analysis, Solving circuit equations with matrices and The RC circuit: natural and step response, and on the Hardware lessons on Ohm's law and on resistors and capacitors.
A circuit as text
SPICE is a program that solves circuits. You describe the circuit as a netlist: plain text, one line per part. Each line gives the part's name, the two nodes it joins and its value. The first line is only a title.
the netlist (input deck) 1 RC divider charging 2 V1 1 0 DC 10 3 R1 1 2 1k 4 R2 2 0 1k 5 C1 2 0 1u IC=0 6 .op 7 .tran 0.1m 3m UIC 8 .end line 1 is always the title: SPICE skips it. Lines starting with . are commands. SPICE: Simulation Program with Integrated Circuit Emphasis, UC Berkeley, 1973.
Read the lesson step by step
A circuit as text
SPICE is a program that solves circuits. You describe the circuit as a netlist: plain text, one line per part. Each line gives the part's name, the two nodes it joins and its value. The first line is only a title.
the netlist (input deck) 1 RC divider charging 2 V1 1 0 DC 10 3 R1 1 2 1k 4 R2 2 0 1k 5 C1 2 0 1u IC=0 6 .op 7 .tran 0.1m 3m UIC 8 .end line 1 is always the title: SPICE skips it. Lines starting with . are commands. SPICE: Simulation Program with Integrated Circuit Emphasis, UC Berkeley, 1973.Number the nodes
A node is a point where parts join. SPICE needs every node named, and the ground node must be called 0. Here node 1 is the source's top, node 2 joins R1, R2 and C1, and node 0 is ground.
node 0 ground, the 0 V reference node 1 V1 (+) and R1 node 2 R1, R2 and C1 rules (ngspice manual §2.1.3–2.1.4) the ground node must be named 0 every node needs a DC path to ground every node needs at least two connectionsRead one line
The line R1 1 2 1k says: a resistor called R1 joins node 1 to node 2 and has 1 kΩ. Letters scale numbers: k is 10³, u is 10⁻⁶, m is 10⁻³. Careful: in SPICE m means milli; mega is meg.
R1 1 2 1k │ │ │ └ value: 1k = 1 × 10³ Ω │ │ └ second node │ └ first node └ name: the first letter is the type R resistor, C capacitor, V voltage source, L inductor scale letters meg 10⁶ k 10³ m 10⁻³ u 10⁻⁶ n 10⁻⁹ p 10⁻¹²The source and its sign
V1 1 0 DC 10 puts a 10 V source with + on node 1 and − on node 0. SPICE counts source current flowing into the + node, through the source, out at −. A source that delivers power shows a negative current.
V1 1 0 DC 10 + − value SPICE's reference for I(V1): into + (node 1), through V1, out of − (node 0) That is the passive sign convention: p = v × i is the power V1 ABSORBS. A source that supplies power has I(V1) < 0 and p < 0.Each part stamps the matrix
SPICE writes Kirchhoff's current law at every node, using modified nodal analysis. Each resistor adds its conductance, G = 1/R = 1 mS, into fixed cells. The source adds a row saying v(1) = 10 V. Watch the cells fill.
unknowns x = [ v1 v2 I(V1) ] R1 (1–2, G = 1 mS): +G at (1,1) and (2,2) −G at (1,2) and (2,1) R2 (2–0, G = 1 mS): +G at (2,2) only (node 0 has no row) V1 (1–0): 1 at (1,3) and (3,1), 10 in b [ 1m −1m 1 ] [v1] [ 0] [−1m 2m 0 ] [v2] = [ 0] [ 1 0 0 ] [iV] [10]The operating point (.op)
At DC a capacitor carries no current, so .op treats C1 as open. Solving the matrix gives v(1) = 10 V, v(2) = 5 V and I(V1) = −5 mA. The minus sign means V1 delivers 50 mW; R1 and R2 take 25 mW each.
.op C1 open (no DC current) v(1) = 10 V v(2) = 5 V I(V1) = −5 mA hand check: divider 10 × 1k/(1k + 1k) = 5 V power absorbed (passive sign convention) V1 10 × (−5 mA) = −50 mW R1 25 mW R2 25 mW sum −50 + 25 + 25 = 0Transient analysis (.tran)
.tran 0.1m 3m UIC asks for the voltages from 0 to 3 ms. UIC means start from the IC values in the netlist, so C1 starts empty at 0 V, not at the .op answer. Now the capacitor's current matters.
.tran tstep tstop UIC tstep 0.1 ms print step tstop 3 ms max step (ngspice default) min(tstep, tstop/50) = 0.06 ms UIC use initial conditions: skip the .op, start from IC=0 C1 law: i = C · dv/dt A derivative: the answer at each moment depends on the moment before.A capacitor becomes a resistor and a source
SPICE cannot solve a derivative directly. Over one time step h it uses i = C·(v_now − v_before)/h. That is a conductance GC = C/h = 10 mS in parallel with a current source GC·v_before: parts a DC solver already handles.
backward Euler, step h = 0.1 ms i = C (v_now − v_before) / h = (C/h)·v_now − (C/h)·v_before └ conductance └ current source GC = C/h = 1 µF / 0.1 ms = 10 mS Ieq = GC · v_before (into node 2) This "companion model" turns every time step into a DC problem.One time step is one DC solve
First step, h = 0.1 ms. C1 was at 0 V, so Ieq = 0 mA. Node 2 sees 1 + 1 + 10 = 12 mS. KCL gives v(2) = 10 mA ÷ 12 mS = 0.833 V, the first plot dot.
KCL at node 2, step 1 (t = 0.1 ms) (v2 − 10)·1m + v2·1m + v2·10m − Ieq = 0 Ieq = GC × 0 V = 0 12m · v2 = 10m v2 = 0.833 V matrix: cell (2,2) grows by GC to 12 mS, b gets Ieq in row 2. Same matrix shape as the .op; only two numbers changed.March to 3 ms
Repeat: each answer becomes the next step's v_before. v(2) climbs 0.833, 1.53, 2.11 V and levels off at 5 V, the .op answer. Hand check: τ = (R1 || R2)·C1 = 0.5 ms, so v(2) = 5(1 − e^(−t/τ)).
step t (ms) Ieq (mA) v(2) (V) 1 0.1 0 0.833 2 0.2 8.33 1.528 3 0.3 15.28 2.106 4 0.4 21.06 2.589 5 0.5 25.89 2.991 ... 30 3 ... 4.98 exact: τ = 500 Ω × 1 µF = 0.5 ms v(2) = 5(1 − e^(−t/τ))Smaller steps, smaller error
At t = τ the exact answer is 3.16 V. Steps of 0.1 ms give 2.99 V; steps of 10 µs give 3.14 V, almost on the exact curve. Real SPICE uses a better rule (trapezoidal) and picks h from an error estimate.
v(2) at t = τ = 0.5 ms exact 3.161 V h = 0.1 ms 2.991 V error 0.17 V h = 10 µs 3.142 V error 0.018 V backward Euler: error ∝ h (first order) ngspice default: trapezoidal (second order), step chosen from a truncation-error estimate, never above tmax.
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In the lesson's circuit, SPICE's .op prints I(V1) for the 10 V source. What value does it print?
5 mA flows out of V1's + terminal into R1. SPICE counts source current as flowing into the + node, through the source, out of the − node, so it prints −5 mA. The negative sign means V1 delivers power: 10 V × (−5 mA) = −50 mW absorbed.
02The same transient is run with a time step of 50 µs instead of 0.1 ms, still with backward Euler and C1 starting at 0 V. What is v(2) after the first step?
GC = C/h = 1 µF / 50 µs = 20 mS, and Ieq = 0. KCL at node 2: (1 + 1 + 20) mS × v2 = 10 mA, so v2 = 0.455 V. The exact value at that moment is 0.476 V.
03A student types R5 3 0 2.2M, meaning a 2.2 MΩ resistor. What does SPICE use?
In SPICE both M and m mean milli (10⁻³), so 2.2M is 0.0022 Ω. Mega is written meg: R5 3 0 2.2meg.
04In the lesson the .tran line ends in UIC and C1 has IC=0. If UIC is removed, where does v(2) start at t = 0?
Without UIC, SPICE first solves the DC operating point, with C1 open, and starts the transient from it. Node 2 is already at 5 V and nothing changes. UIC tells it to skip that solve and start from the IC values.
05With 0.1 ms steps, v(2) at t = τ comes out 0.17 V below the exact value. Roughly what error do steps of 10 µs give?
Backward Euler is a first-order method: its error shrinks in proportion to h. Ten times smaller steps give an error about ten times smaller, 0.018 V (3.14 V instead of 3.16 V).
Glossary
- SPICE
- Simulation Program with Integrated Circuit Emphasis: a circuit simulator first written at UC Berkeley in the early 1970s. Many simulators, such as ngspice, still follow its netlist format and methods.
- Netlist
- The text description of a circuit: one line per element giving its name, the nodes it joins and its value, plus command lines that start with a dot.
- Node 0
- The ground node. SPICE measures every node voltage from it, and every node must have a DC path to it.
- Modified nodal analysis (MNA)
- Nodal analysis extended with the currents of voltage sources as extra unknowns. SPICE solves the resulting matrix equation A·x = b.
- Stamp
- The fixed pattern of matrix cells an element adds to. A conductance G between nodes i and j adds +G at (i,i) and (j,j) and −G at (i,j) and (j,i).
- Operating point (.op)
- The DC solution of a circuit, found with capacitors open and inductors shorted.
- Transient analysis (.tran)
- A solution that steps through time, giving every node voltage as a waveform.
- Companion model
- What a capacitor or inductor becomes over one time step: a conductance in parallel with a current source, found from an integration rule such as backward Euler.
Notes and sources
- The figure is an ideal circuit: a 10 V source, two 1 kΩ resistors and a 1 µF capacitor. Every voltage, current, matrix entry and plot point is computed by the lesson's own small solver, the same modified nodal analysis SPICE uses, not by running a real SPICE.
- The figure steps time with backward Euler and a fixed 0.1 ms step because it is the easiest companion model to see. ngspice uses the trapezoidal rule by default (or Gear), and adjusts the step from a truncation-error estimate, so its waveform lies much closer to the exact curve.
- SPICE reports a voltage source's current as flowing into its + node. That is the passive sign convention, so a battery or supply that delivers power always shows a negative current. It is the most common surprise when reading a first SPICE output.
- ngspice is a free, open-source SPICE. Graphical simulators work the same way underneath: the schematic you draw is turned into a netlist like this one before it is solved.
- Wikipedia, SPICE
- L. W. Nagel, SPICE2: A Computer Program to Simulate Semiconductor Circuits, UC Berkeley ERL Memo M520 (1975)
- ngspice User's Manual (ground node 0, topological rules, element lines, .tran, .ic and uic, integration options)
- ngspice documentation index
- Wikipedia, Modified nodal analysis
- Wikipedia, Backward Euler method
- Alexander & Sadiku, Fundamentals of Electric Circuits (7th ed.)
- MIT OpenCourseWare 6.002 Circuits and Electronics, lecture notes (lecture 12: Capacitors and first-order systems)
Quiz Unlocks after the last step.