 Welcome to this session on Gaussian elimination. In this session, we shall implement using a C plus plus program the Gaussian elimination technique that we had discussed in the last session. We remember that we had seen a system of linear algebraic equations in n variables can be represented by three matrices. The coefficient matrix would be an n by n matrix. There would be an array of n variables whose values are to be determined and an array of corresponding RHS values. The Gaussian elimination technique we recall reduces the coefficient matrix to upper triangular form making corresponding changes to the right hand side array and we then use back substitution to calculate values of all variables. Here is a quick recap of some of the representation. This is the general system of linear equations in n variables represented by matrix which will have the coefficients, the vector of unknown variables and the right hand side vector of values b naught to b n minus 1. The Gaussian elimination reduces the coefficient matrix to an upper triangular form with ones on the diagonal and using the back substitution we shall be able to solve for the values of all the unknown variables. When a coefficient matrix is reduced to an upper triangular form we generally get the following system x naught plus a naught 1 x 1 plus a naught 2 x 2 etcetera. The last row will be simply x n minus 1 equal to b n minus 1. Please note that the values of matrix a and b will now be different from the original values because lot of computations have gone while we transform the original system into this system. But the back substitution can be applied to calculate values of all the variables. Just as a demo a system of equations in two variables which we had 2 x plus 4 y equal to 8 and 4 x plus 3 y equal to 1 we can represent x by x naught and y by x 1. Then we have a two dimensional matrix 2 by 2 matrix representing these coefficients where the coefficient values will be 2 4 a 1 0 is 4 a 1 1 is 3 representing the second row or second equation. Right hand side b naught is a b 1 is 1 and after reducing the matrix to the upper triangular form the coefficients will turn out to be like this where the new values are as indicated we had seen this last time. We now look at the program gauss dot c p p and let us see how we construct such a program. First of course the standard inclusion of i o stream and the declarations of variables i j k will be used as index variables as is the common practice n represents the number of unknown variables or represents the system of equations as being of n linear equation. Matrix a represent the coefficient matrix we have declared it to be 100 by 100 matrix b correspondingly the right hand side will have 100 elements and of course x 100 which will not contain anything but eventually will contain the values which we would have calculated for the unknown variables which can be up to 100 of course we assume that the value of n is less than 100. We use additional variables divisor factor sum the meaning of which will become clear as we proceed. First we read the input we get the value of n we read the matrix a for simplicity I have avoided including statements which will validate the input but the program which will upload on the side will have all those things. Then I will read the matrix b this completes the input statement note again that our standard do loop for loop that we use will have i equal to 0 i less than n i plus plus kind of standard structure because this varies from 0 to n minus 1 and that is exactly the index value for which we need to read the values of the matrix b or here a i j which will have two nested loops one varying i from 0 to n minus 1 the other varying j from 0 to n minus 1. So, for every value of i j will be varied from 0 to n minus 1 notice that the input will have to be given in the row order that is 0th row all values will be input first then i will become 1 then the row number 1 all values of other columns will be given etcetera we now come to the Gauss elimination proper please note that our objective is to get 1 on the diagonal we start with the first row i equal to 0 now here the mat a i i element need not be 1 originally but whatever is that element if we divide the entire row by that element obviously on the diagonal will get 1. So, we just look at this value capture it as a divisor variable value and assign 1.0 directly to this there is no need to divide obviously because the divisor is same as the value but now we divide all values in the row by that divisor observe that we are doing a transformation to recalculate all coefficients in that row for j equal to i plus 1 to n minus 1 because it is the ith row that we are talking about. So, we recalculate i comma jth element of mat a by dividing that element by divisor we also divide the corresponding right hand side element. So, mat b i is also divided by divide notice what we have done we have actually transformed the original system by replacing the 0th row by a new row but which is derived by multiplying that 0th row original 0th row by a factor in this case it is the divisor by which we are dividing. Now if we redo the same thing again and again for i equal to 1 i equal to 2 and i equal to n minus 1 we would have got all of these corrected let us look at the subsequent sequence. We have subsequent rows and in the subsequent rows below this diagonal we need to get all 0 we now apply the second transformation namely we subtract the appropriate portion of the ith equation from the next row. So, for if i plus 1 is less than n then for k equal to i plus 1 to k less than n that is a k to n minus 1 we now calculate a factor which is nothing but mat a k i this is the factor by which we have to take a portion of this row subtract the ith equation from it. But having taken that portion we set that to 0 because this will be the column under the i comma ith row i comma ith element which is going to be 1. So, for j equal to i plus 1 up to n minus 1 we recalculate k comma jth element by subtracting from it a factor times mat a i comma j observe a i comma j is the original row we are looking at and a factor of that is subtracted from the kth row and we do this for all values of k from i plus 1 to n minus we do not forget to appropriately modify the kth element of matrix B as well. This simple nested iteration will complete the process of handling one particular portion of the transformation now we back substitute starting with the last variable. So, we know that x n minus 1 is simply the B n minus 1 ith element and once having got that for n minus 2 to 0 please note that we are going backwards n minus 1th value we already got now we got to get n minus 2 n minus 3 etcetera up to the 0th value. So, this is for once we are using a for loop which moves from a higher value to a lower value going backwards all the way up to 0 what we do essentially we sum of the i th row using values of x which we already determined we start with sum equal to 0 and for i plus 1 up to n minus 1 we calculate mat a i j into x j into the cumulative sum. Notice that all these x j's have already been determined previously because j starts only with i plus 1 on this having done this I simply calculate ith element of x as mat b i minus sum and when I do that for all values of i I would have actually solved the system is a very simple elegant and neat looking solution some of you might have difficulty in understanding this in the first instance, but please read this program and also try to execute this program. We need to output the results. So, these are standard output statements first you output the matrix which is the upper triangular matrix and then you output the values of x here are sample input and output results for your benefit. We have run this program with n equal to 4 the matrix a having these values and matrix b having these values you can simply input these values one after another starting with 4 then this row then this row and so on. When you do that when the program is executed you will actually get these results first the matrix a will be printed which is reduced to upper triangular form it will be printed something like this and then values of the variables of x will be printed as 1 2 3 4. In summary then in this session we wrote a C plus plus program to implement the Gaussian elimination method for solving simultaneous equation. We saw sample input for a system of 4 equations and the results. Please note that this program is also available on the side as gauss dot c p p. Feel free to download it load it into your code blocks or any other environment in which you are executing these programs and try it out for these input as well as other input values. Please do look at the problems that you might get when the values that are input represent a peculiar variant of a system of simultaneous equations those are the issues which are left to experts in the simultaneous equations. But as far as programming is concerned you are now equipped to write programs similar to this. Thank you.