[Music] [Music] hello viewers welcome back to the course on scientific computing using matlab so in the previous lectures we have discussed the iterative methods for solving the linear system of equation so in the in the previous lecture we have discussed that how we can make the matlab code for the ghost jacobi method so in today lecture we will will try to make the matlab code for gauss seidel method and we know that in the gauss-seidel methods that the method is convergent if eigen values the modulus value of the eigen values should be less than equal to one or the spectral radius that is rho h that should be less than one so and the rho h we know that this rho h where h is the convergence matrix so this one we can delete strictly less than one so and now in the gauss seidel method we know that the h will become minus l plus d inverse u so in this case we will find try to find that what is the our convergence matrix now if you want to find the rate of convergence so rate of convergence of iterative methods to solve the system a x is equal to b so you find the rate of convergence so rate of convergence is given as there is minus log and then spectral radius because the spectral radius we are taking so that is the the maximum value we are taking so if you see from here we can write this as i can write should be less than one so if i take the maximum eigen values using that one we will find the spectrum and i know that the if the gaussian method is converging that then the eigen value the maximum eigenvalue will be less than one so that is why the negative sign has been taken because this quantity is less than one so whenever take the log of this one that will be negative so just to get rid of this one i will take the negative sign from here so that is the rate of convergence of the of any iterative methods which may be a gauss seidel it may be a gauge jacobi and other iterative method then the rate of convergence is given by this formula and in the code we will try to make so will introduce all this in the in the matlab code we introduce that how we can calculate this ah convergence matrix then how we can calculate the rate of convergence for a given initial condition so let us move to then so let us move to matlab to code goes cedar method so let us move to the matlab so let us start doing the code for the go seeder so as we have done in the jacobi method so i have also made the gauss hidden method this is for you so we can do it here now it is same as i am defining the function that x is equal to cedar a b x not in tolerance so in the previous one we have change the name to jacobi so now it is a seed gauss serial method other things are same that a is the matrix which we are going to solve that is n corrosion b is the right hand vector and x naught is the initial approximation of the solution and the tolerance is that the how much accuracy i need for the finding the solution of the system now this is the b so i find the length of b that is n now from here i define a vector that is with the zero elements so this is the dimension n and the column one so it is a column vector so you can write from here that this is the column vector now i start with the iteration so i that is initial value 0 i just take the error is equal to 1 just to start the process now the display i show that the initial gas is whatever the initial gas i give so that will be x i will put that one is the x old is equal to x naught so x naught is coming from this from the function where this function has been called so this x naught will go and it will save into the x alt and if i want to display this one so i will remove this semicolons now this will be displayed on the screen now i will go to the while loop so in the while loop i will go from here that this is the if the error is greater than tolerance and iteration in less than 100 then we should enter into this one so if any one of is is false then we will not move to the while loop so error should be greater than tolerance and iteration should be less than hundred now from here if i go inside then i will go from j from 1 to n now what will happen in the gauss seidel method that the first value that is the first equation of the system will find out the value of x suppose i have a the solution as x y z in 3 by 3 or x 1 x 2 x 3 up to x n in n by n matrix then x one will be calculated same as the jacobi method so this will be j is equal to one so x nu one will be b j that is one because j is one here minus a j so j is one and these are the matrix from two to n multiplied by x volt whatever the initial initial approximation i have given so that will be multiplied by this one and then the whole divided by x jj the coefficient of x one so that is the first point in the matrix so this will be calculated and then the last one j is equal to n so i know that in when i take j j equal to n then the new updated value will be used to find out the x nu so that is why i take x nu j that is equal to b j and then the same matrix a j from 1 to n minus 1 and then see from here that i have taken the x nu so this is the x nu so whatever the solution we are getting from the from the previous equations we are solving that j is equal to 1 and j is equal to 2 3 up to n minus 1 that all solution will be used here and then divided by ajj so this and this conditions we are we have to calculate separately else means other than this first and last we will find out the x nu j is equal to b j so this is the value we are going to use so what is going to happen if we split the matrix into two parts so the matrix below the diagonal elements will be calculated this x nu and above the main diagonal it will be used with the x old so whatever the same thing we have done so we will find out the b j minus a j and then all the columns from 1 to j minus 1 i will use the updated value that is x nu minus a j and j plus 1 to n because i have to leave the value on a j j so after that all the values above the main diagonal i am using the old value so that is from j plus 1 to n and then the whole things will be divided by ajj so in this case we have to split the whole while loop and then for the for loop into three parts when j is equal to one j is equal to n and other than that now this is the end of if loop and this is the end of for loop then i will find the error so this is the norm i am taking so x nu minus x sold and i am taking the maximum now so that is the error i am taking then after this i will whatever the value we are getting from this x nu i will use this x nu to update my old value so that is this x nu will go to the x volt now iteration will be increased by one so earlier iteration was zero now it is one so after one iteration whatever the solution you are getting you will get this now this is i have written this one to find out that how the accuracy and how the solution is approaching to converge so i have displayed only two cases when my matrix is three by three or my matrix is four by four because other than it is very difficult to display on the screen so if if n is equal to three i print at iteration so x one so i am suppose i have three by three system so my solutions are x y z so i put x at this iteration here y at this iteration and judd that iteration and this is the error i am finding so i will put this here as iteration x old one because all the solution is going to this as a vector so that first component will be i am treating as x then the second component i am treating as a y and the third component i am treating as a jet so this is the z i am finding and in the last i am finding the displaying the error also and that is the acceleration is a new line so this will end and if n is equal to 4 the same thing is happening except the last one i will take that as a w and then solution will be x old four and this is the error so i will display this one on the screen and this will be updated at each iteration and that will be display in the screen as in the form of a table and then i will end this while loop and then x is equal to x nu because i have to send back this x output so this my action after all this iteration my action will go and it will save in the x and this x will be returned to the the system or return to the code from where this function has been called so this is the function we have made for cedar method now i will want to use this one so let us i will use matlab code we have used for the gauss jacoby so this is my clear all clc now in this case i will start with the the matrix this one so what i do i will take itati method and let us take this system and for the gauss jacoby also we have discussed that this ma is going to be diverging so let us do this one now after doing i will make the code because from here to here i have used this one for the gauge kobi because this is the gojoko only change we have done is that here we are calling with the gauge kobe now i am doing the same thing for the gauss seidel so i have made this code applicable for all the gauss elimination gauss ah jacobi and gauss-seidel so in this case here i am taking three by three system so this is three by three system so i am taking the initial condition as one one zero and this is the dash means the it is will be a column vector the tolerance i am taking ten is to power minus five now i will call them function x is equal to gauss seidel from here so this is the input i am passing then after that it will give me the x so the i will show that the display the solution of the gain system in this one now what i want to do i want to find the convergence matrix so that is the convergence matrix n is the length of b l is the left matrix these are diagonal matrix u is upper triangular matrix so l is the lower triangular matrix so i will putting the value of the given matrix into the corresponding lower diagonal upper triangular matrix so it is i am putting for loop i from 1 to n and j from 1 to n now if i is less than j it means i am putting that if i is one then if i is one then j will move from two to n ok so one is i is less than j then i will getting the values as a upper triangular matrix ok so now if i is equal to j then this will saved in the diagonal matrix as a diagonal matrix and this is the else value so if it is not this or not this then whatever is left that will saved in the lower triangular matrix ok so this is the way we can split the matrix a into the lower triangular upper triangular diagonal matrix now from the gauss hidden method i know that my convergence matrix is minus inverse l plus d multiplied by u now the display the eigen values of the corresponding matrix i find the eigen value of this matrix so that will show the eigen value then i find the spectral radius so spectral radius i am finding from here and i call that a spectral red so that is what i am doing i am finding the eigen value then i take the absolute value because the eigen value may be complex also so in that case i will sub i will find out the absolute value of that and then i am finding the maximum fall now i am from here i am displaying that the spectral radius and the rate of convergence of the given method is spectral radius will be this one and minus log of the spectral radius that is the rate of convergence so let us try to run this one so i will run this one and so let us see what will happen so from here if you see it started with the iteration number one then x iteration y iteration z iteration so this is my initial solution and the error this then it is keep increasing then at the station number 2 the solution goes to minus 14 then minus 161 and you see that the value is going to be infinity minus infinity in this case and the error if you see then the error is going to be increasing and going to be infinity so in this case after 100 iteration i stopped my program and then this is the solution and in this case the i can see that the method is not converging it is diverging so this for the given system equation our our syst our method is diverging and from here you can also see that the if you find the the eigen value then it is coming almost 10 eigenvalue so the eigen value is coming tan because if the system is going to converge then we know that the eigen value is going to be less than one the modulus of that eigen values so this is the the first one we have taken now i will change this one and then i will take this system so this is also three by three system taking and then i run this one so let's see what will happen now from here this is my solution so the initial guess i started with one one zero now ah after the semi iteration you can see that my solution is this x is converging to 2 this is connecting to 4 and z is converging to 3. so that is my system the solution of this system and then if i find the eigen value so eigen value is coming zero so zero eigen value means this matrix is singular matrix then its value is minus point one and this is the maximum eigen value this one so the maximum eigen value is point one two five zero so that shows that this factor radius is this one so using this spectral radius so that is my spectral radius and you can see that the rate of convergence is very high that is two so with this help with this gauss seidel method i am able to find the solution in the same iteration only now from here you can see that what will happen if i change the initial approximation so let us try to change the linear initial approximation suppose i take x 0 is equal to 1 0 0 or what will happen if i take all the zeros then let us see what will happen and if we see from here then if i take 0 0 then the solution is still converging to this one within the 7 iteration and the same eigen value definitely will come but in this case the iterations are similar the same iteration so i told you that that you can choose any initial condition whatever the initial condition you want to choose now suppose i put it 0 0 0 and then 1 then let us see what will happen so in this case also if you see 0 0 1 the same initial condition ah means changing the initial condition but the number of tradition is same and the rate of convergence so this is the way we can find that how for the different initial condition our method is converging because from here we can see that if i take the convergence matrix then the eigen value of convergent matrix is less than 1 the modulus of that one so and the smallest the value the faster the method will be so in this case the eigen value the maximum eigen value is point one two five so that is why you can see that the order of convergence is quite high and that is second order method in this case so this is the three by three system i have taken now let us try to take the another system so let us take four by four so four by four system but in the initial condition i have to change so this is the initial condition four by four so i will take this one because that vector initial vector should be of four component so this is x y z and w and the tolerance i am changing not changing i am keeping the same tolerance so my system is now four cross four so let us run this one and from here if you see now i started with the four by four matrix and the initial approximation of the solution i am starting with one one one zero now you can see that iteration number one this value is at the initial point then after the iteration each iteration this is converging very fast even after the fifth iteration you can see that it is going close to the root and after few iteration that is the iteration that our solution is now reaching to the tolerance so in this case my x component has 0.49 so 0.5 is the value 0.27 minus 0.52 and 0.37 so that is this one and error you can see that my error is point zero zero zero two so it is less than the given tolerance so that is why we stopped here and now from here you can see that the eigen values i am finding so these are the eigen values i am finding and from here you can see that one eigen value is finding is 0 another is the complex eigen value x component is this and y component is this now again and we know that the eigen values if they are complex they appear in a conjugate form so that is the conjugate of this complex number and then i am getting the another real eigen value that is point one six four five so now from here i take the modulus absolute value of this one so after getting the absolute value i find that the spectral radius is coming this because this is the e value i am getting so e one is zero so i i just check that what will happen absolute value of e two this point see this is the point two one nine one so that is the absolute value of this so this is and then using this one i find the order of con rate of convergence so rate of convergence in this case is 1.51 so because here the eigen value the spectral radius has increased in the previous case it was 0.1 now it is 0.21 so the the it is going close to means as compared to the previous one it this value is greater so that is why the order of convergence or the rate of convergence has decreased from 2 to 1.5 so this way we can find the solution for 4 by 4 system now the same thing i can take now what i do i will reduce the tolerance now i keeping the same initial condition but i am reducing the tolerance so let us see what will happen so in this case now you can see that the num the tolerance i have decreased by one factor only now the number of station has reduced from the previous one so it is now it is seven iteration and the solution is is accurate up to four decimal so this is so less than point zero ten is one minus four so now this is my error so number of iteration is reduced and this is my solution now from here you can see that the because this is no not going to change with the with respect to the initial condition or the tolerance so that is not going to change and the rate of convergence will be same that is 1.5 only the change comes will with the number of iteration that so in this case the number of creations are same now what will happen lets see if i change the initial condition one zero one zero so in the in this case one zero one zero the same number of iteration we are getting now point seven or the iterations are seven and that error has reduced from point in the previous one it was point zero zero three now it is point zero zero two so the solution is this one so that is my approximate solution after same iteration so in this case the solution is converging because you can see from here that after three iteration you will get the knowledge of that what will be the solution see the power of this method that even after three iteration you can see that the solution is converging toward point four nine this is converging to point two seven this is converging to minus 0.52 and that is 0.37 so even after 3 iteration you are able to see or guess that where our solution is going to have to lie so that is the power of the gauss-seidel method now so in this case i may so that is four by four system we have taken now let us take another system so let us take another 4 cross 4 system so i will write in front of you so let us take the matrix a that is equal to suppose i take the matrix 1.2 then 2.3 then 0 and then i take 1 so that is the first row i take then i take minus 0.1 then 0 then 0.5 then minus 1.5 so this is the another row i am taking then i take the another row zero one zero point two so this is another one and i now i take the last row so last row is point two five minus point three zero zero so let us take this system where the matrix is this and then i take the right hand side vector so b i am taking one zero point three one point five so let us take this one i am just taking the randomly so let us try to solve this one so i run this one and then see what will happen now from here the matrix in the it is coming that the matrix is singular and so you can see that even after the first iteration this solution x is giving and the y z and w they are coming not a number so let us change this matrix or maybe i can take this initial condition change in initial condition so the same thing so let us see so the same thing is happening now it means that this system is not soluble with the help of gauss-seidel method maybe i can change this matrix into the simpler one so i take one one point two three so let us take three by three matrix so i just put three plus one four and five point two then i take one point one zero and two and then another take i take four minus one and three so let us take this system three by three and i just on the right hand side i take one the summation of all the elements in the row so one three four five so i take five point two then the next one i take one point one zero and two so it is two plus one three point one and then the last one is four plus three seven minus one six so let us take this one so its its answer should be 1 1 and i take this one as zero so let us take this and try to find the solution for this so let us take the the previous case let us see what is happening because it may happen that i have now it is giving the solution for the the given matrix so in this case the the solution is there actually maybe i am not taking a matrix with a diagonal dominancy so let us so i am taking this so let us change this one to diagonal dominancy because our matrix if the matrix is diagonal dominant so let us take this as a three and this is one so i take one point one and then two and then zero ok and then i take this three and then i take four maybe so in this case this this is the diagonal dominant 1.1 2 0 so that is the matrix and 3 minus 1 so maybe i can take this one so let's see what will happen yeah so now it is giving the solution so after the 12 iterations you are getting the solution so that was the problem because i have chosen a random matrix and that was not diagonally dominant so from here you can see that the solution is going to be 1 close to 1 and the matrix was singular so it is 0 so both 2 eigen values are 0 basically and this is 0.51 so if the it is going to be very large as compared to the previous cases so in this you can see the rate of convergence has increased to be even less than linear value so it is 0.6 value so now we will stop here so today we have discussed the matlab code for gauss seidel method and with the help of gaussian matter we have tried to solve the system three by three system or four by four system and we also found that the system is not dynamic dominant then the it may or may not converge so in that previous case it was not converging but when we make the matrix as a diagonal dominant then it was giving the solution in the 12 iteration so in the next lecture we will continue with the other problems so thanks for viewing thanks very much [Music] [Music] you