 Welcome back. In the previous segment we discussed the organization of the main program and the classes which are related to the canvas and the graphics. Now we are going to talk about how the circuit is represented inside the computer. So this is done in a class called MathRep. So it keeps track of the data related to the circuit. So the data consists of the manner in which the components are connected, the conductance values and the current source values or what is sometimes referred to as current source strengths. Then the main member function in this is the function solve which solves the circuit built up until then. So let me talk about the representation a bit more precisely and this is discussed in section 23.4 of the book. So suppose we have an n node circuit then it will be represented using an n by n matrix and we are going to call this matrix A. In addition to that we will also have an n by 1 vector B. So these will keep evolving as our circuit gets built and then when we want the circuit to be solved we will perform mathematical operations on this matrix and vector and we will see all of that soon. So this matrix A is going to be used to represent the connections and the circuit values in the following manner. So whatever conductance there is between node i and node j we are going to make an entry of that value in the matrix, in the matrix element Aij. So just to make sure that this is understood. So let us say this is node i and let us say this is node j in our circuit and suppose connecting these two we have we put some conductance c. So then we want simultaneously in the representation to have Aij equal minus of c. So this will keep track of what the conductance is and of course it is symmetrical. So we will also have A of j i also minus c and it is 0 if no conductance is specified. So if there is no edge then this had better be 0 because no edge is equivalent to 0 conductance. So this is indicative of how much current can flow and if there is no edge no current can flow and therefore it is natural to say that the conductance is 0. Aii is going to be the sum of the conductance is connected to node i. So let me again give an example. So this is some ij, this is some k, this is some l and let us say this conductance is c, this is some d, this is some e then Aii we want to be equal to c plus d plus e. So note that given the circuit we can figure out what these values are and given these values we can figure out what the circuit is. So to that extent this is the representation of this circuit. Of course we are not completely done yet, we have not said how voltage sources are represented. So voltage sources are represented in the following manner. So we not only have this matrix but we also have that vector b. If you have a current source which say starts off at some vertex w and enters i and it has some strength j. So then b of i should be the sum of all such current values which are forced to enter. So we will see this in a minute and of course if there are some currents leaving then that is as good as negative entry. So over here we are subtracting the sum of the strength leaving i. So how does this circuit get built incrementally? Well what happens when a new node is added? So let me draw a picture over here, a new picture. So here is some circuit that we have created and now we create a new node. Well this new node does not have any connections at all. So what do you think will be present in its row and in its column? So if we add a new node we are going to have an extra column and an extra row added. And since the node is not connected to anything at all we have to make that initial row and column become 0. So again let me remind you how did we add a new node? Well suppose this circuit already had nodes 0 through n minus 1 then when we created a new node we created a new node n. So this is now going to correspond to increasing the number of rows and the number of columns. And so the nth row will corresponding to the new node and that has to be made 0. What about B of n? So if you remember or I guess you do not have to remember B of n is supposed to contain the sum of currents and currents leaving and entering through sources. But right now there are no sources connected. So as a result 0 gets added to the column vector B. So originally our column vector look like this so say B0 through B of n minus 1 and we are going to add a B of n to it and the value for this we are going to make it to B is 0 because there is no current source connected to this. So now what happens if a conductance C is added between nodes? So let us go back to this picture. So suppose a conductance C is added then what happens over here? Suppose let us say we are just adding this but if we add this aij should be minus c. As a result we are going to subtract c from aij remember aij was originally 0. So both of these were originally 0 so we are going to subtract c from them and we have to this c has to appear over here and so to aii there might have been other entries but we now add c as well. So that is how adding a conductance gets reflected in our data structure and if I add going from node i to node j so let us say this is node i and this is node j so I can even draw it over here so let me do it that way. So suppose I add a current source going from i to j over here so its strength is j well what should happen? So B of i should have the current sources entering i but this is current leaving i so as a result I should subtract j from B of i so which is exactly what I am doing and this j is entering j the node j and therefore I am adding capital J to B of j. So this is how exactly that circuit gets built up. So let us look at our code and look at the member functions add node add source add conductance. So these are operations in Mathrep. So add node first so we have to push back an entire zero row we have to append a new zero row so this is what happens this is what makes that happen notice that the vector class makes it very easy to append and append rows since this is a vector of vectors so a is a vector of vectors C over here and therefore this will just append an entire row and then we have to add a zero column so now what we do is we are just going to look at every row and we are going to append a zero to it that is it so this is a loop over every row of that vector and we are appending a zero and finally we want to append a zero to our B as well and so that is what this does. So what about add source so if I am adding a source you remember that if a source of value V is added then going from I to J then value has to be subtracted and to J the value has to be added so this is what happens what we need to do and if I want to add a conductance then to II the value has to be added see this and also to JJ because the conductance is connecting to and the treatment is symmetrical and from IJ the value has to be subtracted and also from GI so that is what add conductance does so quite simple. Alright so let us get back to our presentation so now we will talk about how to solve the circuit. So solving the circuit first of all requires us to find the voltages at the nodes so we do not know those voltages and let us say we represent the voltage at node I by X of I so X of I is so X is going to be a vector and I is going to be the ith index in it value at index I in it but right now this is unknown to us. So now I want to tell you a little bit about how circuits work so let me start on that so this is our vertex I and this is our vertex J and let us say that going from here to here there is conductance GIJ. Now how so what do we know about the current going from here to here okay so it turns out that this current from I to J is equal to the conductance of that times the voltage of I the voltage drop or the voltage at I minus the voltage at J so this is the current going from I to J. Now we do not know this we only know well yeah so everything here is an unknown so we do not know the current we do not know the voltages but what we have done is we have established a relationship between the voltages at these points and the current over here okay so this is just what is called Kerchoff's voltage law as you might have perhaps studied in physics okay all right so now I can ask what is the total current leaving I okay so there might be other edges okay so how do I calculate this well I just take the sum over let me let me do it in a different color just to keep track of it so I am going to take the sum over all the J all the all the conductance is connected to connected to I okay so if I just take the sum so that gives me the total current leaving okay now I can factor this out and that factoring is important so let me write it down a little bit carefully or I guess I can explain it on the screen okay so what is happening over here well there is this excite term this excite term is common to all the terms in this series so I am going to pull it out and what is the coefficient of the excite term well it is going to be the sum of the conductances gij so that is what I have written over here some of the conductances leaving I or these gij's and then I am left with this remaining term so gij minus xij and that I have just pulled that minus sign out over here okay all right so that is the total current leaving I through the conductances now this if you remember the sum of the conductances leaving I was just aii okay and therefore this is xi times aii and gij the negative of it is in fact aij so this is what the entire value is okay and in fact this is nothing but the ith component of the product of the matrix a and x okay so we do not know x yet but what we have done is we have established a relationship between the total current leaving I through the conductances and this matrix a and this unknown vector x so it is ith component of the product is indeed the current leaving I through the conductances okay and this this must equal I am sorry I should have said equal over here this must equal the total current entering I through the conductances right so so here is my here is my vertex I and I am talking about current leaving through conductances but there is current entering through voltage sources as well okay so what is the current entering through sorry through the current sources as well so what is the current entering through the current sources well the way we define b it is exactly bi okay so what does this tell us this tells us that this must be equal to this so the ith component of the product ax must equal the ith component of b but this is true for all ith components and therefore in fact this entire matrix product must exactly equal b okay so sorry we should be so we have to solve ax equal to b because we we know that ax must equal b and now x is the only unknown this vector of unknowns is there but we can get that if we solve this matrix equation so this is a simultaneous equation which we have to solve okay now there is a slight itch over here this system has n equations and n unknowns remember n is the number of nodes in our circuit okay unfortunately okay these equations are not independent okay but we will get an a solution if we set any unknown to 0 okay so after we set an unknown to 0 we will get a solution and the details of that I am not going to talk about right now but they are discussed in this section okay so that is what we are going to do we are going to set we are going to set xn minus 1 to 0 okay and this will effectively allow us to drop the last column as well okay so we will get a system which has n minus 1 equations and n minus 1 unknowns and these equations will all be independent that is they will contain distinct information and then we can solve it okay by the way if you are so many of you probably know circuit no electrical circuits a little bit and if we have voltages in our electrical circuit then voltages are all relative so I can fix any voltage to be 0 and that is exactly what we are doing over here okay but it relates to equations not being independent and that is discussed in this section so please read that okay so anyway so I have told you why this product ax must equal to b and I have told you that we are going to set one of the values xn minus 1 to 0 and then we just have to solve for the remaining unknowns so we have a smaller system well slightly smaller system which we have to solve okay so now how do we solve this system of equations so this is discussed in section 15.2.1 and I am going to discuss it very briefly okay so so we are going to solve ax equal to b and we are only going to look at the first n minus 1 unknowns and first n minus 1 equations so in fact let us forget that we even had that n minus xn minus 1 okay so basically the idea is that we are going to use equation i to eliminate xi from other equations so let me just write that down show that to you okay okay so our equations are a11 x1 plus a12 x2 plus a13 x3 and so on equal to b1 then a21 x1 plus a22 x2 plus a23 x3 and so on equals b2 and in general ak1 x1 plus ak2 x2 and somewhere here I will get akk xk and so on equals bk okay now what this is saying is that I am going to use this equation to eliminate x1 from everyone from every other equation over here okay so how do I do that okay so in general if I have say ai1 x1 plus ai2 x2 and ai i xi equals bi okay then this equation is going to be used to eliminate this ith unknown okay so if this is going to eliminate the ith unknown okay how do I do it well okay so let me let me write down what the equations these equations are so I am going to have a11 x1 plus a12 x2 and so on and say the typical term will be a1i xi and so on equals b1 okay then a21 x1 plus a22 x2 and so on and the term over here will be a2i xi all the way equals b2 okay and maybe the ith term is going to be the ith equation is going to be something like a j1 x1 plus a j2 x2 aji xi equals bj and let us say the ith equation is ai1 x1 plus ai2 x2 and here I am going to get aii xi equals bi okay so what our idea over here is saying is that we are going to use this equation to eliminate xi from all these other equations okay so how do we do that well if I want to get rid of this term over here in this jth equation what do I need to do I am going to multiply okay I am going I am going to multiply this by aji upon aii okay so this entire row is going to be multiplied by aji upon aii so what do I get over here so over here I will get aji xi if I now subtract this resulting row which has aji xi from this I will get a zero over here okay so that is how I have eliminated this so that is what I am going to do so I am going to do this for this row this row this row and also all the rows underneath it okay and now in this I am going to divide by aii okay so we in general we need to worry about whether aii is going to be non-zero but the way we have defined aii we will always be guaranteed that it will be non-zero and so we do not have to worry about it so another way of saying this is that we do not need to pivot in case you in case you have seen this heard this term pivot okay so what does this do so at the end of it once we do the elimination our matrix a is going to look like some non-zero entries over here along the diagonal and everything else is zero and b has changed suitably but b may have whatever entries over here zeros and non-zeros okay in general non-zeros okay so we are we have a diagonal matrix but now if we in fact make this one then this will exactly be the solution of ax sorry I should have x over here okay equal to so if this is made one then this will exactly be the solution okay so in other words if I divide this entry by this entry I will get the solution for xi okay so this is the ith row okay so that is in fact what we are going to do okay so xi is bi upon aii so that is what is involved in solving the circuit okay so at this point we will have found xi which are the voltages and so the program can print the voltage at all the nodes okay and once we know the voltage voltages we can also of course find the currents because we already said that they can be they are related as per this over here but we are not going to do that that is just a detail that you can take care of okay and you could also say that look I do not want the current printed on or the voltage printed on my on my shell window but I want it printed in the circuit in the canvas itself and that also could be done but again that is a detail that you can take care of okay all right so we will take a quick look at this member function solve okay so what does solve do so I have described it to you already so we are going to use the ith row okay to eliminate xi from every jth row okay so so long as of course i is not equal to ga so it has it is going to calculate this multiplier and then it is just going to subtract that row and that is that subtraction process that is all okay and then finally it is going to print out bi upon aii as the final voltage values okay and remember that the last so so we have we had thrown out one unknown and we had set that to 0 so that that was that had index a of size a dot size minus 1 and that value was 0 so we are going to print that out as well okay so that is it so that is what the solver looks like okay so we have this already and so what have we discussed so we have discussed how circuits are represented as a system of equations okay and we said that we are going to set 1 unknown to 0 and that gives us independent equations then we discussed how the system can be solved okay so we will take a quick break over here and then we will discuss extensions and we will conclude this lecture sequence thank you.