Resistance calculator: Difference between revisions
No edit summary |
|||
Line 169: | Line 169: | ||
@b.setVoltage @voltage |
@b.setVoltage @voltage |
||
build = ( |
build = (s) -> |
||
stack = [] |
stack = [] |
||
for word in s.split ' ' |
for word in s.split ' ' |
||
Line 175: | Line 175: | ||
else if word == '*' then stack.push new Parallel stack.pop(), stack.pop() |
else if word == '*' then stack.push new Parallel stack.pop(), stack.pop() |
||
else stack.push new Resistor parseFloat word |
else stack.push new Resistor parseFloat word |
||
stack.pop() |
|||
⚫ | |||
node |
|||
node = build |
node = build "10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +" |
||
⚫ | |||
print " Ohm Volt Ampere Watt Network tree" |
print " Ohm Volt Ampere Watt Network tree" |
||
node.report "" |
node.report "" |
Revision as of 12:57, 14 March 2019
Introduction
- Calculate the resistance of a network of resistors.
- The resistors can be connected in series or parallel.
- Use infix or RPN to state the network.
- Calculate resistance, voltage, current and power for every resistor and operation.
Background
- Serial Resistors: the sum of the resistors gives the equivalent resistor
- Parallel Resistors: the inverse of the sum of the inverse of the resistors
- The voltage drops over the resistors
- Current = Resistance / Voltage
- Power = Current * Voltage
Input
- Infix: ((((R8 + R10) * R9 + R7) * R6 + R5) * R4 + R3) * R2 + R1
- RPN: 10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +
Output
- Voltage = 18.0 V
- 10.000 ohms in the upper left corner is the equivalent resistance.
- The first operation is 10 + 2 = 12 which can be found in the three middle rows.
Ohm Volt Ampere Watt Network tree 10.000 18.000 1.800 32.400 + 4.000 7.200 1.800 12.960 | * 8.000 7.200 0.900 6.480 | | + 4.000 3.600 0.900 3.240 | | | * 8.000 3.600 0.450 1.620 | | | | + 4.000 1.800 0.450 0.810 | | | | | * 12.000 1.800 0.150 0.270 | | | | | | + 4.000 0.600 0.150 0.090 | | | | | | | * 12.000 0.600 0.050 0.030 | | | | | | | | + 10.000 0.500 0.050 0.025 | | | | | | | | | r 2.000 0.100 0.050 0.005 | | | | | | | | | r 6.000 0.600 0.100 0.060 | | | | | | | | r 8.000 1.200 0.150 0.180 | | | | | | | r 6.000 1.800 0.300 0.540 | | | | | | r 4.000 1.800 0.450 0.810 | | | | | r 8.000 3.600 0.450 1.620 | | | | r 4.000 3.600 0.900 3.240 | | | r 8.000 7.200 0.900 6.480 | | r 6.000 10.800 1.800 19.440 | r
Python
<lang python>
- RPN
class Resistor : def __init__(self, resistance, a=None, b=None, symbol='r'): self.resistance = resistance self.a = a self.b = b self.symbol = symbol def res(self) : return self.resistance def setVoltage(self, voltage): self.voltage = voltage def current(self) : return self.voltage / self.res() def effect(self) : return self.current() * self.voltage def report(self,level=""): print(f"{self.res():8.3f} {self.voltage:8.3f} {self.current():8.3f} {self.effect():8.3f} {level}{self.symbol}") if self.a: self.a.report(level + "| ") if self.b: self.b.report(level + "| ")
class Serial(Resistor) : def __init__(self, a, b) : super().__init__(0, b, a, '+') def res(self) : return self.a.res() + self.b.res() def setVoltage(self, voltage) : ra = self.a.res() rb = self.b.res() self.a.setVoltage(ra/(ra+rb) * voltage) self.b.setVoltage(rb/(ra+rb) * voltage) self.voltage = voltage
class Parallel(Resistor) : def __init__(self,a,b) : super().__init__(0, b, a, '*') def res(self) : return 1 / (1 / self.a.res() + 1 / self.b.res()) def setVoltage(self, voltage) : self.a.setVoltage(voltage) self.b.setVoltage(voltage) self.voltage = voltage
def build(s) : stack = [] for word in s.split(' '): if word == "+": stack.append(Serial(stack.pop(), stack.pop())) elif word == "*": stack.append(Parallel(stack.pop(), stack.pop())) else: stack.append(Resistor(float(word))) return stack.pop()
node = build("10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +") print(" Ohm Volt Ampere Watt Network tree") node.setVoltage(18.0) node.report() </lang>
Python
<lang python>
- Infix
class Resistor : def __init__(self, resistance, a=None, b=None, symbol='r') : self.resistance = resistance self.a = a self.b = b self.symbol = symbol def res(self) : return self.resistance def setVoltage(self, voltage) : self.voltage = voltage def current(self) : return self.voltage / self.res() def effect(self) : return self.current() * self.voltage def report(self,level="") : print(f"{self.res():8.3f} {self.voltage:8.3f} {self.current():8.3f} {self.effect():8.3f} {level}{self.symbol}") if self.a: self.a.report(level + "| ") if self.b: self.b.report(level + "| ") def __add__(self,other) : return Serial(self,other) def __mul__(self,other) : return Parallel(self,other)
class Serial(Resistor) : def __init__(self, a, b) : super().__init__(0, a, b, '+') def res(self) : return self.a.res() + self.b.res() def setVoltage(self, voltage) : ra = self.a.res() rb = self.b.res() self.a.setVoltage(ra/(ra+rb) * voltage) self.b.setVoltage(rb/(ra+rb) * voltage) self.voltage = voltage
class Parallel(Resistor) : def __init__(self,a,b) : super().__init__(0, a, b, '*') def res(self) : return 1 / (1 / self.a.res() + 1 / self.b.res()) def setVoltage(self, voltage): self.a.setVoltage(voltage) self.b.setVoltage(voltage) self.voltage = voltage
[R1,R2,R3,R4,R5,R6,R7,R8,R9,R10] = [Resistor(res) for res in [6,8,4,8,4,6,8,10,6,2]] node = ((((R8+R10) * R9 + R7) * R6 + R5) * R4 + R3) * R2 + R1 node.setVoltage(18) print(" Ohm Volt Ampere Watt Network tree") node.report() </lang>
CoffeeScript
<lang coffeescript> nd = (num) -> num.toFixed(3).padStart 8
class Resistor constructor : (@resistance,@a=null,@b=null,@symbol='r') -> res : -> @resistance setVoltage : (@voltage) -> current : -> @voltage / @res() effect : -> @current() * @voltage report : (level) -> print "#{nd @res()} #{nd @voltage} #{nd @current()} #{nd @effect()} #{level}#{@symbol}" if @a then @a.report level + "| " if @b then @b.report level + "| "
class Serial extends Resistor constructor : (a,b) -> super 0,a,b,'+' res : -> @a.res() + @b.res() setVoltage : (@voltage) -> ra = @a.res() rb = @b.res() @a.setVoltage ra/(ra+rb) * @voltage @b.setVoltage rb/(ra+rb) * @voltage
class Parallel extends Resistor constructor : (a,b) -> super 0,a,b,'*' res : -> 1 / (1 / @a.res() + 1 / @b.res()) setVoltage : (@voltage) -> @a.setVoltage @voltage @b.setVoltage @voltage
build = (s) -> stack = [] for word in s.split ' ' if word == '+' then stack.push new Serial stack.pop(), stack.pop() else if word == '*' then stack.push new Parallel stack.pop(), stack.pop() else stack.push new Resistor parseFloat word stack.pop()
node = build "10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +" node.setVoltage 18.0 print " Ohm Volt Ampere Watt Network tree" node.report "" </lang>