Skip to content

Instantly share code, notes, and snippets.

@pepijndevos
Created December 2, 2020 19:54
Show Gist options
  • Save pepijndevos/5800f07c436d3cb6d6dd5bc7d3aa5bcb to your computer and use it in GitHub Desktop.
Save pepijndevos/5800f07c436d3cb6d6dd5bc7d3aa5bcb to your computer and use it in GitHub Desktop.
Calculate node voltages from a netlist
using LinearAlgebra
@enum Element Resistor Voltage Current
netlist = [
(0, 1, Resistor, 4),
(0, 1, Current, 6),
(0, 2, Resistor, 6),
(0, 3, Resistor, 8),
(1, 2, Resistor, 3),
(1, 3, Resistor, 6),
(1, 3, Current, 4),
(2, 3, Resistor, 5),
]
dim = 3
admitance = zeros(dim, dim)
current = zeros(dim)
for (n1, n2, typ, val) in netlist
if typ == Resistor
if n1 > 0
admitance[n1, n1] += 1/val
end
if n2 > 0
admitance[n2, n2] += 1/val
end
if n1 > 0 && n2 > 0
admitance[n1, n2] -= 1/val
admitance[n2, n1] -= 1/val
end
elseif typ == Current
if n1 > 0
current[n1] -= val
end
if n2 > 0
current[n2] += val
end
end
end
println(admitance)
println(current)
voltages = inv(admitance)*current
println(voltages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment