---
title: Diode circuit lab
heading: Solving diode circuits exactly with Newton's method
eyebrow: C5 · Lesson 14 · Diode circuit lab
xp: 50
---

::: hook
Every circuit simulator, including the one in these lessons, solves nonlinear circuits the same way: guess the node voltages, replace each diode by the straight line that touches its curve at the guess, solve that linear circuit, and repeat. That's Newton's method (A6), and near the answer it doubles the number of correct digits on every step. In this lab you'll write it for one diode, then for a network of two, and then use exact analysis to find out how good the rectifier ripple formula of lesson 7 really is.
:::

::: play
- A half-wave rectifier, solved by the simulator's Newton iteration at every time step.
- The simple estimate for its ripple is $V_pT/RC$. Read the ripple off the scope and compare: at large ripple the estimate is noticeably high.
- Raise C until the ripple is small: the estimate and the scope agree better and better.
:::
:::

```sim circuit
V1 in 0 sine 10 V 50 Hz     @ 0,0 down
D1 in out                   @ 0,0 right
C1 out 0 100u               @ 2,0 down
RL out 0 1k                 @ 4,0 down
slider: C1 22u..1m log
scope: v(in), v(out)
time: 0..100m s
```

::: predict
## Newton's method solves a diode-resistor circuit to 0.01 mV. Starting from 0.7 V, how many steps does it typically need?

- [ ] One
- [x] Three to six
- [ ] About 50
- [ ] Hundreds

::: reveal
Three to six. Each step replaces the exponential by its tangent at the current guess; near the answer the error is squared on every step, so 10 mV becomes about 1 mV, then 0.01 mV, then far less. Starting far above the answer is slower, because each step can only come down about one thermal voltage while the exponential is huge.
:::
:::

::: nameit
## Newton's method for one diode

Kirchhoff's current law at the diode's anode, with the supply $V_s$ through R:

$$f(V) = \frac{V_s - V}{R} - I_S\left(e^{V/nV_T} - 1\right) = 0$$

Newton's step is $V \leftarrow V - f(V)/f'(V)$ with

$$f'(V) = -\frac1R - \frac{I_S}{nV_T}e^{V/nV_T}$$

In circuit terms, $-f'(V)$ is the total conductance at the node: the resistor's $1/R$ plus the diode's small-signal conductance at the guess. Start at 0.7 V and stop when the step is below, say, $10^{-12}$ V.

## Several nodes

For node voltages $\mathbf v$, KCL at every node gives a vector $\mathbf f(\mathbf v) = 0$. Newton's step solves the linear system

$$J\,\Delta\mathbf v = -\mathbf f \qquad J_{ij} = \frac{\partial f_i}{\partial v_j}$$

and $J$ is exactly the nodal conductance matrix of the circuit with each diode replaced by its conductance at the guess. Simulators add limiting so that one step can't raise a diode's voltage by much more than a few thermal voltages.

## Exact rectifier ripple

With an ideal diode and input $V_p\cos\omega t$, the diode stops conducting just after the peak, at $\tan\omega t_\text{off} = \frac{1}{\omega RC}$, when the input starts falling faster than the capacitor can discharge. The capacitor then decays as $V_p\cos(\omega t_\text{off})e^{-(t - t_\text{off})/RC}$ until the next cycle's input rises to meet it at $t_1$, between $\frac34T$ and $T$, where

$$\cos\omega t_1 = \cos(\omega t_\text{off})\,e^{-(t_1 - t_\text{off})/RC}$$

Solving that by bisection gives the exact ripple, $V_p - V(t_1)$.
:::

::: example
## Do one Newton step by hand for 5 V, 1 kΩ, $I_S = 10^{-14}$ A, starting from 0.7 V.

::: solution
1. **Diode current at 0.7 V.**
   $$10^{-14}e^{0.7/0.025852}\ \text{A} = 5.75\ \text{mA}$$
2. **The function to zero.**
   $$\begin{aligned} f &= 4.30 - 5.75 \\ &= -1.45\ \text{mA} \end{aligned}$$
3. **Its slope.** The diode's conductance is
   $$\frac{5.75\ \text{mA}}{25.85\ \text{mV}} = 0.222\ \text{S}$$
   so $f' = -0.223$ S.
4. **Newton step.**
   $$\begin{aligned} \Delta V &= -\frac{f}{f'} \\ &= -\frac{1.45\ \text{mA}}{0.223\ \text{S}} \\ &= -6.5\ \text{mV} \end{aligned}$$
   which gives 0.6935 V.
5. **Compare.** The answer is 0.6925 V; the next step lands within 0.02 mV.

**Answer:** one Newton step takes the guess from 0.7 V to 0.6935 V, against an answer of 0.6925 V.
:::
:::

::: example
## Compare the estimate with the exact ripple for $V_p$ = 10 V, 50 Hz, 1 kΩ and 100 µF.

::: solution
1. **Estimate.**
   $$\begin{aligned} \Delta V &= \frac{10}{50 \times 10^3 \times 10^{-4}} \\ &= 2.0\ \text{V} \end{aligned}$$
2. **Exact.** The exact analysis gives 1.65 V.
3. **Why it's smaller.** The capacitor's decay is exponential, so it falls a little less than a straight line, and the input meets it about 9% of a period early.

**Answer:** the estimate gives 2.0 V of ripple, and the exact ripple is 1.65 V.
:::
:::

::: check
given: VD = 0.6 | 0.7; IS = 1 | 10
answer: IS*1e-15*exp(VD/0.025852)/0.025852
unit: S
tolerance: 0.5%

## A diode with $I_S$ = {IS} fA is guessed at {VD} V. What small-signal conductance does Newton's method use for it?

::: right
Right. $\frac{I_S}{V_T}e^{V/V_T}$ = {answer}.
:::

::: misses
- [IS*1e-15*exp(VD/0.025852)] That's the current; the conductance is its slope.
- [any] Not yet. Differentiate $I_Se^{V/V_T}$.
:::
:::

::: check
given: Vs = 3 | 5 | 10; R = 1 | 10
answer: 0.7 - ((Vs - 0.7)/(R*1000) - 1e-14*(exp(0.7/0.025852) - 1))/(-1/(R*1000) - 1e-14/0.025852*exp(0.7/0.025852))
unit: V
tolerance: 0.01%

## Take one Newton step from 0.7 V for a diode ($I_S = 10^{-14}$ A) in series with {R} kΩ across {Vs} V. Where does it land?

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

::: misses
- [0.025852*ln((Vs - 0.7)/(R*1000)/1e-14)] That's the simple iteration of lesson 3, not Newton's step.
- [any] Not yet. $V - f(V)/f'(V)$ at 0.7 V.
:::
:::

::: check
given: f = 50 | 60 | 1000; R = 0.1 | 1; C = 100 | 1000
answer: atan(1/(2*pi*f*R*1000*C*1e-6))/(2*pi*f)*1e6
unit: µs
tolerance: 0.5%

## A half-wave rectifier at {f} Hz has RC of {R} kΩ × {C} µF. How long after the peak does its ideal diode stop conducting?

::: right
Right. $\tan\omega t_\text{off} = 1/\omega RC$: {answer}.
:::

::: misses
- [0] It keeps conducting a moment past the peak, until the input falls faster than RC lets the capacitor follow.
- [any] Not yet. Solve $\tan\omega t = 1/(\omega RC)$.
:::
:::

::: check
kind: python

## Write `solve_diode(Vs, R, IS=1e-14, n=1)` returning `(V, I)` for a diode in series with R across `Vs`, by Newton's method from 0.7 V. Use VT = 0.025852 V.

```python starter
import math

VT = 0.025852

def solve_diode(Vs, R, IS=1e-14, n=1):
    V = 0.7
    return V, (Vs - V) / R
```

```python solution
import math

VT = 0.025852

def solve_diode(Vs, R, IS=1e-14, n=1):
    V = 0.7
    for _ in range(200):
        e = IS * math.exp(V / (n * VT))
        f = (Vs - V) / R - (e - IS)
        df = -1 / R - e / (n * VT)
        step = f / df
        V -= step
        if abs(step) < 1e-13:
            break
    return V, (Vs - V) / R
```

```python tests
cases = [((0.5, 1e3), 0.49770357631264694), ((1, 1e3), 0.6291468588782716), ((5, 1e3), 0.6925436331805306), ((12, 100), 0.7768267943511051), ((5, 1e4), 0.6333699280771139)]
for (Vs, R), want in cases:
    V, I = solve_diode(Vs, R)
    assert abs(V - want) < 1e-9, f"Vs = {Vs} V, R = {R} Ω: expected {want:.6f} V, got {V:.6f} V."
    assert abs(I - (Vs - want) / R) < 1e-12, "Return the current through R as well."
V, I = solve_diode(5, 1e3, IS=1e-12, n=2)
assert abs(V - 1.141293306393933) < 1e-9, f"With IS = 1 pA and n = 2, expected 1.1413 V; got {V:.4f} V."
```

::: right
Right. The starter's fixed 0.7 V is fine at 5 V, but at 1 V it gives a current 19% low, and at 0.5 V an impossible negative current.
:::
:::

::: check
kind: python

## A supply `Vs` feeds R1 to node 1, which has diode D1 to ground and R2 on to node 2; node 2 has diode D2 to ground. Write `solve_network(Vs, R1, R2)` returning `(V1, V2)` by Newton's method on both nodes at once (both diodes have $I_S = 10^{-14}$ A, n = 1).

```python starter
import numpy as np

VT = 0.025852
IS = 1e-14

def solve_network(Vs, R1, R2):
    return 0.7, 0.7
```

```python solution
import numpy as np

VT = 0.025852
IS = 1e-14

def solve_network(Vs, R1, R2):
    v = np.array([0.7, 0.6])
    for _ in range(200):
        e1, e2 = IS * np.exp(v / VT)
        f = np.array([(Vs - v[0]) / R1 - (e1 - IS) - (v[0] - v[1]) / R2,
                      (v[0] - v[1]) / R2 - (e2 - IS)])
        J = np.array([[-1 / R1 - e1 / VT - 1 / R2, 1 / R2],
                      [1 / R2, -1 / R2 - e2 / VT]])
        step = np.linalg.solve(J, -f)
        v = v + step
        if np.max(np.abs(step)) < 1e-13:
            break
    return float(v[0]), float(v[1])
```

```python tests
cases = [((5, 1e3, 100), (0.6892681132503464, 0.6376798898521803)), ((10, 470, 1e3), (0.7317010223012819, 0.6019895069573793)), ((3, 1e3, 1e4), (0.6764378774597672, 0.5431635769620955))]
for args, (w1, w2) in cases:
    V1, V2 = solve_network(*args)
    assert abs(V1 - w1) < 1e-9 and abs(V2 - w2) < 1e-9, f"For {args}: expected ({w1:.6f}, {w2:.6f}) V, got ({V1:.6f}, {V2:.6f}) V."
```

::: right
Right. The Jacobian is the circuit's conductance matrix with each diode replaced by $I_Se^{V/V_T}/V_T$: the same matrix a simulator builds.
:::
:::

::: check
kind: python

## Write `exact_ripple(Vp, f, R, C)`: the steady-state peak-to-peak ripple of a half-wave rectifier with an ideal diode, input $V_p\cos\omega t$, found by bisection as in the explanation above.

```python starter
import math

def exact_ripple(Vp, f, R, C):
    return Vp / (f * R * C)
```

```python solution
import math

def exact_ripple(Vp, f, R, C):
    w, tau, T = 2 * math.pi * f, R * C, 1 / f
    toff = math.atan(1 / (w * tau)) / w
    V0 = math.cos(w * toff)
    g = lambda t: math.cos(w * t) - V0 * math.exp(-(t - toff) / tau)
    a, b = 0.75 * T, T
    for _ in range(200):
        m = (a + b) / 2
        if g(m) < 0:
            a = m
        else:
            b = m
    t1 = (a + b) / 2
    return Vp - Vp * V0 * math.exp(-(t1 - toff) / tau)
```

```python tests
cases = [((10, 50, 1e3, 100e-6), 1.6549624465195283), ((10, 60, 100, 1000e-6), 1.4105868438864952), ((5, 1000, 1e4, 1e-6), 0.4445396755827895), ((12, 50, 470, 47e-6), 6.27656075807883)]
for args, want in cases:
    got = exact_ripple(*args)
    assert abs(got - want) < 1e-9 * args[0], f"For Vp, f, R, C = {args}: expected {want:.6f} V, got {got:.6f} V."
```

::: right
Right. The estimate $V_pT/RC$ is 12% high for a 0.44 V ripple on 5 V, and 1.7 times the truth at the largest ripple here; it's a safe upper bound for choosing C.
:::
:::

::: explain
## Why is the Jacobian in Newton's method the same as the conductance matrix of a linear circuit?

::: model
Each row of the system is Kirchhoff's current law at one node: the currents leaving through every element must add to zero. Newton's method replaces each element's current by its tangent at the present guess. For a resistor that's the resistor itself; for a diode it's a conductance equal to the slope of its curve, $I_Se^{V/V_T}/V_T$, plus a constant current. So the derivatives of the node currents with respect to the node voltages are exactly the conductances you'd stamp into a nodal matrix for that linearized circuit, and each Newton step is just a nodal analysis of it.
:::
:::

::: practice

::: check
given: I = 0.1 | 1 | 10
answer: I*1e-3/0.025852
unit: S
tolerance: 0.5%

## What small-signal conductance does a diode carrying {I} mA have at 300 K?

::: misses
- [any] $I/V_T$.
:::
:::

::: check
given: R1 = 1 | 4.7; R2 = 2.2 | 10
answer: 1/(R1*1000) + 1/(R2*1000)
unit: S
tolerance: 0.5%

## Node 1 connects through {R1} kΩ to a source and {R2} kΩ to node 2. With its diode off, what is $-J_{11}$?

::: misses
- [any] The sum of the conductances at the node.
:::
:::

::: check
given: Vp = 5 | 10; f = 50 | 60; R = 1 | 2.2; C = 220 | 470
answer: Vp/(f*R*1000*C*1e-6)
unit: V
tolerance: 0.5%

## What does the estimate $V_pT/RC$ give for {Vp} V, {f} Hz, {R} kΩ and {C} µF?

::: misses
- [any] $V_p/(fRC)$.
:::
:::
:::
