0% found this document useful (0 votes)
49 views5 pages

Lab IV: Solving A System of Linear Equations and Kirchoff 'S Laws

The document discusses solving systems of linear equations using Kirchoff's laws. It provides two examples: 1) Solving a simple system of two equations, and 2) Solving a more complex system of N equations that follows a shifting pattern. It then discusses using Kirchoff's rules to solve circuits, stating the two rules and providing an example of writing the rules for a circuit with 6 branches and 2 batteries.

Uploaded by

SanjanaLakshmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
49 views5 pages

Lab IV: Solving A System of Linear Equations and Kirchoff 'S Laws

The document discusses solving systems of linear equations using Kirchoff's laws. It provides two examples: 1) Solving a simple system of two equations, and 2) Solving a more complex system of N equations that follows a shifting pattern. It then discusses using Kirchoff's rules to solve circuits, stating the two rules and providing an example of writing the rules for a circuit with 6 branches and 2 batteries.

Uploaded by

SanjanaLakshmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Lab IV : Solving a system of linear equations and Kirchos laws

Example 1 : Solving a simple system of linear equations If we have a small system of equations, we may
easily solve them by direct substitution
2x
1
x
2
= 1
x
1
+ x
2
= 10
By summing the two equation, we isolate x
1
and we proceed to nd that x
1
= 1.5 and x
2
= 4. It is a rather standard
procedure of linear algebra to cast such equations in the form of matrices
A x = b
where
A =

2 1
4 1

x =

x
1
x
2

and b =

1
10

(1)
In order to solve the matrix equation above, we invert the matrix A and multiply both sides of the above matrix
equation by its inverse. This operation of course assumes that this inverse exists.
A
1
A x = A
1
b x = A
1
b (2)
In Octave, the function inv inverts nonsingular matrices. Thus, the above system may be solved easily by
octave:1> A=[2 -1;4 1]; b=[-1 10];
octave:2> x=inv(A)*b
x =
1.5000
4.0000
Example 2 : A more complex system of linear equations This example is not only a demonstration of solving a
linear system of equations but also serves as a reminder of tricks that we use to form matrices with certain well-dened
patterns. Lets consider the following set of N equations :
x
1
+ 2x
2
+ 3x
3
= 1
x
2
+ 2x
3
+ 3x
4
= 1
x
3
+ 2x
4
+ 3x
5
= 1
.
.
.
x
N1
+ 2x
N
+ 3x
1
= 1
x
N
+ 2x
1
+ 3x
2
= 1
These equations follow a shifting pattern which wraps around after the Nth variable. The corresponding matrix then
looks like
C =

1 2 3 0 0 0 0
0 1 2 3 0 0 0
0 0 1 2 3 0 0
.
.
.
.
.
.
.
.
.
3 0 0 0 0 1 2
2 3 0 0 0 0 1

