---
title: Circuit simulation with SPICE
heading: What a simulator computes, and when to trust it
eyebrow: C11 · Lesson 24 · Circuit simulation with SPICE
xp: 40
---

::: hook
Every circuit in this course's sims is solved the way SPICE solves circuits: nodal equations, Newton's method for the nonlinear parts, and adaptive time steps for the transient. A simulator checks a design before anything is built, but it answers exactly the question it's asked, with exactly the models it's given. An AC sweep is a linearization that will happily show a 100 V output from a 5 V amplifier; a transient run with too coarse a time step misses a glitch; a model with the wrong saturation current puts a transistor in the wrong region. Knowing what each analysis computes is how you tell a real result from an artifact.
:::

::: play
- An RC low-pass, analyzed two ways at once: the scope runs a **transient** analysis of a 1 kHz sine, and the Bode plot runs an **AC** analysis from 10 Hz to 1 MHz.
- Move R or C: the AC sweep's corner moves, and the transient output's size and lag change to match the AC answer at 1 kHz.
- The AC analysis is small-signal: it would give the same shape for a 1 mV or a 1000 V input. Only the transient analysis respects nonlinearity and limits.
:::
:::

```sim circuit
V1 in 0 sine 1 V 1k Hz   @ 0,0 down
R1 in out 1k             @ 0,0 right
C1 out 0 100n            @ 2,0 down
slider: R1 100..10k log
slider: C1 10n..1u log
scope: v(in), v(out)
time: 0..3m s
bode: v(out)/v(in) | f = 10..1M Hz
```

::: predict
## An AC analysis is run on an amplifier powered from ±5 V with a gain of 100 and a 1 V input. What output does it report?

- [ ] About 5 V, where the output clips
- [x] 100 V: the AC analysis is linearized around the operating point and knows nothing about clipping
- [ ] 0 V: the input is too large
- [ ] It refuses to run

::: reveal
100 V. AC analysis linearizes every device at the DC operating point and solves a linear circuit, so the output is just gain times input, however large. Treat AC results as gains and phases, and check large signals with a transient run.
:::
:::

::: nameit
## Kinds of analysis

| Analysis | Computes | Use for |
|---|---|---|
| operating point (.op) | DC node voltages and currents, nonlinear devices solved | bias checks; always run first |
| DC sweep (.dc) | the operating point as a source or parameter varies | transfer curves |
| AC (.ac) | small-signal gain and phase, linearized at the operating point | frequency response, stability |
| transient (.tran) | the full nonlinear response over time | waveforms, clipping, switching, startup |
| noise, Monte Carlo, worst case | noise spectra; spread over part tolerances | performance and yield |

## How it solves

Nonlinear circuits are solved by **Newton's method**: linearize every device at a guess, solve, repeat. Near the answer the error roughly squares each iteration, so convergence is fast once close. When it fails ("no convergence"), simulators fall back on gmin stepping (adding small conductances) and source stepping (ramping the supplies). Transient analysis advances in steps sized by an error estimate; to resolve a signal at f with N points per cycle, steps must be about $1/(Nf)$ or smaller.

## Trusting results

- Check the operating point before anything else: a transistor in the wrong region invalidates every other analysis.
- A model is only as good as its parameters: manufacturer models, measured if possible.
- Results that look too good (perfect matching, infinite bandwidth) usually mean a part was left ideal.
- **Monte Carlo** runs the circuit many times with parts drawn from their tolerance distributions; the fraction that meets the spec estimates the **yield**. With Gaussian spreads, a spec at ±kσ passes a fraction $\mathrm{erf}(k/\sqrt2)$: 95.4% at 2σ, 99.73% at 3σ.
:::

::: example
## Newton's method starts with an error of 0.1 V, and the error squares each iteration. How many iterations to reach 10⁻⁸ V?

::: solution
1. **Square the error each iteration.**
   $$0.1 \to 0.01 \to 10^{-4} \to 10^{-8}$$
   That's three iterations.
2. **Compare with halving.** That's quadratic convergence; a method that only halved the error would need 24.

**Answer:** three iterations.
:::
:::

::: example
## A Monte Carlo run shows an output with a Gaussian spread; the spec is the mean ± 3σ. What yield should production expect?

::: solution
1. **Yield within ±3σ.**
   $$\mathrm{erf}(3/\sqrt2) = 99.73\%$$
   or 2.7 failures per thousand.
2. **A tighter spec.** Tightening the spec to ±2σ would fail 4.6%.

**Answer:** a yield of 99.73%, or 2.7 failures per thousand.
:::
:::

::: example
## A circuit's output varies with a Gaussian spread. What yield should production expect if the specification is the mean ± 2σ, and ± 3σ?

::: solution
1. **Fraction within ±kσ.**
   $$\operatorname{erf}(k/\sqrt2)$$
2. **At 2σ and 3σ.**
   $$\begin{aligned} \operatorname{erf}(2/\sqrt2) &= 95.45\% \\ \operatorname{erf}(3/\sqrt2) &= 99.73\% \end{aligned}$$
3. **In parts per million.** Out of specification that's 45,500 and 2,700.
4. **Why aim higher.** A 3σ design still fails a few thousand units per million, which is why high-volume products aim for 4σ to 6σ.

**Answer:** 95.45% for ±2σ and 99.73% for ±3σ.
:::
:::

::: check
given: f = 1 | 50 | 1000; N = 20 | 100
answer: 1/(N*f*1e3)*1e6
unit: µs
tolerance: 0.5%

## A transient run must show a {f} kHz signal with {N} points per cycle. What is the largest time step?

::: right
Right. $1/(Nf)$ = {answer} µs.
:::

::: misses
- [1/(f*1e3)*1e6] That's a whole period; divide by {N}.
- [any] Not yet. Period over points per period.
:::
:::

::: check
given: k = 1 | 1.5 | 2 | 3
answer: 100*erf(k/sqrt(2))
unit: %
tolerance: 0.01%

## Monte Carlo shows a Gaussian output. What percentage of units fall within ±{k}σ of the mean?

::: right
Right. $\mathrm{erf}(k/\sqrt2)$ = {answer}%.
:::

::: misses
- [100*erf(k)] The argument is $k/\sqrt2$.
- [any] Not yet. $\mathrm{erf}(k/\sqrt2)$.
:::
:::

::: check
given: e0 = 0.1 | 0.01; t = 1e-12 | 1e-8
answer: ceil(log(log(t)/log(e0))/log(2) - 0.000001)
tolerance: 0

## Newton's method starts with error {e0}, and the error squares each iteration. How many iterations until it is at or below {t}?

::: right
Right. {answer} iterations: the number of correct digits doubles each time.
:::

::: misses
- [any] Not yet. List the errors: square each one until it's small enough.
:::
:::

::: check
given: R = 1 | 4.7 | 10; C = 10 | 100; f = 100 | 1000
answer: -10*log(1 + (2*pi*f*R*1e3*C*1e-9)^2)
unit: dB
tolerance: 0.5%

## An AC analysis of an RC low-pass (R = {R} kΩ, C = {C} nF) reports what gain at {f} Hz, in dB?

::: right
Right. {answer} dB.
:::

::: misses
- [any] Not yet. $-10\log_{10}(1 + (\omega RC)^2)$.
:::
:::

::: check
given: p = 10 | 20 | 50; d = 3 | 5 | 6
answer: p*d + 1
tolerance: 0

## An AC sweep uses {p} points per decade over {d} decades, including both ends. How many frequencies does it compute?

::: right
Right. {answer}.
:::

::: misses
- [p*d] Count the starting point too.
- [any] Not yet. Points per decade × decades, plus the first.
:::
:::

::: check
kind: choice

## A simulated amplifier shows a perfectly flat gain to 10 GHz. What's the most likely explanation?

- [ ] It really is that good
- [x] An op amp or transistor was left as an ideal model with no bandwidth limit or capacitances
- [ ] AC analysis always shows flat responses
- [ ] The time step was too small

::: misses
- [1] Real parts have finite bandwidth and capacitance.
- [3] AC analysis shows whatever the models' frequency dependence gives.
- [4] AC analysis has no time step.
:::
:::

::: check
kind: parts
given: e0 = 0.1 | 0.5; t = 1e-6 | 1e-9 | 1e-12

## Newton's method starts with error {e0} V, and each iteration squares the error (in volts).

::: part
answer: ceil(ln(ln(t)/ln(e0))/ln(2) - 0.000001)
tolerance: 0

### How many iterations bring the error below {t} V?

::: misses
- [any] After k steps the error is $e_0^{2^k}$.
:::
:::

::: part
answer: e0^2
unit: V
tolerance: 0.5%

### What is the error after one iteration?

::: misses
- [any] Squared.
:::
:::
:::

::: check
kind: parts
given: k = 1 | 2 | 3 | 4

## A design's output is Gaussian, and the specification is the mean ± {k}σ.

::: part
answer: erf(k/sqrt(2))*100
unit: %
tolerance: 0.01%

### What yield should production expect?

::: misses
- [any] $\operatorname{erf}(k/\sqrt2)$.
:::
:::

::: part
answer: (1 - erf(k/sqrt(2)))*1e6
unit: ppm
tolerance: 0.5%

### How many parts per million fail?

::: misses
- [any] One minus the yield.
:::
:::
:::

::: explain
## Why should a simulation's DC operating point be checked before trusting its AC analysis?

::: model
AC analysis linearizes every nonlinear device at the operating point: a transistor's transconductance, output resistance and capacitances all depend on its bias current and voltages. If the operating point is wrong, say a transistor saturated, cut off, or biased at the wrong current because of a bad resistor value or model, the small-signal parameters are wrong too, and every gain and corner frequency computed from them is meaningless, even though the plot looks perfectly reasonable. Checking the bias first confirms that the circuit being linearized is the one intended.
:::
:::

::: practice

::: check
given: f = 1 | 10 | 50 | 100 | 200 | 500; N = 20 | 50 | 100
answer: 1/(N*f*1e3)*1e6
unit: µs
tolerance: 0.5%

## Time step for {N} points per cycle at {f} kHz?

::: misses
- [any] $1/(Nf)$.
:::
:::

::: check
given: k = 0.5 | 1 | 1.5 | 2 | 2.5 | 3
answer: 100*erf(k/sqrt(2))
unit: %
tolerance: 0.01%

## Fraction within ±{k}σ?

::: misses
- [any] $\mathrm{erf}(k/\sqrt2)$.
:::
:::

::: check
given: p = 10 | 20 | 25 | 50 | 100; d = 2 | 3 | 4 | 6
answer: p*d + 1
tolerance: 0

## Points in a {d}-decade sweep at {p} per decade?

::: misses
- [any] p × d + 1.
:::
:::
:::
