'''
solution.py
Eric Ayars
June 2016
A solution of the 5-loop circuit exercise.
'''
import numpy as np
# Start by getting the values of R and V
R1 = float(input("R1 = "))
R2 = float(input("R2 = "))
R3 = float(input("R3 = "))
R4 = float(input("R4 = "))
R5 = float(input("R5 = "))
R6 = float(input("R6 = "))
V1 = float(input("V1 = "))
V2 = float(input("V2 = "))
V3 = float(input("V3 = "))
V4 = float(input("V4 = "))
# Use these values to build the matrix M. Note that this matrix
# will vary depending on the equation set selected to solve the
# problem: here I use the equations from the solution on the
# PICUP webpage.
M = np.array([ [R1, 0, R3, 0, 0, 0],
[0,-R2, R3, 0, 0, 0],
[0, 0, 0, 0,-R5, R6],
[0, 0, 0, R4, R5, 0],
[0, 0, 0,-R4, 0, 0],
[1, -1, -1, 0, 0, 0] ])
# here is the right-hand-side b:
b = np.array([V1,V2,V2-V3,0,V4,0])
# And this is all it takes to get a solution:
x = np.linalg.solve(M,b)
# show the result.
print(x)