---
title: "Device simulation lab: a pn junction by Newton's method"
heading: Solving Poisson's equation with the carriers, and checking the depletion approximation
eyebrow: S1 · Lesson 51 · Device simulation lab
xp: 60
---

::: hook
Every formula for the pn junction in this course rests on the depletion approximation: carriers vanish abruptly at the edges of the depletion region. Real device simulators (TCAD) make no such assumption. They solve Poisson's equation with the electron and hole densities included, which makes it nonlinear, by Newton's method on a grid. In this lab you'll write a small version of that solver for a junction in equilibrium and under bias, and use it to measure how good the depletion approximation is: close, and off in a predictable way.
:::

::: predict
## The depletion approximation predicts a junction's peak field. Compared with the exact solution of Poisson's equation with the carriers included, is it high or low?

- [x] A few percent high: the carriers' tails into the depletion region screen part of the charge
- [ ] A few percent low
- [ ] Exact: the approximation is only about the shape
- [ ] Off by a factor of two

::: reveal
A few percent high. Near each edge, the majority carriers don't vanish abruptly; they fade over a few Debye lengths, partly cancelling the ionized dopants' charge. The exact solution behaves almost exactly like the depletion approximation with the voltage reduced by $2kT/q$, about 52 mV: for a 0.77 V junction that's 3% in peak field. Under large reverse bias the correction becomes negligible.
:::
:::

::: nameit
## The equations

Measure the electrostatic potential φ from the intrinsic level, with the hole quasi-Fermi potential at 0 and the electron one at $-V_a$ (lesson 7: both flat across the junction). Then

$$n = n_ie^{(\phi + V_a)/V_T}, \qquad p = n_ie^{-\phi/V_T}, \qquad \frac{d^2\phi}{dx^2} = -\frac{q}{\varepsilon}(p - n + N_d - N_a)$$

The boundaries are neutral contacts: $\phi(0) = -V_T\ln\frac{N_a}{n_i}$ and $\phi(L) = -V_a + V_T\ln\frac{N_d}{n_i}$.

## Newton's method on a grid

On a uniform grid with spacing h, the residual at each interior point is

$$F_i = \frac{\phi_{i-1} - 2\phi_i + \phi_{i+1}}{h^2} + \frac{q}{\varepsilon}(p_i - n_i + D_i)$$

and its Jacobian is tridiagonal: $\frac{1}{h^2}$ off the diagonal, $-\frac{2}{h^2} - \frac{q}{\varepsilon V_T}(p_i + n_i)$ on it. Each Newton step solves $J\,\delta = -F$ (a tridiagonal solver, provided) and updates φ by δ, clipped to ±0.5 V so early steps from a crude guess can't overshoot. Starting from the neutral values on each side, it converges in about ten steps.

## Resolution

The grid must resolve the Debye length on both sides, $L_D = \sqrt{\varepsilon kT/q^2N}$: 4 nm at $10^{18}$ cm⁻³. A junction with a heavily doped side needs a fine grid (or a nonuniform one, as real simulators use) there.

## What you'll write

