Author Topic: MATLAB script, Minimum error resistor pairs for a given ratio (0.1%, E192)  (Read 2760 times)

0 Members and 1 Guest are viewing this topic.

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey
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_qT2Zov00a

In 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
« Last Edit: April 19, 2018, 11:23:44 am by palpurul »
 
The following users thanked this post: 0xfede, Fire Doger

Offline Fire Doger

  • Regular Contributor
  • *
  • Posts: 207
  • Country: 00
  • Stefanos
 :-+
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 here http://jansson.us/resistors.html
 

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey
:-+
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 here http://jansson.us/resistors.html

Thanks :D.
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  :palm:.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: us
Nice script!

I am slowly working my way into Matlab and useful examples are always helpful.

Thanks for sharing!
 
The following users thanked this post: palpurul

Offline slugrustle

  • Frequent Contributor
  • **
  • Posts: 278
  • Country: us
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.
« Last Edit: April 21, 2018, 03:36:03 am by slugrustle »
 

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey
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.

That'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?
 

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey
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
« Last Edit: April 21, 2018, 11:17:37 am by palpurul »
 

Offline slugrustle

  • Frequent Contributor
  • **
  • Posts: 278
  • Country: us
Quote
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?

Yes. 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
 
The following users thanked this post: palpurul

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey
Quote
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?

Yes. 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
Thanks! I'll give this a try.
 

Offline RandallMcRee

  • Frequent Contributor
  • **
  • Posts: 541
  • Country: us

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  |O


 
The following users thanked this post: palpurul

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey

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  |O

Great suggestion!
 

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey
Quote
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?

Yes. 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

For some reason my script runs really really slow on GNU octave. Did that ever happen to you?
 

Offline slugrustle

  • Frequent Contributor
  • **
  • Posts: 278
  • Country: us
Quote
For some reason my script runs really really slow on GNU octave. Did that ever happen to you?

It 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 clear

I 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: [Select]
for i = 1:SizeofSVL
    for j = 1:SizeofSVL
        ...
    end
end

MATLAB 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_compilation

There 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...
 
The following users thanked this post: palpurul

Offline slugrustle

  • Frequent Contributor
  • **
  • Posts: 278
  • Country: us
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 E24
10 do not belong to E192, E96, or E24.

Here are the values for the last category:
25 ohms
50 ohms
400 ohms
2500 ohms
5000 ohms
25000 ohms
40000 ohms
50000 ohms
80000 ohms
90000 ohms

So 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 datasheet
http://www.yageo.com/documents/recent/PYu-RT_1-to-0.01_RoHS_L_9.pdf
lists 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 datasheet
https://industrial.panasonic.com/cdbs/www-data/pdf/RDM0000/AOA0000C307.pdf
lists 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.
 

Offline Kirr

  • Regular Contributor
  • *
  • Posts: 66
  • Country: jp
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 E24
10 do not belong to E192, E96, or E24.

Here are the values for the last category:
25 ohms
50 ohms
400 ohms
2500 ohms
5000 ohms
25000 ohms
40000 ohms
50000 ohms
80000 ohms
90000 ohms
Very interesting! Is it possible to get similar stats for Mouser's stock?

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey

There 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.
Yep. 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.

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 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.

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.

That's a good way to reduce unnecessary computation. I might eventually modify my script and add this feature.

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...

Thanks!  :-+
 

Offline palpurulTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: tr
  • Hey
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.

 How do you do that? :o I am not really a programming guy, but that's really useful and time-saving.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf