# Goal optimization: finding the best combination of capacitors and resistors resulting in given properties

I want to edit @MarcoB answers (because the code he gave doesn't answer the question appropriately)->(the function that need to be optimised is not implemented as asked)

But it seems like I am having some trouble (again) with his code. I made some modification (since I want it to be clear for others who might need something like this) and it look like this

resistors =
Flatten@Outer[Times,
PowerRange[1, 10000], {100, 110, 120, 130, 150, 160, 180, 200,
220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680,
750, 820, 910}];

capacitors =
Flatten@Outer[Times,
PowerRange[1/10, 100000000], {10, 15, 22, 33, 47, 68}];

combinations = {#1 #2 #3^2, 2 #2  #3} -> {#1, #2, #3} & @@@
Tuples[{capacitors, capacitors, resistors}];

{#1, #2, #3} -> {#1 #2 #3^2, 2 #2 #3} & @@@
DeleteDuplicates[Nearest[combinations, {384400, 1240}, {5, 1500}]]

this is all in one cell (as per @MarcoB suggestion -> so I don't get error with the memory, if I remembered right) but it doesn't give me any answer.

Even though I specifically chose {384400, 1240} which is the result I've got from this (same ?) code:

resistors =
Flatten@Outer[Times,
PowerRange[1, 10000], {100, 110, 120, 130, 150, 160, 180, 200,
220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680,
750, 820, 910}];

capacitors =
Flatten@Outer[Times,
PowerRange[1/10, 100000000], {10, 15, 22, 33, 47, 68}];

combinations = {#1 #2 #3^2, 2 #2  #3} -> {#1, #2, #3} & @@@
Tuples[{capacitors, capacitors, resistors}];

combinations[[20]]

(*Out:
{384400, 1240} -> {1, 1, 620}
*)

Notice anything different ?

What am I doing wrong ?

## Original Question :

I am in the early process of optimizing and simplifying my work load, but I can't seem to find the function I need to do the job.

Here is a description of my problem (as I currently have it set up in Excel):

• I have a list of resistors and a list of capacitors (all sorted, in separate columns); I also have two values; $a_{target}$ and $b_{target}$ which are real numbers.

• I want to obtain a few possible combinations of two capacitors and a resistor resulting in actual $a$ and $b$ values (calculated from the formula below) that best approximate my desired values.

$$f_{(C_1,C_2,R)} = \begin{cases} a=C_1C_2R^2\\ b=2 RC_2 \end{cases}$$

• It is likely that I won't be able to get to the exact prescribed $a_{target}$ and $b_{target}$, but I want to get as close to it as possible with the material I've got.
• The output of the program I am trying to design are the values of capacitors and resistor used to get to this optimum value.

I am looking to optimize some quantity, but I would like to get more than one answer, not only one as with e.g. Minimize or FindMinimum, as those will only give back a single minimum.

I also tried to use FindFit, but I honestly don't think it is made for that either since I am not trying to fit a function to some data (or am I?).

I'm already able to get the data from the excel spread sheet.

Any help on this?

Thanks

• Sebastien, I took the liberty to edit your question to try and clarify the problem you are trying to solve. My edits were quite extensive, so please review this new form and make sure that it actually still represents your original question! – MarcoB Jan 28 '16 at 6:05
• Also: 1) you mention that you are already able to get these values from Excel. Do you already have an automated approach that works there? If so, please include it in the question, as it would go a long way towards adapting your problem to Mathematica. 2) How many different values do you have in your lists of $C$ and $R$? – MarcoB Jan 28 '16 at 6:06
• Sebastien, I wonder if something went wrong in the data import step. The first error message you mention seems to suggest to me that your list of values contains data in non numerical format, e.g. as strings. Of course then the comparisons attempted by Nearest would fail. Can you check that you actually have numbers in your list? For instance try NumberQ /@ resistors and NumberQ /@ capacitors. If any of the results return False, then there's a problem (perhaps in the way those numbers were stored in Excel?) – MarcoB Jan 28 '16 at 19:32
• @MarcoB I will do this imediately, but did you edit your answer, or someone else did ! either way. I copied your code and couldn't make it work. All went well until the las cell {#1, #2, #3} -> {#1 #2 #3^2, 2 #3 #2} & @@@ DeleteDuplicates[ Nearest[combinations, {147000, 1300}, {5, 1500}] ] it didn't return something good (if i may say so) – Sebastien Comtois Jan 28 '16 at 19:37
• I had posted a picture (in your post) of what was happening with the last line – Sebastien Comtois Jan 28 '16 at 19:38

Assume that you have your lists of unique resistor values (resistors) and of unique capacitor values (capacitors). For now, I generate two such lists as follows:

resistors = Flatten@
Outer[
Times,
PowerRange[1, 10000],
{100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330,
360, 390, 430, 470, 510, 560, 620, 680, 750, 820, 910}
]; (* 120 unique values, in ohm *)

capacitors = Flatten@
Outer[
Times,
PowerRange[1/10, 100000000],
{10, 15, 22, 33, 47, 68}
]; (* 60 unique values, in picoFarad *)

Then I generate all possible triplets of (resistor $R$, capacitor $C_1$, capacitor $C_2$) values, considering that a capacitor value can be used twice in a triplet, and calculate the associated $a,b$ values for each triplet:

combinations = {#1 #2 #3^2, 2 #3 #2} -> {#1, #2, #3} & @@@
Tuples[{resistors, capacitors, capacitors}];

Here is an example of such a data point:

combinations[[20]]
(* Out: {225000000, 3000} -> {100, 1, 1500} *)

The list contains $120\times60\times60=432,000$ combinations.

We can then use Nearest to find one or more sets of $(a,b)$ values closest to the target ones we want; let's say that (arbitrarily) we want the 5 combinations or $R, C_1, C_2$ that generate $a\simeq150000$ and $b\simeq250$, within a search radius of $1400$ units. We could then write:

{#1, #2, #3} -> {#1 #2 #3^2, 2 #3 #2} & @@@
DeleteDuplicates[
Nearest[combinations, {147000, 1300}, {5, 1500}]
]

(* Out:
{{200,    68, 33/10} -> {148104,     2244/5 },
{470,  34/5, 34/5 } -> {3694576/25, 2312/25},
{680, 47/10, 34/5 } -> {3694576/25, 1598/25}}
*)

Nearest finds the optimum combinations; if duplicates are returned, they are eliminated by DeleteDuplicates. Each result is re-organized in the form of a rule, which could be read as "these three $R, C_1, C_2$ values generate the following actual $a,b$ values". As you can see, in this case only three distinct combinations were found given the indicated tolerance.

• That's pretty neat, thanks, and I love the explanation. But I don't understand where the b value is in Nearest ? either can I find your 1400 units radius ? – Sebastien Comtois Jan 28 '16 at 14:50
• And I would relly appreciate if you could edit this next answer for me, so I could better understand the way you thinked this throug. And also for personal learning issue with those new sign: give me a minute, I'll try to write as much as I understand -> so you can correct me and help me with this. (because this is a simple problem that I need to understand so I can move on to coding thist on my own) – Sebastien Comtois Jan 28 '16 at 14:54
• This code is not working on my computer – Sebastien Comtois Jan 28 '16 at 18:12
• get these error : Nearest::near1: combinations is neither a list of real points nor a valid list of rules. – Sebastien Comtois Jan 28 '16 at 18:13
• Function::slotn: "Slot number 3 in {#1,#2,#3}->{#1\ #2\ #3^2,2\ #3\ #2}& cannot be filled from ({#1,#2,#3}->{#1\ #2\ #3^2,2\ #3\ #2}&)[147000,1300]." – Sebastien Comtois Jan 28 '16 at 18:13