Created
December 2, 2020 19:54
-
-
Save pepijndevos/5800f07c436d3cb6d6dd5bc7d3aa5bcb to your computer and use it in GitHub Desktop.
Calculate node voltages from a netlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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