% This code solves the three-loop circuit problem. % Three methods are used. % The resistors have the following values: r1 = 1; r2 = .1; r3 = 3; r4 = 2; r5 = 4; % The batteries have the following values: E1 = 10; E2 = 20; E3 = 30; d = [E1 -E2 0 0 -E3 0 0]'; A=[r1 0 0 0 0 -1 0; 0 r2 0 0 0 1 -1; 0 0 r3 0 0 -1 0; 0 0 0 r4 0 0 -1; 0 0 0 0 r5 0 1; -1 1 -1 0 0 0 0; 0 -1 0 -1 1 0 0]; % Use Gauss elimination method. A\d % Use inverse matrix method. inv(A)*d % Use block LU factorization method. A11 = A(1:5,1:5); A12 = A(1:5,6:7); A21 = A(6:7,1:5); A22 = A(6:7,6:7); d1 = d(1:5); d2 = d(6:7); A22hat = A22 - A21*inv(A11)*A12 d2hat = d2 - A21*inv(A11)*d1 x2 = A22hat\d2hat x1 = A11\(d1 - A12*x2)