Resistor mesh: Difference between revisions

(added Julia example)
Line 503:
 
=={{header|Julia}}==
We construct the matrix A that relates voltage v on each node to the injected current b via Av=b, and then we simply solve the linear system to find the resulting voltages (from unit currents at the indicated nodes, i and j) and hence the resistance. Because the grid of resistors is rectangular, we can construct A from a Kronecker productproducts ⊗ (<code>kron</code> in Julia) of "one-dimensional" A1A<sub>1</sub> matrices corresponding to a linear sequence of resistors. The A1A<sub>1</sub> matrices, in turn, are a product of difference matrices D: a matrix D that computes the net current flowing into each node (via the difference of currents flowing in and out) and a matrix –D<sup>T</sup> that computes the voltage differences between nodes, so that the product A1A<sub>1</sub>=–DD<sup>T</sup> computes the voltage differences, multiplies by the admittances (= 1) to get currents, and then computes the current differences to get the net current flowing in or out of each node.
<lang julia>N = 10
D = [ [1.0 zeros(1,N-2)]; diagm(ones(N-2),1) - eye(N-1) ]
Anonymous user