pythonphysicscomputationcircuit

Using Python to solve the "resistor cube"


enter image description here

This is a computational problem.

My code:

import numpy as np

# Number of nodes in the cube
num_nodes = 8

# Create the conductance matrix G and the current vector I
G = np.zeros((num_nodes, num_nodes))
I = np.zeros(num_nodes)

# Define resistances
R1 = 0.18  # Resistance for R1
R = 1      # Resistance for other resistors

# Function to add conductances between nodes i and j with resistance R
def add_conductance(G, i, j, R):
    conductance = 1 / R
    G[i, i] += conductance
    G[j, j] += conductance
    G[i, j] -= conductance
    G[j, i] -= conductance

# Define the connections for the cube (each edge of the cube)
edges = [
    (0, 1, R), (1, 2, R), (2, 3, R), (3, 0, R),  # Bottom face
    (4, 5, R), (5, 6, R), (6, 7, R), (7, 4, R),  # Top face
    (0, 4, R), (1, 5, R), (2, 6, R), (3, 7, R),  # Vertical edges
    (3, 4, R1)                                   # Special resistor R1
]

# Add conductances to the matrix G
for i, j, R_val in edges:
    add_conductance(G, i, j, R_val)

# Define the current injections (1A into node 0 and -1A out of node 7)
I[0] = 1   # Inject 1A at node 0 (node A)
I[7] = -1  # Extract 1A at node 7 (node B)

# To avoid singular matrix, we ground node 7 (remove last row and column from G and I)
G_reduced = np.delete(np.delete(G, 7, axis=0), 7, axis=1)
I_reduced = np.delete(I, 7)

# Solve for the node voltages
V_reduced = np.linalg.solve(G_reduced, I_reduced)

# Insert the ground node voltage back (V[7] = 0)
V = np.insert(V_reduced, 7, 0)

# Calculate the equivalent resistance as the voltage difference between nodes 0 and 7
R_eq = V[0] - V[7]

print(f"The equivalent resistance between A and B is {R_eq:.3f} ohms.")

However, the code gives the wrong values, which I can check.


Solution

  • I have had a go at correcting your code. Your node numbers are inconsistent (as noted by user 9769953). I've put 0-3 on the top and 4-7 on the bottom. Current inlet at 0 and outlet at 6.

    It looks a bit like this:

       I
    ===>=== 0----------3
           /.         /|
          / .        / |
         1----------2  |
         |  4 . . . |. 7
         | .        | /
         |.         |/    I
         5----------6 ===>===
    

    Your setting up of R1 was wrong.

    To ground one value (say V6) you DON'T have to remove that line: just change it to all zeros except a 1 on the diagonal and set the RHS to 0.

    If R1=0.5 (as stated in the question, but not your code) you get the following.

    import numpy as np
    
    # Number of nodes in the cube
    num_nodes = 8
    
    # Create the conductance matrix G and the current vector I
    G = np.zeros((num_nodes, num_nodes))
    I = np.zeros(num_nodes)
    
    # Define resistances
    R1 = 0.5   # Resistance for R1                                    # <======= corrected
    R = 1      # Resistance for other resistors
    
    # Function to add conductances between nodes i and j with resistance R
    def add_conductance(G, i, j, R):
        conductance = 1 / R
        G[i, i] += conductance
        G[j, j] += conductance
        G[i, j] -= conductance
        G[j, i] -= conductance
    
    # Define the connections for the cube (each edge of the cube)
    edges = [
              (0, 1, R), (1, 2, R), (2, 3, R), (3, 0, R1),     # Top face (corrected R1 and node numbers)
              (4, 5, R), (5, 6, R), (6, 7, R), (7, 4, R ),     # Bottom face (corrected node numbers to match current)
              (0, 4, R), (1, 5, R), (2, 6, R), (3, 7, R )      # Vertical edges
            ]
    
    # Add conductances to the matrix G
    for i, j, R_val in edges:
        add_conductance(G, i, j, R_val)
    
    # Define the current injections (1A into node 0 and -1A out of node 6)
    I[0] = 1   # Inject 1A at node 0 (node A)
    I[6] = -1  # Extract 1A at node 6 (node B)       <====== Corrected
    
    # To avoid singular matrix FORCE V[6] to be 0
    G[6,:] = 0;   G[6,6] = 1.0;   I[6] = 0
    
    # Solve for the node voltages
    V = np.linalg.solve(G, I)
    
    # Calculate the equivalent resistance as the voltage difference between nodes 0 and 6    # <==== Corrected
    R_eq = V[0] - V[6]
    
    print( "Conductance matrix:" )
    print( G )
    print( "\nVoltages:" )
    print( V )
    print(f"\nThe equivalent resistance between A and B is {R_eq:.3f} ohms.")
    

    Output:

    Conductance matrix:
    [[ 4. -1.  0. -2. -1.  0.  0.  0.]
     [-1.  3. -1.  0.  0. -1.  0.  0.]
     [ 0. -1.  3. -1.  0.  0. -1.  0.]
     [-2.  0. -1.  4.  0.  0.  0. -1.]
     [-1.  0.  0.  0.  3. -1.  0. -1.]
     [ 0. -1.  0.  0. -1.  3. -1.  0.]
     [ 0.  0.  0.  0.  0.  0.  1.  0.]
     [ 0.  0.  0. -1. -1.  0. -1.  3.]]
    
    Voltages:
    [ 7.63157895e-01  4.73684211e-01  3.42105263e-01  5.52631579e-01
      4.73684211e-01  3.15789474e-01 -9.32233669e-18  3.42105263e-01]
    
    The equivalent resistance between A and B is 0.763 ohms.