1. `depletion(Na, Nd, Va=0.0)`: return `(Vbi, W, Emax)` from the depletion approximation, W in cm, $E_\text{max}$ in V/cm.
2. `solve_junction(Na, Nd, Va=0.0, L=4e-4, n=801)`: return `(x, phi)`, the Newton solution, with the junction at L/2.
3. `junction_cap(Na, Nd, Va)`: the capacitance per area from the solver, $\varepsilon\frac{dE_\text{max}}{d(-V_a)}$ by a central difference with h = 0.05 V, using a domain six depletion widths long (at 5 V reverse) and 1201 points. (Gauss's law: the charge on each side is $\varepsilon E_\text{max}$.)
:::

::: example
## Why does a better approximation subtract $2kT/q$ from the junction voltage?

::: solution
1. **Carrier tails.** Each side of the junction has a transition region a few Debye lengths thick where the majority carriers fade out.
2. **Where the voltage goes.** Their remaining charge partly offsets the dopants', so a little of the junction voltage is spent pushing back carriers rather than across bare dopants.
3. **How much.** Integrating Poisson's equation through those tails shows the lost voltage is kT/q on each side.
4. **An example.** For $N_a = 10^{17}$, $N_d = 10^{16}$ the plain formula gives 46.6 kV/cm; with 0.774 − 0.052 V it gives 45.1, and the Newton solver gives 45.2.

**Answer:** the majority-carrier tails on each side of the junction use up kT/q each, $2kT/q$ in all; subtracting it gives 45.1 kV/cm in the example, against the solver's 45.2.
:::
:::

::: example
## How do you get the capacitance from a solver that only gives the potential?

::: solution
1. **Charge from the field.** By Gauss's law, the field at the junction equals the charge on one side divided by ε, so per area
   $$Q = \varepsilon E_\text{max}$$
2. **Difference two solutions.** Solving at two nearby biases and differencing gives
   $$\begin{aligned} C &= \frac{dQ}{dV_R} \\ &= \frac{\varepsilon}{2h}[E_\text{max}(V_a - h) \\ &\quad - E_\text{max}(V_a + h)] \end{aligned}$$
3. **Check against $\varepsilon/W$.** At 1 V and 3 V reverse the solver lands within 1.5% of $\varepsilon/W$, slightly above it, since the carrier tails make the effective plate spacing a little smaller than W.

**Answer:** take $Q = \varepsilon E_\text{max}$ from Gauss's law at two nearby biases and difference them to get $C = dQ/dV_R$.
:::
:::

::: check
kind: python

## Step 1. Write `depletion(Na, Nd, Va=0.0)` returning `(Vbi, W, Emax)` in V, cm and V/cm.

```python starter
import math
q = 1.602176634e-19
eps = 11.7 * 8.854e-14
Vt = 0.02585
ni = 1e10

def depletion(Na, Nd, Va=0.0):
    Vbi = 0.0
    W = 1e-4
    Emax = 0.0
    return Vbi, W, Emax

print(depletion(1e17, 1e16))
```

```python solution
import math
q = 1.602176634e-19
eps = 11.7 * 8.854e-14
Vt = 0.02585
ni = 1e10

def depletion(Na, Nd, Va=0.0):
    Vbi = Vt * math.log(Na * Nd / ni**2)
    W = math.sqrt(2 * eps * (Vbi - Va) / q * (Na + Nd) / (Na * Nd))
    Emax = 2 * (Vbi - Va) / W
    return Vbi, W, Emax

print(depletion(1e17, 1e16))
```

```python tests
Vbi, W, E = depletion(1e17, 1e16)
assert abs(Vbi - 0.773784) < 1e-5, f"V_bi for 1e17 and 1e16 should be 0.7738 V; got {Vbi:.5f}."
assert abs(W * 1e4 - 0.331764) < 1e-4, f"W should be 0.3318 µm; got {W * 1e4:.5f}."
assert abs(E / 46646.7 - 1) < 1e-4, f"E_max should be 46.65 kV/cm; got {E / 1000:.3f}."
Vbi, W, E = depletion(1e17, 1e16, -5.0)
assert abs(W * 1e4 - 0.906252) < 1e-4, f"At 5 V reverse, W should be 0.9063 µm; got {W * 1e4:.5f}."
```

::: right
Right. These are the reference values the solver will be measured against.
:::
:::

::: check
kind: python

## Step 2. Write `solve_junction(Na, Nd, Va=0.0, L=4e-4, n=801)` returning `(x, phi)`.

```python starter
import numpy as np, math
q = 1.602176634e-19
eps = 11.7 * 8.854e-14
Vt = 0.02585
ni = 1e10

def thomas(a, b, c, d):
    # solves a tridiagonal system: a below the diagonal, b on it, c above
    n = len(b); cp = np.zeros(n); dp = np.zeros(n)
    cp[0] = c[0] / b[0]; dp[0] = d[0] / b[0]
    for i in range(1, n):
        m = b[i] - a[i] * cp[i - 1]
        cp[i] = c[i] / m if i < n - 1 else 0.0
        dp[i] = (d[i] - a[i] * dp[i - 1]) / m
    x = np.zeros(n); x[-1] = dp[-1]
    for i in range(n - 2, -1, -1):
        x[i] = dp[i] - cp[i] * x[i + 1]
    return x

def solve_junction(Na, Nd, Va=0.0, L=4e-4, n=801):
    x = np.linspace(0, L, n); h = x[1] - x[0]
    D = np.where(x < L / 2, -Na, Nd).astype(float)
    D[np.isclose(x, L / 2)] = (Nd - Na) / 2
    left = -Vt * math.log(Na / ni)
    right = -Va + Vt * math.log(Nd / ni)
    phi = np.where(x < L / 2, left, right).astype(float)
    # Newton: build F and the tridiagonal Jacobian, solve, clip, update, repeat
    return x, phi

x, phi = solve_junction(1e17, 1e16)
print(phi[-1] - phi[0])
```

```python solution
import numpy as np, math
q = 1.602176634e-19
eps = 11.7 * 8.854e-14
Vt = 0.02585
ni = 1e10

def thomas(a, b, c, d):
    n = len(b); cp = np.zeros(n); dp = np.zeros(n)
    cp[0] = c[0] / b[0]; dp[0] = d[0] / b[0]
    for i in range(1, n):
        m = b[i] - a[i] * cp[i - 1]
        cp[i] = c[i] / m if i < n - 1 else 0.0
        dp[i] = (d[i] - a[i] * dp[i - 1]) / m
    x = np.zeros(n); x[-1] = dp[-1]
    for i in range(n - 2, -1, -1):
        x[i] = dp[i] - cp[i] * x[i + 1]
    return x

def solve_junction(Na, Nd, Va=0.0, L=4e-4, n=801):
    x = np.linspace(0, L, n); h = x[1] - x[0]
    D = np.where(x < L / 2, -Na, Nd).astype(float)
    D[np.isclose(x, L / 2)] = (Nd - Na) / 2
    left = -Vt * math.log(Na / ni)
    right = -Va + Vt * math.log(Nd / ni)
    phi = np.where(x < L / 2, left, right).astype(float)
    k = q / eps
    for it in range(200):
        nn = ni * np.exp((phi + Va) / Vt)
        pp = ni * np.exp(-phi / Vt)
        F = np.zeros(n)
        F[1:-1] = (phi[:-2] - 2 * phi[1:-1] + phi[2:]) / h**2 + k * (pp[1:-1] - nn[1:-1] + D[1:-1])
        a = np.zeros(n); b = np.ones(n); c = np.zeros(n)
        a[1:-1] = 1 / h**2; c[1:-1] = 1 / h**2
        b[1:-1] = -2 / h**2 - k * (pp[1:-1] + nn[1:-1]) / Vt
        d = np.clip(thomas(a, b, c, -F), -0.5, 0.5)
        phi = phi + d
        if np.max(np.abs(d)) < 1e-10:
            break
    return x, phi

x, phi = solve_junction(1e17, 1e16)
print(phi[-1] - phi[0])
```

```python tests
import numpy as np, math
def peak(x, phi):
    return np.max(np.abs(np.diff(phi))) / (x[1] - x[0])
for Na, Nd, L in ((1e17, 1e16, 2e-4), (1e16, 1e16, 3e-4)):
    x, phi = solve_junction(Na, Nd, 0.0, L, 1201)
    Vbi = 0.02585 * math.log(Na * Nd / 1e20)
    assert abs((phi[-1] - phi[0]) - Vbi) < 1e-9, "The potential must drop by V_bi across the device."
    E = peak(x, phi)
    eps = 11.7 * 8.854e-14
    Wc = math.sqrt(2 * eps * (Vbi - 2 * 0.02585) / 1.602176634e-19 * (Na + Nd) / (Na * Nd))
    Ec = 2 * (Vbi - 2 * 0.02585) / Wc
    W0 = math.sqrt(2 * eps * Vbi / 1.602176634e-19 * (Na + Nd) / (Na * Nd))
    E0 = 2 * Vbi / W0
    assert abs(E / Ec - 1) < 0.01, f"For Na = {Na:.0e}, Nd = {Nd:.0e} the peak field should be within 1% of the corrected depletion value {Ec:.0f} V/cm; got {E:.0f}."
    assert 0.02 < 1 - E / E0 < 0.06, f"The exact peak should sit 2 to 6% below the plain depletion value {E0:.0f}; got {E:.0f}."
    Eends = np.abs(np.diff(phi))[[0, -1]] / (x[1] - x[0])
    assert np.all(Eends < 1e-3 * E), "The field should vanish at the neutral contacts."
x, phi = solve_junction(1e17, 1e16, -5.0, 6e-4, 1201)
assert abs((phi[-1] - phi[0]) - (0.02585 * math.log(1e13) + 5)) < 1e-9, "Under 5 V reverse the drop is V_bi + 5 V."
```

::: right
Right. Ten or so Newton steps give the exact equilibrium (and quasi-equilibrium) solution, and it sits right on the depletion approximation once $2kT/q$ is taken off the voltage.
:::
:::

::: check
kind: python

## Step 3. Write `junction_cap(Na, Nd, Va)`, the capacitance per area from the solver.

```python starter
import numpy as np, math
q = 1.602176634e-19
eps = 11.7 * 8.854e-14
Vt = 0.02585
ni = 1e10

def depletion(Na, Nd, Va=0.0):
    Vbi = Vt * math.log(Na * Nd / ni**2)
    W = math.sqrt(2 * eps * (Vbi - Va) / q * (Na + Nd) / (Na * Nd))
    return Vbi, W, 2 * (Vbi - Va) / W

def thomas(a, b, c, d):
    n = len(b); cp = np.zeros(n); dp = np.zeros(n)
    cp[0] = c[0] / b[0]; dp[0] = d[0] / b[0]
    for i in range(1, n):
        m = b[i] - a[i] * cp[i - 1]
        cp[i] = c[i] / m if i < n - 1 else 0.0
        dp[i] = (d[i] - a[i] * dp[i - 1]) / m
    x = np.zeros(n); x[-1] = dp[-1]
    for i in range(n - 2, -1, -1):
        x[i] = dp[i] - cp[i] * x[i + 1]
    return x

def solve_junction(Na, Nd, Va=0.0, L=4e-4, n=801):
    x = np.linspace(0, L, n); h = x[1] - x[0]
    D = np.where(x < L / 2, -Na, Nd).astype(float)
    D[np.isclose(x, L / 2)] = (Nd - Na) / 2
    phi = np.where(x < L / 2, -Vt * math.log(Na / ni), -Va + Vt * math.log(Nd / ni)).astype(float)
    k = q / eps
    for it in range(200):
        nn = ni * np.exp((phi + Va) / Vt); pp = ni * np.exp(-phi / Vt)
        F = np.zeros(n)
        F[1:-1] = (phi[:-2] - 2 * phi[1:-1] + phi[2:]) / h**2 + k * (pp[1:-1] - nn[1:-1] + D[1:-1])
        a = np.zeros(n); b = np.ones(n); c = np.zeros(n)
        a[1:-1] = 1 / h**2; c[1:-1] = 1 / h**2
        b[1:-1] = -2 / h**2 - k * (pp[1:-1] + nn[1:-1]) / Vt
        d = np.clip(thomas(a, b, c, -F), -0.5, 0.5)
        phi = phi + d
        if np.max(np.abs(d)) < 1e-10:
            break
    return x, phi

def junction_cap(Na, Nd, Va):
    # solve at Va - 0.05 and Va + 0.05, take eps times the change in peak field per volt
    return 0.0

print(junction_cap(1e17, 1e16, -1.0), eps / depletion(1e17, 1e16, -1.0)[1])
```

```python solution
import numpy as np, math
q = 1.602176634e-19
eps = 11.7 * 8.854e-14
Vt = 0.02585
ni = 1e10

def depletion(Na, Nd, Va=0.0):
    Vbi = Vt * math.log(Na * Nd / ni**2)
    W = math.sqrt(2 * eps * (Vbi - Va) / q * (Na + Nd) / (Na * Nd))
    return Vbi, W, 2 * (Vbi - Va) / W

def thomas(a, b, c, d):
    n = len(b); cp = np.zeros(n); dp = np.zeros(n)
    cp[0] = c[0] / b[0]; dp[0] = d[0] / b[0]
    for i in range(1, n):
        m = b[i] - a[i] * cp[i - 1]
        cp[i] = c[i] / m if i < n - 1 else 0.0
        dp[i] = (d[i] - a[i] * dp[i - 1]) / m
    x = np.zeros(n); x[-1] = dp[-1]
    for i in range(n - 2, -1, -1):
        x[i] = dp[i] - cp[i] * x[i + 1]
    return x

def solve_junction(Na, Nd, Va=0.0, L=4e-4, n=801):
    x = np.linspace(0, L, n); h = x[1] - x[0]
    D = np.where(x < L / 2, -Na, Nd).astype(float)
    D[np.isclose(x, L / 2)] = (Nd - Na) / 2
    phi = np.where(x < L / 2, -Vt * math.log(Na / ni), -Va + Vt * math.log(Nd / ni)).astype(float)
    k = q / eps
    for it in range(200):
        nn = ni * np.exp((phi + Va) / Vt); pp = ni * np.exp(-phi / Vt)
        F = np.zeros(n)
        F[1:-1] = (phi[:-2] - 2 * phi[1:-1] + phi[2:]) / h**2 + k * (pp[1:-1] - nn[1:-1] + D[1:-1])
        a = np.zeros(n); b = np.ones(n); c = np.zeros(n)
        a[1:-1] = 1 / h**2; c[1:-1] = 1 / h**2
        b[1:-1] = -2 / h**2 - k * (pp[1:-1] + nn[1:-1]) / Vt
        d = np.clip(thomas(a, b, c, -F), -0.5, 0.5)
        phi = phi + d
        if np.max(np.abs(d)) < 1e-10:
            break
    return x, phi

def junction_cap(Na, Nd, Va):
    L = 6 * depletion(Na, Nd, -5.0)[1]
    def emax(V):
        x, phi = solve_junction(Na, Nd, V, L, 1201)
        return np.max(np.abs(np.diff(phi))) / (x[1] - x[0])
    h = 0.05
    return eps * (emax(Va - h) - emax(Va + h)) / (2 * h)

print(junction_cap(1e17, 1e16, -1.0), eps / depletion(1e17, 1e16, -1.0)[1])
```

```python tests
for Va in (-1.0, -3.0):
    C = junction_cap(1e17, 1e16, Va)
    Cd = eps / depletion(1e17, 1e16, Va)[1]
    assert 0 < C / Cd - 1 < 0.03, f"At {Va} V the solver's capacitance should be within 3% above ε/W = {Cd:.4e} F/cm²; got {C:.4e}."
assert junction_cap(1e17, 1e16, -1.0) > junction_cap(1e17, 1e16, -3.0), "Capacitance falls as reverse bias grows."
```

::: right
Right. Gauss's law turns the solver's peak field into charge, and differencing gives a capacitance that confirms the depletion formula within about 1%, slightly above it as the carrier tails predict.
:::
:::

::: check
given: N = 1e15 | 1e17 | 1e19
answer: sqrt(11.7*8.854e-14*0.02585/(1.602e-19*N))*1e7
unit: nm
tolerance: 1%

## A device simulator must resolve the Debye length. What is it in silicon doped {N} cm⁻³?

::: right
Right. $L_D = \sqrt{\frac{\varepsilon kT}{q^2N}}$ = {answer} nm.
:::

::: misses
- [any] Not yet. ε in F/cm, kT/q in volts, N in cm⁻³.
:::
:::

::: check
given: V = 0.7 | 0.8 | 0.9; R = 0 | 1
answer: (1 - sqrt((V + R - 2*0.02585)/(V + R)))*100
unit: %
tolerance: 2%

## A junction has $V_{bi}$ = {V} V and {R} V of reverse bias. By what percentage does the $2kT/q$ correction lower the predicted peak field?

::: right
Right. $E_\text{max} \propto \sqrt{V}$, so $1 - \sqrt{\frac{V - 2kT/q}{V}}$ = {answer}%.
:::

::: misses
- [2*0.02585/(V + R)*100] The field goes as the square root of the voltage: about half that.
- [any] Not yet. Compare $\sqrt{V_{bi} - V_a - 2kT/q}$ with $\sqrt{V_{bi} - V_a}$.
:::
:::

::: check
given: E = 20 | 50 | 120
answer: 11.7*8.854e-14*E*1000/1.602e-19
unit: cm^-2
tolerance: 1%

## A junction's peak field is {E} kV/cm. How many ionized dopants per cm² sit on each side of it?

::: right
Right. Gauss's law: $\varepsilon E_\text{max}/q$ = {answer} cm⁻².
:::

::: misses
- [any] Not yet. The field at the junction is the charge on one side over ε.
:::
:::

::: explain
## Why does Newton's method need its updates clipped when it starts from a crude guess, but converge quickly once it's close?

::: model
The carrier densities depend exponentially on the potential, so the equations are strongly nonlinear. Far from the solution, the linearized Jacobian can be a poor guide: a full Newton step computed from it may overshoot by volts, and then the exponentials blow up or the iteration bounces around. Clipping each update to a fraction of a volt keeps the early steps in the range where the linearization is roughly right. Once the guess is close, the linearization is accurate, every step roughly squares the error, and the full steps are small enough that the clip never acts. That's the usual pattern for device simulators: damped steps at first, then quadratic convergence.
:::
:::

::: practice

::: check
given: N = 1e14 | 1e15 | 3e15 | 1e16 | 1e17 | 3e18
answer: sqrt(11.7*8.854e-14*0.02585/(1.602e-19*N))*1e7
unit: nm
tolerance: 1%

## Debye length at {N} cm⁻³?

::: misses
- [any] $\sqrt{\varepsilon kT/q^2N}$.
:::
:::

::: check
given: E = 10 | 30 | 50 | 90 | 150 | 250
answer: 11.7*8.854e-14*E*1000/1.602e-19
unit: cm^-2
tolerance: 1%

## Dopants per cm² on each side for a {E} kV/cm peak?

::: misses
- [any] $\varepsilon E/q$.
:::
:::

::: check
given: V = 0.75 | 0.85; R = 0.5 | 2
answer: (1 - sqrt((V + R - 2*0.02585)/(V + R)))*100
unit: %
tolerance: 2%

## Peak-field correction: $V_{bi}$ = {V} V, {R} V reverse?

::: misses
- [any] $1 - \sqrt{(V - 2kT/q)/V}$.
:::
:::
:::