We are now going to write a function, linear system that takes in the number N, which is the number of equations
(and variables) and returns the solution of the above system. Lets rst analyze the matrix above. Its a rather sparse
matrix with only a strip of numbers in the middle. The main diagonal is lled with ones, the 1st diagonal is lled
with twos and the second with threes. Other than that there are the three numbers on the lower left corner, which is
a result of the wrapping around. Youll remember that to construct such a diagonal matrix by placing a given array
on its diagonal, we use the diag function in Octave.
2
octave:1> a=rand(5,1)
a =
0.10585
0.75673
0.80748
0.28744
0.70518
octave:2> M=diag(a)
M =
0.10585 0.00000 0.00000 0.00000 0.00000
0.00000 0.75673 0.00000 0.00000 0.00000
0.00000 0.00000 0.80748 0.00000 0.00000
0.00000 0.00000 0.00000 0.28744 0.00000
0.00000 0.00000 0.00000 0.00000 0.70518
By supplying a second argument to the diag function we can shift the diagonal up or down.
octave:3> M=diag(a,1)
M =
0.00000 0.10585 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.75673 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.80748 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.28744 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.70518
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
octave:4> M=diag(a,-1)
M =
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.10585 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.75673 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.80748 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.28744 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.70518 0.00000
octave:5> M=diag(a,-2)
M =
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.10585 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.75673 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.80748 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.28744 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.70518 0.00000 0.00000
As you can see the diag function adjusts the size of the resulting matrix to t the entire vector on the given diagonal.
One should thus make sure to supply a vector with correct length. With this information, it is no quite straightforward
to write our function.
3
function X=linear_system(N)
M=diag([ones(N,1)])+diag([2*ones(N-1,1)],1)+diag([3*ones(N-2,1)],2);
M(N-1)=3;
M(N,1:2)=[2 3];
a=ones(N,1);
X=inv(M)*a;
endfunction
When you run this function, youll see that the answer is always the same, namely you get a repeating fraction of
0.1666 for all the x
n
where n = 1, , N. This is due to the fact that the equations are completely symmetric in
all the variables. To break the symmetry a little, lets modify our function so that our equations read
x
1
+ 2x
2
+ 3x
3
= 1
x
2
+ 2x
3
+ 3x
4
= 1
x
3
+ 2x
4
+ 3x
5
= 1
x
4
+ 2x
5
+ 3x
6
= 1
.
.
.
x
N1
+ 2x
N
+ 3x
1
= 1
x
N
+ 2x
1
+ 3x
2
= 1.
That is to say, the inhomogeneous part is alternating ones and -1s. To do that, we can change the vector a in the
above function to
a=ones(N,1);
a(2:2:N)=-1;
while the rest of the function remains the same. The result is somewhat interesting : for even N, we always get an
alternating array of 0.5 and -0.5 whereas for odd N, the results are dierent for dierent N.
We can now write a loop around the function linear system and call it for a series of odd N and watch the
progression of x
1
as a function of N. We can collect the consecutive values in an array and nally plot this array.
The smallest meaningful number of variables for this system is 5.
octave:1> x1s=[];
octave:2> for N=5:2:17
> x1s=[x1s;linear_system(N)(1)];
> endfor
octave:3> plot([5:2:17],x1s,b*-)
The resulting plot should look like this :
4
-0.6
-0.58
-0.56
-0.54
-0.52
-0.5
-0.48
-0.46
4 6 8 10 12 14 16 18
x
1
N
As you can see, as N gets larger x
1
changes value and nally asymptotes to a value near -0.5.
I would like to draw your attention to a construct that I used in the above example, which is
linear_system(N)(1).
Because the function linear system returns an array, you can access the elements of the resulting array directly from
the function name without resorting to an intermediate array variable. Example 3 : Kirchos rules When we
are faced with a simple circuit, we usually solve it by geometric considerations. If, however, the circuit that we need
to solve is too complicated, we might not be able to visually simplify it. In that case, its safest to resort to Kirchos
rules. Kirchos rules state the following :
1. The sum of potential drops around a closed loop is always zero.
2. Due to charge conservation, the sum of all currents entering a junction and leaving a junction are equal.
In practice, we follow a set of rules of thumb to conduct calculations with Kirchos rules :
1. We mark all currents in all branches. The directions may be arbitrary.
2. We identify loops and give them directions. Directions are chosen completely arbitrarily.
3. We identify the junctions and apply the second rule.
4. We identify inequivalent loops and write the rst rule. In doing this, we
take as negative the potential change across each resistor if we are going in the current and the loop are
going in the same direction (positive if opposite).
take as negative the potential charge across each battery if we are going from the positive plate to the
negative plate.
5. After writing as many inequivalent equations as there are unknown currents, we solve the resulting system of
equations.
Lets write down the Kircho rules for the following circuit.
5
i
1
i
1
i
2
i
3
i
4
i
6
i
5

1

2
2r
r r
r
2r
i
2
+ i
3
= i
1
i
3
+ i
5
= i
6
i
4
+ i
6
= i
1
i
4
+ i
5
= i
2
2i
2
r
1
i
5
r + i
3
r = 0
2i
4
r +
2
+ i
6
r + i
5
r = 0
We see that for some values of r,
1
and
2
, some current values are zero and some others are negative. A zero current
simply means that there is no charge transfer across that branch whereas a negative current implies that an incorrect
direction has been assigned to the current in that branch.
function currents=kirchoff1(ep1,ep2,r)
A=[ 1 -1 -1 0 0 0;
0 0 1 0 1 -1;
-1 0 0 1 0 1;
0 1 0 -1 -1 0;
0 -2*r r 0 -r 0;
0 0 0 -2*r r r];
E=[0 0 0 0 ep1 -ep2];
currents=inv(A)*E;
endfunction

You might also like