 Hi, in this lecture, we will learn to implement Gaussian elimination method as a python code. We will start our discussion with a quick recall of the naive Gaussian elimination method. Remember, we will implement only the naive Gaussian elimination method. We will not go to implement the modified Gaussian elimination method in this class. As we did in the theory part, we will implement the naive Gaussian elimination method only for a 3 by 3 system. Recall that the naive Gaussian elimination method first does a elimination process in which we will convert the given system into a upper triangular system by doing certain elementary row operations. The elementary row operations are as follows. First, we will define certain elements like m 2 1 m 3 1 which are given like this. This should hold only if a 1 1 is not equal to 0. Therefore, we have to first check whether a 1 1 is not equal to 0. If it is not equal to 0, then only you will go to calculate m 2 1 and m 3 1 and then next what we will do is we will retain the first equation as it is. We will replace the second equation by this expression. What is this? We will call the first equation as e 1, the second equation as e 2 and the third equation as e 3 and then we will replace e 2 by this equation which is e 2 minus m 2 1 into e 1. Similarly, e 3 is replaced by this equation that is e 3 minus m 3 1 e 1. This is what we have to do in step 1 and that will make these terms to be 0. And thereby you will get your reduced system like this where the second equation and the third equation first coefficients will be 0. And then we will go for the second step where we will define m 3 2 like this. For that once again we have to check whether a 2 2 2 what is this? This is nothing but the coefficient of x 2 in the second equation of the reduced system after step 1 that is this one. So, that you have to take and check whether it is non-zero. If it is non-zero then only you will go further into the algorithm otherwise you have to stop your code. Now, if it is non-zero then you will first find m 3 2 and then you will go to do the elimination process with the third equation. How will you do that? Well for the second step you will retain the first two equations as they are and then you will go to replace the third equation by the third equation minus m 3 2 into the second equation that is you will multiply this equation with m 3 2 and then subtract with the third equation. Recall we still call the first equation as e 1, second as now e 2 and e 3 and then you will replace this new equation with the old third equation and thereby you will get an upper triangular matrix like this. This is what the Gaussian elimination method does. We have already learnt it in one of our previous lectures and now we will go to do the backward substitution process. The elimination process is now over and we got a upper triangular matrix which is known to be equivalent to the given system in the sense that the solution of the given system is the same as the solution of the reduced upper triangular system. Now at the end you have to go for the back substitution for that we first have to check whether a 3 3 is not equal to 0. If so then you can obtain x 3 from this equation and then once you have x 3 you will substitute x 3 into the second equation and get x 2 here and once you have x 1 and sorry this is x 2 and x 3 then we will go to substitute them in the first equation to get the value of x 1. So, that is the back substitution process. If you recall we have also observed that you can have a lu factorization of our matrix A using Gaussian elimination method where u is nothing but the upper triangular matrix that we got here right that itself will be taken as u and then the lower triangular matrix is formed by collecting all the m values and arranging them in this format with all the diagonal elements as 1 right. So, this is the upper triangular matrix and this is the lower triangular matrix and you can check that A can be obtained by multiplying L into u. So, our interest is to develop a python code for the naive Gaussian elimination method and we also want to get the upper triangular matrix as well as the lower triangular matrix. Let us see how to develop the python code for this. To save time I have already developed the python code I will just go to explain it here. Please note that our intention here is not to develop an efficient code but just to implement the method into a python code. Therefore, often what we will do is we will implement the method in the way it is explained theoretically. In that process sometime we may even lose the efficiency of the coding part but our intention is to learn the coding as the first step. Therefore, we will often try to implement the methods just like how we have written mathematically and explained the same way we will try to implement it. Efficiency is altogether a different skill which one has to develop later. Here we will not concentrate on developing an efficient code. This is generally followed in all the codes that we develop. Sometimes we may develop a code which is also efficient but sometimes we may lose the efficiency just because we want to put the coding part just like how we have explained it mathematically. Having said that let us come to explain the naive Gaussian elimination code only for a 3 by 3 system. Just for an example, I have taken the system as a is equal to 161 minus 237 and 1 minus 8 minus 2. This is just I have chosen randomly and I am also taking the right hand side vector b and that is given as 1 minus 6 and 2. If you want to change your matrix you just have to change this a parameter and also if you want to change the right hand side vector you have to just change here. And now let us go to do the step 1. So, this is the first elimination process. Since our interest is to formulate the matrix L which is the lower triangular matrix I am just defining my all my m parameters in the form of a matrix. Remember this is just a idm in python which will initialize a two dimensional array with all the entries as 0. This is very useful you can also do it with a pair of nested for loops, but this is often handy for us to do. You just have to remember this format and everywhere you can copy and paste wherever you want to initialize a matrix. Here my interest is to initialize a 3 by 3 matrix. Therefore, the parameter 3 is sitting in these two places and then what I am doing is all the diagonal elements are initialized as 1. If you remember in the LU factorization we want all the diagonal elements as 1. Therefore, the matrix m that I am defining here is basically the matrix L later I will take it as L finally, I could have just defined it as L, but in the theory we are doing all these parameters with the notation m. Therefore, I have initialized it as m and then all the diagonal elements are taken as 1. Now for the first step the eliminated matrix that is the coefficient matrix of this system is what I am calling as a 1 in my code. I am just initializing my a 1 here again it is a 3 by 3 matrix therefore, I have these parameters 3 here. If you put 4 here and 4 here you will get a 4 cross 4 matrix and so on and all the elements of the matrix will be initialized as 0. Similarly, I am initializing the corresponding right hand side vector b as b 1. Mathematically we have this notation and this is what I am initializing as b 1 in my code. Now if you recall the first equation is retained as it is right. So, E 1 is retained as it is we are not going to disturb it that is why I have taken a 1 of 0 0 it means what a 1 of 0 0 is nothing, but a 1 1. If you recall I have told in the last class that python always starts its index from 0 and then it goes up to n minus 1. If you have n elements in your sequence it always goes from 0 to n minus 1. Therefore, if you are trying to access a 1 1 mathematically then you should actually do a of 0 comma 0 perhaps we can write it as a here. So, this is what is so suppose mathematically you have a i j then in the code it is a of i minus 1 j minus 1 is what we have to use in the code as the notation right. So, that is what I am doing a 1 1 is stored in the first element of the this matrix and similarly this is a 1 2 is taken as it is in the first step a 1 3 is taken as it is in the first step. So, this is what here we have done after the first elimination step the first equation should not be changed right a 1 1 should be as it is a 1 2 should be as it is and a 1 3 should be as it is that is what we are doing in the code here and that finishes the first equation. Now, we have to come for the second and third equation elimination process if you recall we have to define m 2 1 and m 3 1 in the python notation this has to be written as m it is 2 therefore, in python it should be 1 and the second index is 1 therefore, we have to have it as 0 similarly this is m 2 0. So, that is what I am doing here m 1 0 m 2 0 and that is a 2 1 divided by a 1 1 similarly this is a 3 1 divided by a 1 1 all the index should be reduced by 1 that you should always keep in mind in order to adopt your mathematics to python notation this one thing that you have to remember next is the second equation. If you remember the second equation is e 2 minus m 2 1 into e 1 should be replaced by the equation e 2 right therefore, in the matrix a 1 remember here the first element is 0 into x 1 right therefore, we should not touch a 1 of 0 0 because we have already initialized the entire a 1 matrix as 0 matrix therefore, we will not touch this element at all we want it to be 0 we only want to take this element right mathematically we had this notation now instead of this we are using the notation a 1 therefore, it should be a 1 of 1 1 right because this is 2 2 therefore, in python it should be 1 1 that is what I am writing here that is a 1 of a 1 1 is equal to a of a 1 1 minus m 1 0 remember this is nothing, but a 2 2 2 that is what we have taken as a 1 of 1 1 right that is nothing, but a 2 2 minus that is the second equation minus m 2 1 into a 1 2 right if you go back you can see this is the expression we have defined in the method and that is implemented in this step and similarly you can now put a 2 3 of 2 x 3 right that is what we are writing here a 1 instead of 2 we have 1 here instead of 3 we have 2 here therefore, this element is what we are actually trying to compute here and that is given by this expression you can compare it with the expression that we have given in the method theoretically and see how it comes similarly you can go to do the third equation elimination and you can see that that is given by this expression in the code. So, that defines your left hand side coefficient matrix that is this is the first equation then this comes the second equation where the first coefficient is 0 therefore, we are not doing any computation with that similarly the third equation is defined here again with the first coefficient that is the coefficient is x 1 is set as 0 automatically when we are initializing the matrix a 1 and later we are not putting any effort to compute it because we already know that its value should be 0 therefore, we just put that and we never compute that now comes the right hand side calculation that is the vector b is now changed to a new vector and the new vector is already initialized as b 1 with 0 as values now we have to put the actual values in b 1 remember the first equation is not changed therefore, the value in b 0 should be kept in b 1 also right this is the right hand side first coordinate of the vector b and now b 1 of 1 this is nothing but the second coordinate of the right hand side vector after the first elimination process you can go back theoretically and see what is the expression for that and come back and try to understand its corresponding notation in the python code you will clearly see that this is the notation that needs to be written similarly the third equations right hand side is given like this so with this we have completely calculated the reduced system at the step 1 right now let us go to do the step 2 for step 2 if you recall what we have to do we have to first check whether this a 222 right that should not be equal to 0 if that is so then we will go to calculate m 32 let us see how to do that well here also in the step 1 we have checked whether a 11 is not equal to 0 only when a 11 is not equal to 0 we will do this calculation if not what we will do we will simply put a warning command that new Gaussian elimination method failed because of a 00 is 0 and then I have defined a variable indicator at the beginning of this code if you go back you can see I have initialized it as 1 that is just as an indicator I will see whether I have encountered this 0 element anywhere in my steps anywhere it encounters then it will just put indicator as 0 it means the Gaussian elimination method is already failed and then it comes to the second step if the first step was successful then my initialized value of indicator will be there which is 1 now it will take as 1 here this if condition will allow the control to go into it when this indicator carries any non 0 value ok if it carries 0 value then it will not get into this if condition. So, in the first step if the name Gaussian elimination method failed then indicator is 0 therefore this will not be executed otherwise it will carry the value 1 from the initialized level and therefore this if condition will be executed if this if condition is executed let us see what happens again you will initialize the matrix A 2 which is our final tridiagonal matrix because we are working with 3 by 3 system therefore we will only go up to 2 steps right. So, we will be finishing our Gaussian elimination method with the second step therefore this A 2 will be our final upper triangular matrix at the beginning of this step I am initializing it as a 3 by 3 0 matrix and I am also initializing the right hand side vector B 2 when I know already that the first two coefficients of B 2 will not be changed in fact I can put it at the initialization level itself and then keep the third coordinate as 0 and later I will fill it up with the corresponding value. Now, the first two equations will not be changed at all when compared to the previous step therefore the first equation is not changed right from the given equation therefore I am taking the first equation same as the given first equation the second equation is not changed from the step 1 therefore the second equation is just taken from the first A 1 matrix because A 1 is formulated at step 1 right and now for third equation we have to do the elimination process for that I have to check whether A 1 of 1 1 that is A 2 2 right this is A 2 2 of 2 that is what we have called as A 1 of 1 1 right that should not be equal to 0 this is what we are checking theoretically and I am doing it here if it is not equal to 0 then I will go to find M 3 2 right in python it should be M 2 1 1 less always and that is given by this and then in the third equation remember the first one will be 0 x 1 which is eliminated in the first step now in the present step we are also eliminating the coefficient of x 2. So, when we know that this is 0 as usual we will not put any effort to compute it it is already initialized in this step itself as 0. So, we will retain it as it is plus A 3 3 3 this is what we will be now computing here and similarly equal to B 3 3 and that is computed here you can go back to the expressions given theoretically and see how we have written this right and once you have it then finally we are having our upper triangular matrix now you have to go for the back substitution process well if you recall again the back substitution should come only when you have successfully computed both the steps right. Therefore, you will again check whether the indicator is still holding the value 1 because if any of the step failed then your control of output indicator as 0 and therefore, it would have switched off the process and that is taken care here if the indicator is 0 it will never get into this if condition if indicator is 1 then only it will get into this or any non 0 for that matter in our case it is 1. Once it comes in then you have to again check whether A 3 3 3 is non 0 that is what we have to check right that is what I am doing in the code I am checking whether A 2 of 2 2 is not equal to 0 if it is not equal to 0 then I will do the back substitution process here instead of typing the back substitution code what I did is I have separately defined it as a function in python if you know that you are going to use a set of lines repeatedly in many codes then it is better to put it as a separate function and call that function into your program wherever it is needed what I did here is I have made this back substitution as a separate code I have written it in the form of a function how to do that well that can be done with this command define a function the function name is back substitution and what are all the parameters that I have to supply into this function these are the upper triangular matrix and the right hand side vector. So, I am passing these two information into this function and what that function does is it initializes a vector x as 0 0 0 and then x 3 is what is denoted as x 2 here and what is x 3 theoretically if you see x 3 is obtained as b 3 divided by a 3 3 right that is what we are doing here x 3 is b 3 divided by u 3 3 now I have captured this matrix as u therefore I have to use the notation u here similarly x 2 you can compare the theoretical expression and see how it comes and x 1 theoretically here it is x 0 and its expression is given like this once it computes x 1 x 2 and x 3 it returns the value x and now we have to get back that value into our original program remember we have computed a 2 and b 2 from step 2 right that I am pausing it into the function back substitution and got the value x back from the back substitution and that is going to be my solution again at this level if the diagonal element of the third equation is 0 then I should not do the back substitution process that is taken care of by this if condition then the rest all are the print commands that is I am printing the output in all this commands let us try to see how this program runs for that I have to click this play button it will take some time it has initialized some memory for us in the cloud mission and it has run the code you can see that the code has run successfully this is the a matrix that I have given and this is the b matrix I have given and what is the solution the solution is given like this so I am just printing the solution somewhere here see I am printing the solution here and that is printed here the solution is given by this now I am also saving the matrix U what is my matrix U here I am printing the matrix U the matrix U is nothing but a 2 in my program right so I am printing it here similarly the matrix L is printed here and that is nothing but m in my program notation okay so that is what you can see in the output so L is given like this and U is given like this I am also checking whether L into U is giving back my A you can also check that L into U is precisely the A that we have taken you can see that I am just doing this at this level this is nothing but the matrix multiplication of L into U right so that is exactly as what we want is given here so this is given and that completes the code for the Gaussian elimination method I hope you have understood one thing is when you run the program before running the main Gaussian elimination program you have to once run this function back substitution otherwise the main program will give you an error message with this we will end this class thank you for your attention.