How can we use the software Mathematica to simulate and calculate the equivalent resistance between node A and node B in the 7x7 grid of resistors?
EDITED:
Using code from links:
And Assuming:Grid where each edge depicts a resistor between its incident nodes (all resistance values are assumed to be 1Ω
gridresistor[p_, q_, ai_, aj_, bi_, bj_] :=
[{A, B, k, c, V}, A = ConstantArray[0, {p*q, p*q}];
Do[k = (i - 1) q + j;
If[{i, j} == {ai, aj}, A[[k, k]] = 1, c = 0;
If[1 <= i + 1 <= p && 1 <= j <= q, c++; A[[k, k + q]] = -1];
If[1 <= i - 1 <= p && 1 <= j <= q, c++; A[[k, k - q]] = -1];
If[1 <= i <= p && 1 <= j + 1 <= q, c++; A[[k, k + 1]] = -1];
If[1 <= i <= p && 1 <= j - 1 <= q, c++; A[[k, k - 1]] = -1];
A[[k, k]] = c], {i, p}, {j, q}];
B = SparseArray[(k = (bi - 1) q + bj) -> 1, p*q];
LinearSolve[A, B][[k]]];
g1 = 7; g2 = 7;(*Grid 7 x 7*)
Ax = 4; Ay = 5;(*Node A*)
Bx = 5; By = 4;(*Node B*)
N[gridresistor[g1, g2, Ax, Ay, Bx, By]]
(*0.660852*)
With our getting into the correctness of the computation itself (I didn’t check that) there is an obvious syntax error
You forgot using a Module or a Block
So the code should look like
gridresistor[p_, q_, ai_, aj_, bi_, bj_] :=
Module [{A, B, k, c, V}, A = ConstantArray[0, {p*q, p*q}];
...
]
Yes you are right.Thanks.
Thank you so much!
I want to ask that why we have that loop in the code:
Do[k = (i - 1) q + j;
If[{i, j} == {ai, aj}, A[[k, k]] = 1, c = 0;
If[1 <= i + 1 <= p && 1 <= j <= q, c++; A[[k, k + q]] = -1];
If[1 <= i - 1 <= p && 1 <= j <= q, c++; A[[k, k - q]] = -1];
If[1 <= i <= p && 1 <= j + 1 <= q, c++; A[[k, k + 1]] = -1];
If[1 <= i <= p && 1 <= j - 1 <= q, c++; A[[k, k - 1]] = -1];
A[[k, k]] = c], {i, p}, {j, q}];
And why A[[k, k + q]], A[[k, k - q]], A[[k, k + 1]], A[[k, k - 1]] is equal to -1?
My coding in Wolfram language are quite low so I can’t help you.
This problem is potentially dangerous - I do hope that everyone is still well!
