1
$\begingroup$

I am trying to check if a list of resistors can be used to achieve a set of equivalent resistances. (Reciprocal of the sum of the reciprocals of the individual resistors is the equivalent resistance) Achieving the exact value of equivalent resistance is very tough, so I would like to allow a tolerance of 5% max. privArrayTest generates a list of all combinations of the resistors and calculates the equivalence. I attempted to implement the tolerance with tolerance[x_], and run that through ContainsAll, but it appears to just be checking the edge values rather than values within the range.

ParallelResist[arrayInTest_, arrayInCompare_] :=
 Module[{privArrayTest, invert, sum, privArrayC, tolerance},
  (*internal functions*)
  invert[x_] := x^-1;
  sum[x_] := Total[x];
  tolerance[x_] := {x - x*.05, x + x*.05};

  (*main body*)
  privArrayTest = Map[invert, N[Map[sum, Subsets[Map[invert, arrayInTest], {1, \[Infinity]}]]]];
  (*1,\[Infinity] prevents the first blank subset from being \generated*)

  privArrayC = Map[tolerance, arrayInCompare];
  Print[privArrayC]; (*Debugging Code*)
  Print["Debug code,privArrayTest", privArrayTest];(*Debugging code*)

  (*compare code, returns true false*)
  ContainsAll[privArrayTest, privArrayC]
  ]

Example input:

resistors = {1500, 1000, 2500, 20000};
equivalence = {472.441, 483.871, 895.522};

Example output:

{{448.819,496.063},{459.677,508.065},{850.746,940.298}}

Debug code,privArrayTest{1500.,1000.,2500.,20000.,600.,937.5,1395.35,714.286,952.381,2222.22,483.871,582.524,895.522,689.655,472.441}

True
$\endgroup$

1 Answer 1

2
$\begingroup$

ContainsAll checks whether expressions contain other expressions on a pattern matching level. It does not check containment in intervals.

Maybe this helps?

tol = 0.05;

possibleresistances = 1/Total[1/Rest@subsets, {2}];
A = Outer[
   {equiv, resistance} \[Function] Between[resistance, (1 + {-1, 1} tol) equiv],
   equivalence,
   possibleresistances
   ];

And @@ Or @@@ A

True

$\endgroup$
7
  • 1
    $\begingroup$ Some alternatives: 1. possibleresistances = HarmonicMean[#]/Length[#] & /@ subsets 2. the main testing function can be done as {equiv, resistance} \[Function] Between[resistance, (1 + {-1, 1} tol) equiv]. $\endgroup$ Mar 3, 2019 at 10:00
  • $\begingroup$ Hi @J.M. Good points. However, it appears to me that HarmonicMean is somewhat inefficient (which does matter in this scenario). $\endgroup$ Mar 3, 2019 at 10:12
  • $\begingroup$ Huh, that's interesting. What about 1/Total[1/subsets, {2}]? $\endgroup$ Mar 3, 2019 at 10:14
  • $\begingroup$ That's better! Thank you! $\endgroup$ Mar 3, 2019 at 10:34
  • $\begingroup$ So the main issue is I need the possible resistances contain each member of the equivalence list(+-5%) at least once for the possible resistances to be a successful list. The code I have works, albeit not the best written, except for looking through the ranges of acceptable tolerance. I was able to use ContainsAll when I matched the machine precision (without the tolerance), so I am looking for a replacement command, if one exists. $\endgroup$
    – Sc Bones
    Mar 3, 2019 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.