1

E24 直列抵抗と E12 および E6 直列コンデンサとインダクタの値を取得して、RC/RL 回路設計に役立つ Ruby スクリプトを設計しようとしています。F = 1 / ( 2 * Pi * R * C )基本的には、 [たとえばRC回路の]方程式との可能な組み合わせをそれぞれ循環するだけです。

私はここで効率を求めているのではなく、ただ信頼性を求めています。上記の式の値を使用して、抵抗アレイとコンデンサ/インダクタアレイのすべての組み合わせ(3つすべてではありません)を循環させる必要があり、特定の値のプラス/マイナスx%以内にある場合は、使用した配列。2 つの for ループをネストして各組み合わせを実行できますが、許容範囲内の値を返すだけでなく、最も近い (または正確な) 値を教えてほしいと考えています。

4

3 に答える 3

2

E24E12およびE6配列、CERTAIN_VALUEおよび desiredを定義した後DELTA、これは機能するはずです。

F = lambda{ |i, r, c| 1 / ( 2 * i * r * c ) }

equation_solver = lambda {
  |equation, resistances, capacitances, inductances, desired_value, delta|
  resistances.find.with_object [] do |resistance, memo|
    capacitances.find.with_object memo do |capacitance, memo|
      inductances = E6.find{ |inductance|
        ( equation.(inductance, resistance, capacitance) - desired_value ).abs < delta
      } and memo << resistance << capacitance << inductance
    end
  end
}

desired_r_c_i = equation_solver.( F, E24, E12, E6, CERTAIN_VALUE, DELTA )
于 2012-10-30T07:08:36.657 に答える
0
$resArray = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1,10, 11, 12, 13, 15, 16, 18, 20, 22, 24, 27, 30, 33, 36, 39, 43, 47, 51, 56, 62, 68, 75, 82, 91,100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270, 300, 330, 360, 390, 430, 470, 510, 560, 620, 680, 750, 820, 910,1000, 1100, 1200, 1300, 1500, 1600, 1800, 2000, 2200, 2400, 2700, 3000, 3300, 3600, 3900, 4300, 4700, 5100, 5600, 6200, 6800, 7500, 8200, 9100, 10000, 11000, 12000, 13000, 15000, 16000, 18000, 20000, 22000, 24000, 27000, 30000, 33000, 36000, 39000, 43000, 47000, 51000, 56000, 62000, 68000, 75000, 82000, 91000, 100000, 110000, 120000, 130000, 150000, 160000, 180000, 200000, 220000, 240000, 270000, 300000, 330000, 360000, 390000, 430000, 470000, 510000, 560000, 620000, 680000, 750000, 820000, 910000, 1000000, 1100000, 1200000, 1300000, 1500000, 1600000, 1800000, 2000000, 2200000, 2400000, 2700000, 3000000, 3300000, 3600000, 3900000, 4300000, 4700000, 5100000, 5600000, 6200000, 6800000, 7500000, 8200000, 9100000, 1000000]
$capArray = Array.new
$indArray = Array.new
$breakFreq = 19*(10**3)
$tol = 0.997
$PI = Math.atan2(0,-1)

def makeArray(j,arr)
    for i in 0...$resArray.size
        if i%j == 0
        arr << $resArray[i]*(10**-9)
        end
    end
end

def capTest()
puts "RC Circuit Estimation"
    for i in 0...$resArray.size
        for j in 0...$capArray.size
            n = 1/(2*$PI*$resArray[i]*$capArray[j])
            if n > $breakFreq*$tol && n < $breakFreq*(2-$tol)
                diff = 100 - ((n-$breakFreq)/$breakFreq).abs
                puts "Resistor:  #{$resArray[i]} Ohms"
                puts "Capacitor: #{$capArray[j].to_f} F"
                puts "Within #{diff}%"
                puts "---------------------"
            end
        end
    end
end

def indTest()
puts "RL Circuit Estimation"
    for i in 0...$resArray.size
        for j in 0...$indArray.size
            n = $resArray[i]/(2*$PI*$indArray[j])
            if n > $breakFreq*$tol && n < $breakFreq*(2-$tol)
                diff = (1 - ((n-$breakFreq)/$breakFreq).abs)*100
                puts "Resistor:  #{$resArray[i]} Ohms"
                puts "Inductor: #{$indArray[j].to_f} H"
                puts "Within #{diff}%"
                puts "---------------------"
            end
        end
    end
end

makeArray(2,$capArray)
makeArray(4,$indArray)
capTest()
indTest()
于 2012-10-30T08:19:15.403 に答える
0

これは、コードの軽度のリファクタリングです。

# I noticed that your resistance array is just the same set of values
# repeated with factors from 1 to 1000000, so this would be more concise
# way of defining it (and less prone to typing errors):
RES_ARRAY = [*-1..5].map{ |e| 10 ** e }.map{ |factor|
             [ 10, 11, 12, 13, 15, 16, 18, 20,
               22, 24, 27, 30, 33, 36, 39, 43,
               47, 51, 56, 62, 68, 75, 82, 91 ].map{ |e| e * factor }
           }.reduce( :+ )

# Global variables should be used very rarely. Constants should be written IN UPCASE:
CAP_ARRAY, IND_ARRAY = [], []
BREAK_FREQ = 19 * ( 10 ** 3 )

# Whole words should be used whenever possible in the code:
TOLERANCE = 0.997

# Methods should be named in snake_case:
def make_array( j, arr )
  # Let us use #each_slice and #map insterad of % and << operators:
  arr.each_slice( j ).map( &:first ).map{ |e| e * 1e-9 }
end

def cap_test
  puts "RC Circuit Estimation"
  for i in 0...RES_ARRAY.size
    for j in 0...CAP_ARRAY.size
      n = 1 / ( 2 * Math::PI * RES_ARRAY[ i ] * CAP_ARRAY[ j ] )
      if n > BREAK_FREQ * TOLERANCE and n < BREAK_FREQ * ( 2 - TOLERANCE )
        diff = 100 - ( ( n - BREAK_FREQ ) / BREAK_FREQ ).abs
        puts "Resistor:  #{RES_ARRAY[ i ]} Ohms"
        puts "Capacitor: #{CAP_ARRAY[ j ].to_f} F"
        puts "Within #{diff}%"
        puts "---------------------"
      end
    end
  end
end

def ind_test
  puts "RL Circuit Estimation"
  for i in 0...RES_ARRAY.size
    for j in 0...IND_ARRAY.size
      n = RES_ARRAY[ i ] / ( 2 * Math::PI * IND_ARRAY[ j ] )
      if n > BREAK_FREQ * TOLERANCE and n < BREAK_FREQ * ( 2 - TOLERANCE )
        diff = ( 1 - ( ( n - BREAK_FREQ ) / BREAK_FREQ ).abs ) * 100
        puts "Resistor:  #{RES_ARRAY[ i ]} Ohms"
        puts "Inductor: #{IND_ARRAY[ j ].to_f} H"
        puts "Within #{diff}%"
        puts "---------------------"
      end
    end
  end
end

make_array 2, CAP_ARRAY
make_array 4, IND_ARRAY
cap_Test()
ind_Test()
于 2012-10-30T09:07:41.500 に答える