Resistor mesh: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added link)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 3 users not shown)
Line 8:
 
;See also:
*   (humor, nerd sniping)   [http://xkcd.com/356/ xkcd.com cartoon] (you can solve that for extra credits)
*   [https://www.paulinternet.nl/?page=resistors An article on how to calculate this and an implementation in Mathematica]
<br><br>
 
 
=={{header|11l}}==
Line 1,181 ⟶ 1,182:
{{out}}
<pre>R = 1.608991241729889</pre>
 
 
=={{header|JavaScript}}==
Kirchhoff's circuit laws on the resistor mesh are represented as a linear
equation system for the electric potential at each node of the grid.
The linear equation is then solved using the [https://en.wikipedia.org/wiki/Conjugate_gradient_method conjugate gradient method]:
<syntaxhighlight lang=JavaScript>
// Vector addition, scalar multiplication & dot product:
const add = (u, v) => {let i = u.length; while(i--) u[i] += v[i]; return u;};
const sub = (u, v) => {let i = u.length; while(i--) u[i] -= v[i]; return u;};
const mul = (a, u) => {let i = u.length; while(i--) u[i] *= a; return u;};
const dot = (u, v) => {let s = 0, i = u.length; while(i--) s += u[i]*v[i]; return s;};
 
const W = 10, H = 10, A = 11, B = 67;
 
function getAdjacent(node){ // Adjacency lists for square grid
let list = [], x = node % W, y = Math.floor(node / W);
if (x > 0) list.push(node - 1);
if (y > 0) list.push(node - W);
if (x < W - 1) list.push(node + 1);
if (y < H - 1) list.push(node + W);
return list;
}
 
function linOp(u){ // LHS of the linear equation
let v = new Float64Array(W * H);
for(let i = 0; i < v.length; i++){
if ( i === A || i === B ) {
v[i] = u[i];
continue;
}
// For each node other then A, B calculate the net current flow:
for(let j of getAdjacent(i)){
v[i] += (j === A || j === B) ? u[i] : u[i] - u[j];
}
}
return v;
}
 
function getRHS(phiA = 1, phiB = 0){ // RHS of the linear equation
let b = new Float64Array(W * H);
// Setting boundary conditions (electric potential at A and B):
b[A] = phiA;
b[B] = phiB;
for(let j of getAdjacent(A)) b[j] = phiA;
for(let j of getAdjacent(B)) b[j] = phiB;
return b;
}
 
function init(phiA = 1, phiB = 0){ // initialize unknown vector
let u = new Float64Array(W * H);
u[A] = phiA;
u[B] = phiB;
return u;
}
 
function solveLinearSystem(err = 1e-20){ // conjugate gradient solver
 
let b = getRHS();
let u = init();
let r = sub(linOp(u), b);
let p = r;
let e = dot(r,r);
 
while(true){
let Ap = linOp(p);
let alpha = e / dot(p, Ap);
u = sub(u, mul(alpha, p.slice()));
r = sub(linOp(u), b);
let e_new = dot(r,r);
let beta = e_new / e;
 
if(e_new < err) return u;
 
e = e_new;
p = add(r, mul(beta, p));
}
}
 
function getResistance(u){
let curr = 0;
for(let j of getAdjacent(A)) curr += u[A] - u[j];
return 1 / curr;
}
 
let phi = solveLinearSystem();
let res = getResistance(phi);
console.log(`R = ${res} Ohm`);
</syntaxhighlight>
{{out}}
<pre>R = 1.608991241730955 Ohm</pre>
 
=={{header|jq}}==
'''Works with jq and gojq, that is, the C and Go implementations of jq.'''
 
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
# Create a $rows * $columns matrix initialized with the input value
def matrix($rows; $columns):
. as $in
| [range(0;$columns)|$in] as $row
| [range(0;$rows)|$row];
 
def Node($v; $fixed):
{$v, $fixed};
 
# input: a suitable matrix of Nodes
def setBoundary:
.[1][1].v = 1
| .[1][1].fixed = 1
| .[6][7].v = -1
| .[6][7].fixed = -1 ;
 
# input: {d, m} where
# .d and .m are matrices (as produced by matrix(h; w)) of Nodes
# output: {d, m, diff} with d updated
def calcDiff($w; $h):
def adjust($cond; action): if $cond then action | .n += 1 else . end;
 
reduce range(0; $h) as $i (.diff = 0;
reduce range(0; $w) as $j (.;
.v = 0
| .n = 0
| adjust($i > 0; .v += .m[$i-1][$j].v)
| adjust($j > 0; .v += .m[$i][$j-1].v)
| adjust($i + 1 < $h; .v += .m[$i+1][$j].v)
| adjust($j + 1 < $w; .v += .m[$i][$j+1].v)
| .v = .m[$i][$j].v - .v/.n
| .d[$i][$j].v = .v
| if (.m[$i][$j].fixed == 0) then .diff += .v * .v else . end ) ) ;
 
# input: a mesh of width w and height h, i.e. a matrix as prodcued by matrix(h;w)
def iter:
length as $h
| (.[0]|length) as $w
| { m : .,
d : (Node(0;0) | matrix($h; $w)),
cur: [0,0,0],
diff: 1e10 }
| until (.diff <= 1e-24;
.m |= setBoundary
| calcDiff($w; $h)
| reduce range(0;$h) as $i (.;
reduce range(0;$w) as $j (.;
.m[$i][$j].v += (- .d[$i][$j].v) )) )
| reduce range(0; $h) as $i (.;
reduce range(0; $w) as $j (.;
.k = 0
| if ($i != 0) then .k += 1 else . end
| if ($j != 0) then .k += 1 else . end
| if ($i < $h - 1) then .k += 1 else . end
| if ($j < $w - 1) then .k += 1 else . end
| .cur[.m[$i][$j].fixed + 1] += .d[$i][$j].v * .k ))
| (.cur[2] - .cur[0]) / 2 ;
 
def task($S):
def mesh: Node(0; 0) | matrix($S; $S);
(2 / (mesh | iter)) as $r
| "R = \($r) ohms";
 
task(10)
</syntaxhighlight>
{{output}}
<pre>
R = 1.608991241729889 ohms
</pre>
 
 
=={{header|Julia}}==
Line 2,199 ⟶ 2,367:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">class Node {
construct new(v, fixed) {
_v = v
9,476

edits