Created
September 25, 2011 15:33
-
-
Save xesscorp/1240722 to your computer and use it in GitHub Desktop.
Program for computing the best resistors to parallel in order to synthesize the 168 standard resistor values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MAX_FLOAT = 10E99 | |
NUM_RESISTORS = 3 | |
resistorRoots = [ 1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, | |
3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1 ] | |
resistorDecades = range(0, 7) | |
standardResistors = [r * 10 ** d for r in resistorRoots for d in resistorDecades] | |
def CalcParallelResistance(r0, r1=MAX_FLOAT, r2=MAX_FLOAT): | |
return 1.0 / (1.0 / r0 + 1.0 / r1 + 1.0 / r2) | |
def CalcResistorError(synthResistors): | |
totalError = 0 | |
for stdRes in standardResistors: | |
minError = MAX_FLOAT | |
for synRes in synthResistors: | |
error = ((stdRes - synRes) / stdRes) ** 2 | |
if error < minError: | |
minError = error | |
totalError += minError | |
return totalError | |
def FindResistorCombinations(resistorRoots): | |
resistors = [r * 10 ** d for r in resistorRoots for d in resistorDecades] | |
resistors.append(MAX_FLOAT) | |
synthResistors = [[CalcParallelResistance(a, b, c), a, b, c] | |
for a in resistors | |
for b in resistors | |
for c in resistors | |
if (a <= b <= c) | |
] | |
for stdRes in standardResistors: | |
minError = MAX_FLOAT | |
for synRes in synthResistors: | |
error = ((synRes[0] - stdRes) / stdRes) ** 2 | |
if error < minError: | |
minError = error | |
closestSynRes = synRes[:] | |
pctError = (synRes[0] - stdRes) / stdRes | |
print '{},{},{},{},{},{}'.format( | |
stdRes, | |
pctError, | |
closestSynRes[0], | |
closestSynRes[1], | |
closestSynRes[2], | |
closestSynRes[3], | |
) | |
selectedResistorRoots = [0] * NUM_RESISTORS | |
minError = MAX_FLOAT | |
for r0Index in range(0, len(resistorRoots)): | |
selectedResistorRoots[0] = resistorRoots[r0Index] | |
for r1Index in range(r0Index + 1, len(resistorRoots)): | |
selectedResistorRoots[1] = resistorRoots[r1Index] | |
for r2Index in range(r1Index+1, len(resistorRoots)): | |
selectedResistorRoots[2] = resistorRoots[r2Index] | |
selectedResistors = [r * 10 ** d for r in selectedResistorRoots for d in resistorDecades] | |
selectedResistors.append(MAX_FLOAT) | |
synthResistors = [CalcParallelResistance(a, b, c) | |
for a in selectedResistors | |
for b in selectedResistors | |
for c in selectedResistors | |
if (a <= b <= c) | |
] | |
error = CalcResistorError(synthResistors) | |
if error < minError: | |
minError = error | |
bestSelectedResistorRoots = selectedResistorRoots[:] | |
print minError, bestSelectedResistorRoots | |
print bestSelectedResistorRoots | |
FindResistorCombinations(bestSelectedResistorRoots) | |
#FindResistorCombinations([2.4, 6.2, 9.1]) | |
#FindResistorCombinations([2.4, 8.2, MAX_FLOAT]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment