MATLAB script, Minimum error resistor pairs for a given ratio (0.1%, E192) - Page 1
Forum section: electronics / projects

author: palpurul
author_rank: Regular Contributor
timestamp: April 19, 2018, 10:57:42 am
content: Hello,I created a MATLAB script for selecting minimum error resistor pairs for a given ratio. It can be useful when picking gain resistors or building a voltage divider. You can change the the "Ratio" variable to the desired value and find the closest match resistor pairs. It displays 10 resistor pairs, listing the ratio error in ascending order. It looks something like this:https://drive.google.com/open?id=1ITMoyS-oTdA-PYDcwB1EHd_qT2Zov00aIn this picture desired ratio is 19 and it shows 10 best resistor pairs for this ratio. I thought it was important to have a bunch of resistor pair values because you can't always find best pair even though they are standart values. You can also obtain standart values by dividing or multiply resistor values by powers of 10.To run this make sure you have BestRatioList.m and StandartValueList.m (they are contained in the Gain Resistor Finder.rar file) in the same folder.Thanks. I hope this can be useful for someone someday.Link to the script files :https://drive.google.com/open?id=19sN2137wGZUFgYaEU0enxT2JvzM88_Ni

author: Fire Doger
author_rank: Regular Contributor
timestamp: April 19, 2018, 11:51:44 am
content: It's very useful, especially for feedback.I use electrodroid (Android app) which has E6-E192 and displays also combinations of resistors in series or parallel but only the most accurate combination.Similar herehttp://jansson.us/resistors.html

author: palpurul
author_rank: Regular Contributor
timestamp: April 19, 2018, 12:25:33 pm
content: Quote from: Fire Doger on April 19, 2018, 11:51:44 amThanks.There are similar things to my script on the internet. I just wanted make my own I guess and usually these tools didn't include 0.1% resistors and only shows the most accurate ones.Paralel series combination seems like a good idea. I built this script to find resistor values for 20 gain in non-inverting configuration. The best I can get is 0.12%. This is less than the resistors' tolerance.

author: rstofer
author_rank: Super Contributor
timestamp: April 19, 2018, 02:34:52 pm
content: Nice script!I am slowly working my way into Matlab and useful examples are always helpful.Thanks for sharing!

author: slugrustle
author_rank: Frequent Contributor
timestamp: April 21, 2018, 03:24:16 am
content: This is neat. One thing you can try is using resistor values from multiple series. For example, 30k from E24 and 1.58k from E96 gives an 18.98734 ratio, so 0.0662% error. Even better, both values are in stock when I checked a distributor.74.1k from E192 and 3.9k from E24 gives exactly a ratio of 19, but I can't find 74.1k in stock. From a brief search, it looks like E192 values tend to be non-stocked. My guess is that large manufacturers can order reels of these values if they're willing to wait the required lead time.In my experience, E96 and E24 values tend to be well stocked.Edit: wrote the attached script to test the multiple series thing. It stores the output in the variable results, with the best answers in the first rows; no nice formatting like palpurul's program. I only tested it in GNU Octave, as I don't have a MATLAB license.

author: palpurul
author_rank: Regular Contributor
timestamp: April 21, 2018, 08:35:41 am
content: Quote from: slugrustle on April 21, 2018, 03:24:16 amThat's a really useful script. It never appeared to me to use resistors from different tolerance bands. As you said 74.1k (0.1% resistor) and 3.9k (1% resistor) give the exact ratio for 19. It's difficult to find what you want from E192 series, I usually have to go through the list of resistor pairs that the script gives to find available stock.This is the first time I heard of GNU octave. I tested your script in MATLAB and it worked fine. Are they usually compatible like this?

author: palpurul
author_rank: Regular Contributor
timestamp: April 21, 2018, 11:08:30 am
content: Since my first script gave me 0.12% gain error, I made some modifications to my original script (re-wrote the whole thing). The new script finds two feedback resistors (Rf1 and Rf2) to be used in parallel configuration and a single gain (Rg) resistor.This is the results for 19.I couldn't find any stock for resistor values for the first four results in Digikey, but I found available resistors for the fifth result (highlighted) which has very little error.The script can be downloaded here (Beware!! It is a mess. I added some comments, but it is still difficult to read, but it works) :https://drive.google.com/open?id=1rClRNJuxvb4Dts9YFDECwiaMFLEvjjjF

author: slugrustle
author_rank: Frequent Contributor
timestamp: April 21, 2018, 02:04:00 pm
content: QuoteYes. Octave was designed to be compatible with MATLAB, although it does allow some syntax that MATLAB doesn't.https://www.gnu.org/software/octave/https://en.wikibooks.org/wiki/MATLAB_Programming/Differences_between_Octave_and_MATLAB

author: palpurul
author_rank: Regular Contributor
timestamp: April 21, 2018, 02:53:21 pm
content: Quote from: slugrustle on April 21, 2018, 02:04:00 pmThanks! I'll give this a try.

author: RandallMcRee
author_rank: Frequent Contributor
timestamp: April 21, 2018, 03:29:25 pm
content: Important caveat: solder resistors THEN measure their value for precise matching. *After^ allowing them to come to room temp!Soldering (any temperature hysteresis) can change values in amount that will affect your matching.Speaking from experience

author: palpurul
author_rank: Regular Contributor
timestamp: April 21, 2018, 04:07:01 pm
content: Quote from: RandallMcRee on April 21, 2018, 03:29:25 pmGreat suggestion!

author: palpurul
author_rank: Regular Contributor
timestamp: April 21, 2018, 08:42:10 pm
content: Quote from: slugrustle on April 21, 2018, 02:04:00 pmFor some reason my script runs really really slow on GNU octave. Did that ever happen to you?

author: slugrustle
author_rank: Frequent Contributor
timestamp: April 22, 2018, 06:36:46 pm
content: QuoteIt has. Writing data processing code in Octave was a big part of my last job; I ended up using the profiler a lot to optimize things.>> profile on; your_program; profile off>> profshow>> profexplore>> profile clearI put some tic and toc statements in the first program you posted and found that the following loop takes most of the time (1.1 seconds on my PC):Code:for i = 1:SizeofSVLfor j = 1:SizeofSVL...endendMATLAB has a Just In Time (JIT) compiler that probably optimizes your inner loop here. Octave does not have this feature yet.https://en.wikipedia.org/wiki/Just-in-time_compilationThere is a more efficient way to write the two-resistor ratio finder. For each resistor value in your list, treat it as the lower resistor. Multiply this lower resistor value by the desired ratio to get the "optimal" upper resistor value. Find the real resistor value in your list that is closest to the "optimal" value and compute its actual ratio relative to the lower resistor.When I wrote ratio.m, I also repeated this process, treating each resistor value as an upper resistor, dividing by the desired ratio, and finding the closest lower resistor. I don't know if you need to do this or not. I did it because my list of resistor values included all the x1, x10, x100, x1000, x10,000, x100,000, x1,000,000 multiplied resistor values.I also limited the search range based on the ratio. For example, I didn't test 523k or larger as the lower resistor with a desired ratio of 19, because the largest possible resistor was 9.88M, and 9.88M / 19 is 520k. Similarly, I didn't test 18.9 ohm or smaller as an upper resistor for desired ratio = 19, because 1 ohm is the lowest value in my list.The attached ratio.m below has comments, runs as a function, and checks user input, as it should have done earlier. I'm too lazy to put a nice header comment on it right now...

author: slugrustle
author_rank: Frequent Contributor
timestamp: April 22, 2018, 11:48:47 pm
content: Your difficulty in finding stocked E192 values led me to seek out statistics on which resistor series are typically stocked. I went on DigiKey and used some JavaScript to pull down all stocked values for 0.1% surface mount resistors in 0603 or 0805 case size with +/-10, 15, or 25ppm temperature coefficient. I wrote a little MATLAB / Octave script to figure out how many of these values belong to each resistor series. Here is the output of the script:There are 665 unique 0.1% resistor values in stock.Of these, 138 belong to the E24 series,480 belong to E96 and not E24,37 belong to E192 and not E96 or E2410 do not belong to E192, E96, or E24.Here are the values for the last category:25 ohms50 ohms400 ohms2500 ohms5000 ohms25000 ohms40000 ohms50000 ohms80000 ohms90000 ohmsSo that's interesting. Most of the 0.1% resistors I've purchased came from Yageo's RT series or Panasonic's ERA3A series. Yageo's RT datasheethttp://www.yageo.com/documents/recent/PYu-RT_1-to-0.01_RoHS_L_9.pdflists E24 and E96 series resistance ranges for ordering on page 5, and footnote #2 states that E192 series values are available on request. Similarly, Panasonic's ERA datasheethttps://industrial.panasonic.com/cdbs/www-data/pdf/RDM0000/AOA0000C307.pdflists E24 and E96 series resistance ranges for ordering on page 2, and footnote #3 says E192 series values are available on request. That might explain why they're not typically stocked.

author: Kirr
author_rank: Regular Contributor
timestamp: April 24, 2018, 01:01:10 am
content: Quote from: slugrustle on April 22, 2018, 11:48:47 pmVery interesting! Is it possible to get similar stats for Mouser's stock?

author: palpurul
author_rank: Regular Contributor
timestamp: April 24, 2018, 06:33:04 am
content: Quote from: slugrustle on April 22, 2018, 06:36:46 pmYep. That's I basically did in my first script. I did something similar in the parallel resistor finder script, but I think it's more complex than it should be. I first calculate all parallel combinations in 192x192 matrix (only half of it contains values) and normalize it to compare them with the standart values. I also normalize the multiplied values of the standart resistors (MultipRes). After that, I compare MultipRes with the parallel combination. For each parallel combination I find the closest match in the multiplied resistor (MultipRes). I calculate the difference between the parallel combination values and MultipRes and I find the minimum differences and calculate the errors.Quote from: slugrustle on April 22, 2018, 06:36:46 pmI didn't include the values multiplied by the powers of 10 because I figured I can just multiply each value by 10 after depending on my load condition.Quote from: slugrustle on April 22, 2018, 06:36:46 pmThat's a good way to reduce unnecessary computation. I might eventually modify my script and add this feature.Quote from: slugrustle on April 22, 2018, 06:36:46 pmThanks!

author: palpurul
author_rank: Regular Contributor
timestamp: April 24, 2018, 06:37:04 am
content: Quote from: slugrustle on April 22, 2018, 11:48:47 pmHow do you do that?I am not really a programming guy, but that's really useful and time-saving.