ClearAll["Global`*"] (* second side of Class #4 worksheet *)
R1=20.0;R2=30.0;R3=15.0;R4=25.0;R5=10.0;R6=20.0;R7=20.0;R8=20.0;
v0=12.0;
vsize=5;  (* There are 5 unknown voltages *)
(* In this example, the outputs are in this order: v1,v2,v3,v4,v5,i1,i2,i3,i4,i5,i6,i7 *)
(* This 12 x 12 matrix could easily have been reduced to only 11x11 by
noticing that v0 = v1 before I even started... *)
(* In this Matrix, I colored the columns by hand for clarity... but you should
never do that! Red refers to unknowns voltages, blue to currents...*)
matrix1=(({{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, -1, 0, 0, 0, 0, -R1, 0, 0, 0, 0, 0}, {0, 1, -1, 0, 0, 0, -R2, 0, 0, 0, 0, 0}, {-1, 0, 0, 1, 0, 0, 0, 0, 0, 0, -R3, 0}, {0, 0, 1, -1, 0, 0, 0, 0, 0, -R4, 0, 0}, {0, 0, 1, -1, 0, 0, 0, -R5, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -R6}, {0, 0, 0, 1, -1, 0, 0, 0, -R7, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0, -R8, 0, 0, 0}, {0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 1, -1, 0, -1, 0, 0}, {0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 1}}));constants=(({{v0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}}));
results=Inverse[matrix1].constants//Flatten;
csize=Length[results]-vsize;
v= Part[results, 1;;vsize];
c = Part[results, vsize+1;;Length[results]];
Print["Voltages are v1, v2, v3, v4, v5 = ", v//MatrixForm ](* display voltages *)
Print["Currents are i1, i2, i3, i4, i5, i6, i7 = ",c//MatrixForm ](* display currents *)