Skip to content

Instantly share code, notes, and snippets.

@randName
Last active January 15, 2019 06:35
Show Gist options
  • Save randName/dafea83d927ded5ba39c to your computer and use it in GitHub Desktop.
Save randName/dafea83d927ded5ba39c to your computer and use it in GitHub Desktop.
Gets the best resistors in the E12 series to use for a particular voltage drop desired
E12 = (1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2)
ROS = tuple((x / y, (x, y)) for x in E12 for y in E12)
def estimator(inv, ouv):
ratio = (inv - ouv) / ouv
for r, v in ROS:
abserr = abs(r - ratio)
relerr = abserr / ratio
yield (relerr, abserr, v)
if __name__ == "__main__":
inv = 8.4
ouv = 5.0
for a, b, r in sorted(estimator(inv, ouv))[:5]:
print(r, "- Ratio: %.5f, Error: %.2f%%" % (r[0] / r[1], a * 100))
print("\t%.2f => %.5f" % (inv, (inv * r[1]) / (r[0] + r[1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment