Resistor mesh: Difference between revisions

m
Line 1,270:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{works with|Mathematica|8.0}}
 
Use <b>KirchhoffMatrix</b>, <b>PseudoInverse</b>, and <b>GridGraph</b> to compute the resistance between <em>any</em> two nodes in an <em>m</em> by <em>n</em> grid to the desired precision.
 
<lang mathematica>resistanceGraph[g_, prec_:$MachinePrecision]:=
With[{M = PseudoInverse[N[ KirchhoffMatrix[g], prec]]},
Outer[Plus, Diagonal[M], Diagonal[M]] - M - Transpose[M]
]
 
r = resistanceGraph[GridGraph[{10, 10}], 40];
 
r[[12, 68]]</lang>
{{Out}}
<pre>1.608991241730729655954495520510088761201</pre>
 
{{trans|Maxima}}
<lang mathematica>gridresistor[p_, q_, ai_, aj_, bi_, bj_] :=
Line 1,301 ⟶ 1,296:
{{Out}}
<pre>1.608991241730729655954495520510088761201</pre>
 
{{works with|Mathematica|9.0}}
 
Alternative implementation using <b>GridGraph</b>.
 
<lang mathematica>graphresistor[g_, a_, b_] :=
LinearSolve[
1,111

edits