Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cmath>
- 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};
- const int rLength = sizeof(resistances)/sizeof(*resistances);
- const int max = 500000;
- const int min = 10000;
- const float targetRatio = 28.33333333;
- float ratio = 0;
- float error = 0;
- float bestRatio = 0;
- float bestError = 999;
- int bestIndex[] = {0,0,0,0};
- int minIndex = 0;
- int maxIndex = rLength - 1;
- int r1 = 0;
- int r2 = 0;
- int main(int argc, char **argv)
- {
- for(int i = 0; i<rLength; i++) {
- if(resistances[i] >= min) {
- minIndex = i;
- break;
- }
- }
- for(int i = 0; i<rLength; i++) {
- if(resistances[i] <= max) {
- maxIndex = i;
- }
- }
- printf("Mininum: %d Maximum: %d\n",minIndex,maxIndex);
- printf("Mininum R: %d Maximum R: %d\n",resistances[minIndex], resistances[maxIndex]);
- //Naive Calculation
- printf("Naïve Ratio Finding...");
- for(int i = minIndex; i<=maxIndex; i++){
- for(int j = minIndex; j <=maxIndex; j++) {
- //printf("i:%d\tj:%d\n",resistances[i],resistances[j]);
- ratio = float(resistances[j]) / float(resistances[i]);
- //printf("%f\n",ratio);
- error = std::fabs(ratio - targetRatio);
- if(error < bestError) {
- bestError = error;
- bestIndex[0] = j;
- bestIndex[1] = i;
- bestRatio = ratio;
- }
- }
- }
- 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]]);
- printf("Series Ratio Finding\n");
- bestError = 999.0;
- for(int i = 0; i<rLength; i++) {
- for(int j = 0; j<rLength; j++) {
- for(int k = 0; k<rLength; k++) {
- for(int l = 0; l<rLength; l++) {
- r1 = resistances[i] + resistances[j];
- r2 = resistances[k] + resistances[l];
- if(r1 >= min && r1 <= max && r2 >= min && r2 <= max){
- ratio = float(r1) / float(r2);
- //printf("r %f\n",ratio);
- //printf("%f\n",ratio);
- error = std::fabs(ratio - targetRatio);
- if(error < bestError) {
- bestError = error;
- printf("%f ",error);
- bestIndex[0] = i;
- bestIndex[1] = j;
- bestIndex[2] = k;
- bestIndex[3] = l;
- bestRatio = ratio;
- }
- }
- }
- }
- }
- }
- 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]]);
- printf("Parallel Ratio Finding\n");
- float conductances[rLength] = {};
- for(int i = 0; i<rLength; i++) {
- conductances[i] = 1.0f / (float) resistances[i];
- printf("%f ",conductances[i]);
- }
- printf("\n");
- bestError = 999.0;
- for(int i = 0; i<rLength; i++) {
- for(int j = 0; j<rLength; j++) {
- for(int k = 0; k<rLength; k++) {
- for(int l = 0; l<rLength; l++) {
- float c1 = conductances[i] + conductances[j];
- float c2 = conductances[k] + conductances[l];
- r1 = (int) round(1.0 / c1);
- r2 = (int) round(1.0 / c2);
- if(r1 >= min && r1 <= max && r2 >= min && r2 <= max){
- ratio = float(r1) / float(r2);
- //printf("r %f\n",ratio);
- //printf("%f\n",ratio);
- error = std::fabs(ratio - targetRatio);
- if(error < bestError) {
- bestError = error;
- bestIndex[0] = i;
- bestIndex[1] = j;
- bestIndex[2] = k;
- bestIndex[3] = l;
- bestRatio = ratio;
- }
- }
- }
- }
- }
- }
- 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]]);
- }
Advertisement
Add Comment
Please, Sign In to add comment