codexmeasuring-system-lead-resistance.py
measuring-system-lead-resistance.py
"""Own ideal measuring-system example; write PNG in the current directory.
Tested with Python 3.14.4, NumPy 2.3.5 and Matplotlib 3.10.7.
Stipulated circuit and instrument offset, not experimental measurements.
"""
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Rectangle
current, resistance, lead_one, lead_two, offset = 0.001, 100.0, 1.0, 1.0, 0.001
true_drop = current * (resistance + lead_one + lead_two)
indication = true_drop + offset
assert abs(true_drop - 0.102) < 1e-12
assert abs(indication - 0.103) < 1e-12
assert abs((indication - offset) / current - lead_one - lead_two - resistance) < 1e-10
plt.rcParams.update({"font.size": 11, "figure.facecolor": "white"})
fig, ax = plt.subplots(figsize=(8.5, 4.6), dpi=100)
fig.subplots_adjust(left=0.035, right=0.985, bottom=0.20, top=0.86)
fig.suptitle("A measuring system includes the leads and readout", fontsize=15, y=0.96)
ax.set(xlim=(0, 10), ylim=(0.65, 5.85))
ax.axis("off")
def wire(xs, ys):
ax.plot(xs, ys, color="#333333", lw=1.8, zorder=1)
wire([0.9, 0.9], [3.2, 2.45])
wire([0.9, 0.9], [1.75, 1.0])
ax.add_patch(Circle((0.9, 2.1), 0.35, fill=False, lw=1.8))
ax.annotate("", xy=(0.9, 2.33), xytext=(0.9, 1.87),
arrowprops={"arrowstyle": "->", "color": "#1766a0", "lw": 1.8})
ax.text(1.8, 1.40, "I = 1 mA", ha="center", fontsize=10)
wire([0.9, 2.2], [3.2, 3.2])
wire([3.2, 4.2], [3.2, 3.2])
wire([5.8, 6.8], [3.2, 3.2])
wire([7.8, 9.1, 9.1, 0.9], [3.2, 3.2, 1.0, 1.0])
for left, width, label, color in [
(2.2, 1.0, "Lead R₁ = 1 Ω", "#eeeeee"),
(4.2, 1.6, "Rₓ = 100 Ω", "#dcebf5"),
(6.8, 1.0, "Lead R₂ = 1 Ω", "#eeeeee"),
]:
ax.add_patch(Rectangle((left, 3.0), width, 0.4, facecolor=color,
edgecolor="#333333", lw=1.8))
ax.text(left + width / 2, 2.68, label, ha="center", fontsize=10)
wire([0.9, 0.9, 4.65], [3.2, 5.0, 5.0])
wire([5.35, 9.1, 9.1], [5.0, 5.0, 3.2])
ax.add_patch(Circle((5, 5), 0.35, fill=False, lw=1.8))
ax.text(5, 5, "V", ha="center", va="center", fontsize=12)
ax.text(5, 5.55, "Indicated V = 0.103 V", ha="center", fontsize=11)
ax.scatter([0.9, 9.1], [3.2, 3.2], s=22, color="#333333", zorder=4)
ax.text(1.08, 3.48, "+", fontsize=13)
ax.text(8.83, 3.48, "−", fontsize=13)
ax.text(5, 2.0, "True series drop: I(Rₓ + R₁ + R₂) = 0.102 V", ha="center", fontsize=10)
ax.text(5, 1.48, "Chosen readout offset V₀ = 0.001 V", ha="center", fontsize=10)
fig.text(0.5, 0.10, "Corrected result: Rₓ = (V − V₀)/I − R₁ − R₂ = 100 Ω", ha="center", fontsize=12)
fig.text(0.5, 0.045, "Own ideal circuit; negligible voltmeter loading; all values stipulated.",
ha="center", fontsize=10)
fig.savefig(Path("measuring-system-lead-resistance.png"), facecolor="white")
plt.close(fig)