Sub topic 12
Verifying the Result with a Simulator
A hand calculation by superposition can have a sign error, a source that was deactivated the wrong way, or a mistaken partial result, and nothing in the arithmetic will flag any of them. A circuit simulator gives you an independent answer to compare against. This chapter shows how to check the 12 V and 2 A example from the worked-example chapter. It also shows how to use the simulator to isolate each source's contribution, and how to read a mismatch.
This chapter assumes you have already solved that circuit by hand. It also assumes you can read a short text netlist (a plain-text description of a circuit) and, for the last section, run a short Python script. The examples use SPICE syntax, which ngspice and LTspice both accept.
The circuit as a netlist
The circuit has a 12 V source V1 in series with R1 = 4 Ω, feeding node A. R2 = 6 Ω connects node A to ground. A current source I1 injects 2 A into node A.
* Superposition check: V1 and I1 both active
V1 n1 0 DC 12
R1 n1 a 4
R2 a 0 6
I1 0 a DC 2
.op
.control
run
print v(a) v1#branch
.endc
.endEach line is one element:
- The first letter of the name gives the element type:
Vfor a voltage source,Rfor a resistor,Ifor a current source. - Node
0is always ground. V1 n1 0 DC 12puts the positive terminal atn1and the negative terminal at ground.I1 0 a DC 2is the line that most often causes sign errors. SPICE defines positive current as flowing from the first node, through the source, to the second node. This source therefore pushes 2 A out of ground, through itself, and into nodea. That is the "into node A" direction of the hand analysis..oprequests a DC operating point, which is a single solution of the circuit with all sources constant.
You can run this with ngspice -b circuit.cir. In LTspice, remove the .control block and read the result from the operating-point log. The simulator should report v(a) as about 1.2e+01, which is 12 V. That matches the hand result.
Reproducing each partial response
One matching total is weak evidence. Two errors can cancel and still give 12 V. The stronger check is to make the simulator reproduce each single-source case too. These are the same cases you solved by hand.
You can do this without drawing new circuits. In SPICE, a 0 V voltage source is a short circuit, and a 0 A current source is an open circuit. Setting a source's value to zero is therefore the same as deactivating it, as the source-deactivation chapter described.
| Run | V1 line | I1 line | Expected `v(a)` |
|---|---|---|---|
| Both active | `V1 n1 0 DC 12` | `I1 0 a DC 2` | 12 V |
| V1 only | `V1 n1 0 DC 12` | `I1 0 a DC 0` | 7.2 V |
| I1 only | `V1 n1 0 DC 0` | `I1 0 a DC 2` | 4.8 V |
The partial results should satisfy 7.2 V + 4.8 V = 12 V. The simulator is doing a full nodal solution in every run. Your hand calculation used only a divider in one case and a parallel combination in the other. When the two routes agree, you can trust both.
Zeroing the value is better than deleting or commenting out the line. If you comment out V1, node n1 is left connected only to R1. Most simulators then stop with a "singular matrix" or "floating node" error. If you comment out I1, nothing is left floating, so that case works. Using zero-valued sources for both cases is simpler and avoids the problem.
Reading more than the node voltage
The simulator also reports the current through V1, which the hand method did not ask for. SPICE reports a source's branch current as positive when it flows into the positive terminal. A source that delivers power therefore shows a negative current.
| Run | `v(a)` | `i(V1)` as SPICE reports it |
|---|---|---|
| Both active | 12 V | 0 A |
| V1 only | 7.2 V | -1.2 A |
| I1 only | 4.8 V | +1.2 A |
The currents superpose as well: -1.2 A + 1.2 A = 0 A. This has a physical reading. With both sources active, node A sits at exactly 12 V, which is the same as the source voltage. There is no voltage across R1, so R1 carries no current and V1 delivers nothing. The 2 A from I1 flows entirely through R2, since 12 V / 6 Ω = 2 A.
The partial runs hide this because each one carries 1.2 A through R1, in opposite directions. The V1-only run drives 1.2 A from V1 toward node A. The I1-only run sends 1.2 A from node A back toward the shorted V1. The zero total is the sum of two non-zero parts.
Running the same check in Python
You can also write a small solver and test superposition numerically. This script builds the nodal equations for the circuit with an extra row for V1's branch current, which is the same formulation SPICE uses. It needs NumPy (pip install numpy).
import numpy as np
R1, R2 = 4.0, 6.0
g1, g2 = 1 / R1, 1 / R2
def solve(v1, i1):
"""Return (V_A, branch current of V1) for given source values."""
# Unknowns: [V_n1, V_A, i_V1]; i_V1 flows into V1's + terminal (SPICE sign).
A = np.array([
[g1, -g1, 1.0], # KCL at n1
[-g1, g1 + g2, 0.0], # KCL at A, with I1 injected on the right
[1.0, 0.0, 0.0], # V_n1 = v1
])
b = np.array([0.0, i1, v1])
_, va, iv1 = np.linalg.solve(A, b)
return va, iv1
def clean(x):
return round(float(x), 9) + 0.0 # removes -0.0 and rounding noise
runs = {
"both": solve(12.0, 2.0),
"V1 only": solve(12.0, 0.0),
"I1 only": solve(0.0, 2.0),
}
for name, (va, iv) in runs.items():
print(f"{name:<9} Va = {clean(va):6.2f} V i(V1) = {clean(iv):6.2f} A")
sum_va = runs["V1 only"][0] + runs["I1 only"][0]
sum_iv = runs["V1 only"][1] + runs["I1 only"][1]
assert np.isclose(runs["both"][0], sum_va)
assert np.isclose(runs["both"][1], sum_iv)
print("superposition holds for V_A and i(V1)")The script prints:
both Va = 12.00 V i(V1) = 0.00 A
V1 only Va = 7.20 V i(V1) = -1.20 A
I1 only Va = 4.80 V i(V1) = 1.20 A
superposition holds for V_A and i(V1)The solver is linear in b, so superposition holds by construction. That is the same fact linearity gives you for a real linear circuit. The script confirms your hand numbers and shows where the property comes from.
The verification workflow
Compare the total first. A mismatch there means something is wrong with the circuit description or the whole hand solution. Compare the partial results only after the total matches, so you can tell which source's partial calculation was wrong.
Reading a mismatch
The size and form of the discrepancy usually point to the cause.
- Flipped current source. If you wrote
I1 a 0 DC 2instead ofI1 0 a DC 2, node A gets 7.2 V - 4.8 V = 2.4 V. A result equal to the difference of your two partial answers is a typical sign of this. - Reversed voltage source. Swapping the terminals of V1 gives -7.2 V + 4.8 V = -2.4 V.
- Wrong deactivation by hand. If you opened the voltage source instead of shorting it, the hand result for that case will not match the zero-valued simulation. The simulator shows the correct partial value, and you can work backward from it.
- Forgotten dependent source. Dependent sources must stay active in every partial run. If your circuit has one, do not zero it in the simulator either. Zero only the independent sources.
- Units and prefixes. In SPICE,
mmeans milli andmegmeans mega. Writing1Mgives 1 milliohm, not 1 megaohm. - Rounded comparison. The simulator prints about six significant digits. Compare with a tolerance, not exact equality. A tolerance like
np.isclosewith its defaults is fine for a linear DC circuit.
What a simulator check can and cannot confirm
A match confirms your answer for this circuit, not the general rule. Two situations matter.
Power. Ask the simulator for the power in R2. It reports 24 W, which is (12 V)² / 6 Ω. The wrong superposition of power from the two partial runs gives 7.2²/6 + 4.8²/6 = 12.48 W. The simulator exposes this error immediately, because its full-circuit power does not equal the sum of the partial-run powers.
Nonlinear parts. Replace R2 with a diode and run the same three cases. The sum of the partial voltages no longer matches the full-circuit result. The simulator computes each run correctly, so the mismatch is real. It shows that superposition was not valid for that circuit. For nonlinear circuits, the simulator is the reliable tool, and the check "do the partials add up?" becomes a test of linearity itself.
Sinusoidal and time-varying sources
The same idea extends to AC circuits. For a frequency-domain check, SPICE's .ac analysis applies only the sources that have an AC magnitude, so you can give one source AC 1 and the others AC 0 to see each source's phasor contribution. Add the complex results to get the total. If the sources run at different frequencies, you must add the results in the time domain, never as phasors.
For a time-domain check with .tran, run the circuit once per source and once with all of them, then add the partial traces point by point. This only works cleanly if every run uses the same time points. Set a fixed maximum time step so the runs line up, or interpolate onto a common grid before adding.
Exercise
You run your SPICE netlist with both sources and get v(a) = 2.4 V instead of 12 V. Your hand partials were 7.2 V and 4.8 V. What is the most likely error, and how do you fix it?
Show answer
The current source is probably connected in the wrong direction. Reversing I1 turns +4.8 V into -4.8 V, and 7.2 - 4.8 = 2.4 V. Fix it by writing the source as I1 0 a DC 2, so that positive current flows from ground, through the source, into node a.
Exercise
In the V1-only run, SPICE reports i(V1) = -1.2 A. Explain the sign, and explain why the full-circuit run reports 0 A even though neither partial run is 0 A.
Show answer
SPICE counts current as positive when it flows into the source's positive terminal, so a source that delivers current shows a negative value. In the V1-only run, V1 delivers 1.2 A through R1, which is -1.2 A. In the I1-only run, 1.2 A flows from node A back through R1 into V1's positive terminal, which is +1.2 A. The currents superpose, so the total is -1.2 + 1.2 = 0 A. This matches the full circuit, where node A sits at 12 V, there is no voltage across R1, and R1 carries no current.