Resistance calculator

From Rosetta Code

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

Parallel Resistor Calculator

  • Infix: ((((R8 + R10) * R9 + R7) * R6 + R5) * R4 + R3) * R2 + R1
  • RPN: 10 2 + 6 * 8 + 6 * 4 + 8 * 4 + 8 * 6 +
  • Voltage = 18.0 V

Output

  • 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 RPN

<lang python> 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 Infix

<lang python> 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 RPN

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

Nim Infix

<lang python> import strutils,strformat,sugar,sequtils

type Resistor = ref object of RootObj symbol : char resistance,voltage : float a,b : Resistor

proc res(r:Resistor) : float = if r.symbol == '+': return r.a.res + r.b.res if r.symbol == '*': return 1 / (1 / r.a.res + 1 / r.b.res) r.resistance proc setVoltage(r:Resistor, voltage:float) = if r.symbol == '+': let ra = r.a.res let rb = r.b.res r.a.setVoltage ra/(ra+rb) * voltage r.b.setVoltage rb/(ra+rb) * voltage if r.symbol == '*': r.a.setVoltage voltage r.b.setVoltage voltage r.voltage = voltage proc current(r:Resistor) : auto = return r.voltage / r.res proc effect(r:Resistor) : auto = return r.current * r.voltage proc report(r:Resistor,level:string="") = echo fmt"{r.res:8.3f} {r.voltage:8.3f} {r.current:8.3f} {r.effect:8.3f} {level}{r.symbol}" if r.a!=nil: r.a.report level & "| " if r.b!=nil: r.b.report level & "| " proc `+`(r:Resistor,other:Resistor) : auto = return Resistor(symbol:'+', a:r, b:other) proc `*`(r:Resistor,other:Resistor) : auto = return Resistor(symbol:'*', a:r, b:other)

var R0,R1,R2,R3,R4,R5,R6,R7,R8,R9,R10 : Resistor let resistors = [6,8,4,8,4,6,8,10,6,2].map(res => Resistor(symbol:'r',resistance:res.float)) (R1,R2,R3,R4,R5,R6,R7,R8,R9,R10) = resistors let node = ((((R8 + R10) * R9 + R7) * R6 + R5) * R4 + R3) * R2 + R1 node.setVoltage(18) echo(" Ohm Volt Ampere Watt Network tree") node.report() </lang>