waterlubber

resistor tool

May 14th, 2020
1,644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3.  
  4. const int resistances[] = {0,10,22,47,100,150,200,220,270,330,470,510,680,1000,2000,2200,3300,4700,5100,6800,10000,20000,47000,51000,68000,100000,220000,300000,470000,680000,1000000};
  5. const int rLength = sizeof(resistances)/sizeof(*resistances);
  6. const int max = 500000;
  7. const int min = 10000;
  8. const float targetRatio =  28.33333333;
  9. float ratio = 0;
  10. float error = 0;
  11. float bestRatio = 0;
  12. float bestError = 999;
  13. int bestIndex[] = {0,0,0,0};
  14. int minIndex = 0;
  15. int maxIndex = rLength - 1;
  16. int r1 = 0;
  17. int r2 = 0;
  18.  
  19.  
  20. int main(int argc, char **argv)
  21. {
  22.     for(int i = 0; i<rLength; i++) {
  23.         if(resistances[i] >= min) {
  24.             minIndex = i;
  25.             break;
  26.         }
  27.     }
  28.     for(int i = 0; i<rLength; i++) {
  29.         if(resistances[i] <= max) {
  30.             maxIndex = i;
  31.         }
  32.     }
  33.     printf("Mininum: %d Maximum: %d\n",minIndex,maxIndex);
  34.     printf("Mininum R: %d Maximum R: %d\n",resistances[minIndex], resistances[maxIndex]);
  35.     //Naive Calculation
  36.     printf("Naïve Ratio Finding...");
  37.     for(int i = minIndex; i<=maxIndex; i++){
  38.         for(int j = minIndex; j <=maxIndex; j++) {
  39.             //printf("i:%d\tj:%d\n",resistances[i],resistances[j]);
  40.             ratio = float(resistances[j]) / float(resistances[i]);
  41.             //printf("%f\n",ratio);
  42.             error = std::fabs(ratio - targetRatio);
  43.             if(error < bestError) {
  44.                 bestError = error;
  45.                 bestIndex[0] = j;
  46.                 bestIndex[1] = i;
  47.                 bestRatio = ratio;
  48.             }
  49.         }
  50.     }
  51.     printf("Naïve Results:\nBest Error:\t%f\nBest Ratio:\t%f\nBest Resistances:\t%d Ω\t%d Ω\n\n",bestError,bestRatio,resistances[bestIndex[0]],resistances[bestIndex[1]]);
  52.     printf("Series Ratio Finding\n");
  53.     bestError = 999.0;
  54.     for(int i = 0; i<rLength; i++) {
  55.         for(int j = 0; j<rLength; j++) {
  56.             for(int k = 0; k<rLength; k++) {
  57.                 for(int l = 0; l<rLength; l++) {
  58.                     r1 = resistances[i] + resistances[j];
  59.                     r2 = resistances[k] + resistances[l];
  60.                     if(r1 >= min && r1 <= max && r2 >= min && r2 <= max){
  61.                         ratio = float(r1) / float(r2);
  62.                         //printf("r %f\n",ratio);
  63.                         //printf("%f\n",ratio);
  64.                         error = std::fabs(ratio - targetRatio);
  65.                         if(error < bestError) {
  66.                             bestError = error;
  67.                             printf("%f ",error);
  68.                             bestIndex[0] = i;
  69.                             bestIndex[1] = j;
  70.                             bestIndex[2] = k;
  71.                             bestIndex[3] = l;
  72.                             bestRatio = ratio;
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     printf("Series Results:\nBest Error:\t%f (%f%%)\nBest Ratio:\t%f\nBest Resistances:\t%d Ω\t%d Ω\t%d Ω\t%d Ω\n\n",bestError,100.0*bestError/targetRatio,bestRatio,resistances[bestIndex[0]],resistances[bestIndex[1]],resistances[bestIndex[2]],resistances[bestIndex[3]]);
  80.     printf("Parallel Ratio Finding\n");
  81.     float conductances[rLength] = {};
  82.     for(int i = 0; i<rLength; i++) {
  83.         conductances[i] = 1.0f / (float) resistances[i];
  84.         printf("%f ",conductances[i]);
  85.     }
  86.     printf("\n");
  87.     bestError = 999.0;
  88.     for(int i = 0; i<rLength; i++) {
  89.         for(int j = 0; j<rLength; j++) {
  90.             for(int k = 0; k<rLength; k++) {
  91.                 for(int l = 0; l<rLength; l++) {
  92.                     float c1 = conductances[i] + conductances[j];
  93.                     float c2 = conductances[k] + conductances[l];
  94.                     r1 = (int) round(1.0 / c1);
  95.                     r2 = (int) round(1.0 / c2);
  96.                     if(r1 >= min && r1 <= max && r2 >= min && r2 <= max){
  97.                         ratio = float(r1) / float(r2);
  98.                         //printf("r %f\n",ratio);
  99.                         //printf("%f\n",ratio);
  100.                         error = std::fabs(ratio - targetRatio);
  101.                         if(error < bestError) {
  102.                             bestError = error;
  103.                             bestIndex[0] = i;
  104.                             bestIndex[1] = j;
  105.                             bestIndex[2] = k;
  106.                             bestIndex[3] = l;
  107.                             bestRatio = ratio;
  108.                         }
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.     }
  114.     printf("Parallel Results:\nBest Error:\t%f (%f%%)\nBest Ratio:\t%f\nBest Resistances:\t%d Ω\t%d Ω\t%d Ω\t%d Ω\n\n",bestError,100.0*bestError/targetRatio,bestRatio,resistances[bestIndex[0]],resistances[bestIndex[1]],resistances[bestIndex[2]],resistances[bestIndex[3]]);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment