---
title: Problem set 10
heading: "Problem set 10: numerical methods"
eyebrow: A4 · Problem set 10
type: problems
---

::: hook
These labs use lessons 36 to 43: complex numbers in code, solving equations, numerical derivatives and integrals, and stepping systems through time. Each is a small piece of a circuit simulator, which is what these methods are for.
:::

::: pool
pick: 5

::: check
kind: python
given: Vs = 5 | 12; R1 = 1000 | 2200; R2 = 1000 | 4700; R3 = 2200 | 1000

## A source `Vs` = {Vs} V feeds node 1 through `R1` = {R1} Ω. `R2` = {R2} Ω joins node 1 to node 2, and `R3` = {R3} Ω and a 1 kΩ resistor go from node 2 to ground, in parallel. Node 1 also has 10 kΩ to ground. Write the nodal equations $G\mathbf{v} = \mathbf{i}$ and set `v` to the two node voltages with `np.linalg.solve`.

```python starter
import numpy as np
v = np.zeros(2)
print(v)
```

```python solution
import numpy as np
G = np.array([[1 / R1 + 1 / R2 + 1 / 10e3, -1 / R2],
              [-1 / R2, 1 / R2 + 1 / R3 + 1 / 1e3]])
i = np.array([Vs / R1, 0.0])
v = np.linalg.solve(G, i)
print(v)
```

```python tests
import numpy as np
G = np.array([[1 / R1 + 1 / R2 + 1 / 10e3, -1 / R2], [-1 / R2, 1 / R2 + 1 / R3 + 1 / 1e3]])
want = np.linalg.solve(G, np.array([Vs / R1, 0.0]))
assert np.shape(v) == (2,) and np.allclose(v, want), "Each row is KCL at a node: the sum of conductances at the node times its voltage, minus the shared conductance times the neighbor's voltage, equals the source current Vs/R1 at node 1."
```
:::

::: check
kind: python
given: Vs = 3 | 5 | 12; R = 220 | 1000

## A diode obeys $I = I_s(e^{V_d/V_T} - 1)$ with $I_s = 10^{-14}$ A and $V_T$ = 0.02585 V, in series with `R` = {R} Ω across `Vs` = {Vs} V. Solve $V_s = IR + V_d$ for the diode voltage with bisection on $[0, V_s]$ to within $10^{-12}$ V. Set `Vd` and `I`.

```python starter
import math
Vd = 0.7
I = (Vs - Vd) / R
print(Vd, I)
```

```python solution
import math
Is, VT = 1e-14, 0.02585
g = lambda vd: Is * (math.exp(vd / VT) - 1) * R + vd - Vs
lo, hi = 0.0, float(Vs)
while hi - lo > 1e-12:
    m = (lo + hi) / 2
    if g(m) < 0:
        lo = m
    else:
        hi = m
Vd = (lo + hi) / 2
I = (Vs - Vd) / R
print(Vd, I)
```

```python tests
import math
Is, VT = 1e-14, 0.02585
assert abs(Is * (math.exp(Vd / VT) - 1) - (Vs - Vd) / R) < 1e-6 * (Vs - Vd) / R, "At the answer, the diode's current must equal the resistor's, (Vs − Vd)/R."
assert abs(I - (Vs - Vd) / R) < 1e-12, "I should be (Vs − Vd)/R."
```
:::

::: check
kind: python
given: a = 2 | 3; b = 2 | 4; c = 1 | 2

## A filter's characteristic polynomial is $s^3 + \given{a}s^2 + \given{b}s + \given{c}$. Set `p` to its roots from `np.roots`, `stable` to whether all have negative real parts, and `slowest` to the root's real part closest to zero.

```python starter
import numpy as np
p = np.array([])
stable = None
slowest = 0
print(p, stable)
```

```python solution
import numpy as np
p = np.roots([1, a, b, c])
stable = bool(np.all(p.real < 0))
slowest = p.real.max()
print(p, stable)
```

```python tests
import numpy as np
r = np.roots([1, a, b, c])
assert len(p) == 3 and np.allclose(np.sort_complex(p), np.sort_complex(r)), "Pass the coefficients [1, a, b, c] to np.roots."
assert stable == bool(np.all(r.real < 0)), "stable should be True only if every root's real part is negative."
assert abs(slowest - r.real.max()) < 1e-12, "slowest is the largest real part."
```
:::

::: check
kind: python
given: C = 1 | 10; V = 5 | 12; tau = 1 | 3

## A capacitor `C` = {C} µF charges toward `V` = {V} V through a resistor, with $\tau$ = {tau} ms. Sample $v$ and $i = C\,dv/dt$ (exact formulas) at 2001 points over $10\tau$, and set `E` to the energy delivered, $\int v\,i\,dt$, by the trapezoid rule. Compare with $\frac{1}{2}CV^2$ as `E_ideal`.

```python starter
import numpy as np
E = E_ideal = 0
print(E, E_ideal)
```

```python solution
import numpy as np
Cf, tt = C * 1e-6, tau * 1e-3
t = np.linspace(0, 10 * tt, 2001)
v = V * (1 - np.exp(-t / tt))
i = Cf * V / tt * np.exp(-t / tt)
p = v * i
h = t[1] - t[0]
E = h * (p.sum() - (p[0] + p[-1]) / 2)
E_ideal = 0.5 * Cf * V**2
print(E, E_ideal)
```

```python tests
assert abs(E_ideal - 0.5 * C * 1e-6 * V**2) < 1e-15, "E_ideal is ½CV², with C in farads."
assert abs(E - E_ideal) < 1e-3 * E_ideal, "E should come out within 0.1% of ½CV² (it's a little short because v isn't quite V after 10τ)."
```
:::

::: check
kind: python
given: tau = 1 | 2; n = 10 | 20

## Charge $dv/dt = (1 - v)/\tau$ from 0 for one time constant (`tau` = {tau}) in `n` = {n} steps with Euler and with RK4. Set `e_euler`, `e_rk4` to their errors against $1 - e^{-1}$.

```python starter
import math
e_euler = e_rk4 = 0
print(e_euler, e_rk4)
```

```python solution
import math
f = lambda t, v: (1 - v) / tau
h = tau / n
ve = vr = 0.0
for k in range(n):
    ve = ve + h * f(k * h, ve)
    k1 = f(k * h, vr)
    k2 = f(k * h + h / 2, vr + h / 2 * k1)
    k3 = f(k * h + h / 2, vr + h / 2 * k2)
    k4 = f(k * h + h, vr + h * k3)
    vr = vr + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
exact = 1 - math.exp(-1)
e_euler, e_rk4 = abs(ve - exact), abs(vr - exact)
print(e_euler, e_rk4)
```

```python tests
import math
h = 1 / n
ve = 1 - (1 - h)**n
assert abs(e_euler - abs(ve - (1 - math.exp(-1)))) < 1e-12, "e_euler doesn't match n Euler steps."
assert e_rk4 < 1e-5 and e_rk4 < e_euler / 1000, "RK4 should be far more accurate."
```
:::

::: check
kind: python
given: R = 10 | 40; L = 10 | 47

## Simulate a series RLC (`R` = {R} Ω, `L` = {L} mH, 1 µF) responding to a 1 V step, with RK4 at 1 µs for 20,000 steps. Set `ts` to the settling time in seconds: the time after which $v$ stays within 2% of 1 V.

```python starter
import numpy as np
ts = 0
print(ts)
```

```python solution
import numpy as np
Lh, C, h = L * 1e-3, 1e-6, 1e-6
f = lambda y: np.array([y[1] / C, (1 - R * y[1] - y[0]) / Lh])
y = np.zeros(2)
v = np.zeros(20000)
for k in range(20000):
    k1 = f(y); k2 = f(y + h / 2 * k1); k3 = f(y + h / 2 * k2); k4 = f(y + h * k3)
    y = y + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4)
    v[k] = y[0]
outside = np.nonzero(np.abs(v - 1) > 0.02)[0]
ts = (outside[-1] + 2) * h
print(ts)
```

```python tests
import numpy as np
zeta = R / 2 * np.sqrt(1e-6 / (L * 1e-3))
w0 = 1 / np.sqrt(L * 1e-3 * 1e-6)
est = 4 / (zeta * w0)
assert 0.5 * est < ts < 1.5 * est, f"ts should be roughly 4/(ζω₀) ≈ {est:.3g} s: find the last sample outside ±2% and take the time just after it."
```
:::

::: check
kind: python
given: A = 3 | 5; B = 4 | 2; p = 60 | 120

## Using NumPy complex arithmetic, add three phasors: `A` = {A} at 0°, `B` = {B} at {p}°, and 2 at −90°. Set `S` to the sum (a complex number), `mag` to its size and `deg` to its angle in degrees.

```python starter
import numpy as np
S = 0
mag = deg = 0
print(S)
```

```python solution
import numpy as np
S = A + B * np.exp(1j * np.radians(p)) + 2 * np.exp(-1j * np.pi / 2)
mag = abs(S)
deg = np.degrees(np.angle(S))
print(S, mag, deg)
```

```python tests
import numpy as np
want = A + B * np.exp(1j * np.radians(p)) - 2j
assert abs(S - want) < 1e-12, "Build each phasor as amplitude * np.exp(1j * angle_in_radians) and add."
assert abs(mag - abs(want)) < 1e-12 and abs(deg - np.degrees(np.angle(want))) < 1e-9, "mag is abs(S) and deg is np.degrees(np.angle(S))."
```
:::
:::
