P24 Manual
Multiloop Direct-Current Circuit Problems*
 * Based on material from Matlab Projects for Scientists and Engineers, by A.L. Garcia and M.C. Penland, and used with the authors' permission.
In multiloop circuit problems, a linear set of equations, obtained from junction and loop rules, is solved algebraically to obtain the currents.  When the number of equations is large their solution by pencil and paper calculation is straight-forward yet tedious and error prone making the task ideally suited for the computer.

Theory
In Figure 28-9, Section 28-3, of   Fishbane et al., a simple circuit is presented in Chapter 28, Direct-Current Circuits.  To determine the currents in this circuit, we use Kirchhoff's junction rule in Equation (28-13a) and Kirchhoff's loop rule in Equations (28-14a) and (28-14b):

-I1 + I2 + I3 = 0                  (28-13a)
R1I1 + R2I2 + 0I3 = E1        (28-14a)
0I1 - R2I2 + R3I3 = E2         (28-14b)
In matrix form, this set of equations this set of equations may be written as:
or AI = b where A is the 3x3 matrix on the left hand side, I is the column vector of currents and b is the column vector on the right hand side.  In this form, one may solve for the vector I by moving the matrix A to the right hand side.  We shall use MATLAB's matrix manipulation facilities to accomplish this operation.

Program
The MATLAB program, circuit1, which solves the circuit problem in Example 28-5 of Fishbane et al., is outlined below:

• Enter your username (id).
• Initialize variables (e.g., resistances, voltages).
• Set up the matrix A on the left hand side.
• Set up the vector b on the right hand side.
• Solve for currents I where AI = b.
• Display currents.
In MATLAB, a column vector is entered as a list with rows separated by semicolons.  For a matrix, each element is separated by a comma and the end of a row is marked by a semicolon.  For example, the 3x1 column vector and the 2x2 matrix,
and
are defined in MATLAB as X = [a; b; c] and Z = [a, b; c, d].

To solve the matrix problem AI = b, the program uses MATLAB's built-in matrix manipulation tools.  Specifically, the "backslash" operator (\) is defined as a type of matrix "division" to solve this problem as I = b\A.

Example

A sample output from the program is given below; compare these results with those given in Example 28-5 of Fishbane et al..

EDU» circuit1

  circuit1 - Program to solve a simple circuit problem
      shown in Figure 28-9 and discussed in Example 28-5
      of the textbook by Fishbane et al.

Enter your username (userid); for example,
  USERNAME: ABName
  USERNAME: dcwold

Enter resistances as [R(1), R(2), R(3)]
: [100, 10, 80]
Enter voltages as [E(1), E(2)]: [6,12]
Currents are
I(1) = 0.0673469
I(2) = -0.0734694
I(3) = 0.140816
EDU»

Listing of the Program
% circuit1 - Program to solve a simple circuit problem
%     shown in Figure 28-9 and discussed in Example 28-5
%     of the textbook by Fishbane et al.
clear; help circuit1;  % Clear memory; print header

%@ Enter your username
fprintf('Enter your username (userid); for example, \n');
fprintf('  USERNAME: ABName  \n');
Username = input('  USERNAME: ','s'); % Read input as a text string
fprintf('\n');

%@ Initialize variables (e.g., resistances, voltages)
fprintf('Enter resistances as [R(1), R(2), R(3)]\n');
R = input(': ');
E = input('Enter voltages as [E(1), E(2)]: ');

%@ Set up the matrix A on the left hand side
A = [ -1, 1, 1;   R(1),  R(2), 0;    0, -R(2), R(3)];

%@ Set up the vector b on the right hand side
b = [  0;  E(1);  E(2)];

%@ Solve for currents I where A*I = b
I = A\b;   % Use the MATLAB backslash operator

%@  Display currents
fprintf('Currents are \n');
for j=1:3
  fprintf('I(%g) = %g \n',j,I(j));
end