Skip to content
Advertisement

Python algorithm to approximate closest parallel equivalence of resistors from a list

The formula for series equivalence of resistors:

series equivalence = sum(resistors)

For parallel it is 1/(sum(1/resistors[i]))

I wrote code to return a list of resistors that is closest to a specified target value from a list, within a specified tolerance.

JavaScript

So for example series_equivalance([1,2,3,4,5],7,0)will return [5,2].

I want to a function that can do the same for parallel equivalence. How would I go about it?

Advertisement

Answer

Edit: I made a blog post which expands on the mip solution and can solve for minimum resistors satisfying a tolerance.

I’ve solved this two ways

  1. Using your function and feeding it inverse values
  2. Using a mixed integer linear program

Using your function and feeding it inverse values

This is a half way solution that just feeds in 1/R for the resistor values and gives it a target resistance of 1/target. Then, take reciprocal of the result and you have your resistor values. Doesn’t work with the tolerance value properly. Need to comment out the “Your target is too small” check for it to work.

JavaScript

print(parallel_equivalance([1, 2, 3, 4, 5, 6, 7], 1.5555, 1e-2)) gives Resistors to use are [5, 2], Approximated value: 7, %Δ of 0.0%

Using a mixed integer linear program

For this method I use a mixed integer linear program to pick out which resistors to use, such that (1/r1 + 1/r2 + …) is as close as possible to 1/target. Solves very quickly (<1s) even when given ten thousand resistors to choose from.

You could modify this to pick the least number of resistors (minimise sum(R_in_use)) with the constraint that the error value must be within some margin (say, error >= -eps and error <= +eps)

You’ll need to pip install mip

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement