 Hi, we have completed iterative methods for non-linear equations. In this lecture, we will try to implement Newton-Raphson method. Let us start our discussion with developing a Python code for Newton-Raphson method. Let us recall the Newton-Raphson method along with what are all the inputs we need and also what we want to take as a output from the code. We need the following inputs, first of all we need the function f which defines the required non-linear equation f of x equal to 0 and if you recall the formula for Newton-Raphson method also includes the derivative of the function f right therefore, we have to take f dash also as the input these two are taken as exact expressions and then we need an initial guess for the root of our equation that is taken as x naught and then once we start generating the iterative sequence well we have to stop the iteration at some point right. For that we will take the tolerance parameter epsilon and we will try to check the Cauchy criteria to stop the iteration. Let us quickly recall the Newton-Raphson method. The Newton-Raphson method formula is given like this right remember we are given x naught and from there we will get x 1 using this formula and once you get x 1 using x 1 we will get x 2 and so on. So, this is how the iteration should go remember x naught is taken as the input that you will plug in here in the first iteration and then already we have taken the expression for f and f dash therefore, x 1 can be calculated using this expression and then using x 1 you will get x 2 and so on. So, in that way we have to formulate a loop which will run for each iteration right. Now where to break the loop and how to break the loop well for that we need the stopping criteria well for the stopping criteria we will use the Cauchy criteria and finally, what is the output the output should be the value of the iteration x k plus 1. I hope you understood the idea of how we have to go ahead in coding the Newton-Raphson method let us now see the code. Let us start with the input we have to take the function f which defines the equation f of x equal to 0 right and that is defined here you can see that I am taking f of x is equal to x minus 1 the whole square into x minus 5. From this itself we can clearly see what are all the roots of the equation f of x equal to 0 the roots are x equal to 1 and x equal to 5. The next thing is we have to also input the expression for the derivative of f that I am taking in the variable f d. Remember these two are going to be considered as exact expressions by supplying the value of x you can evaluate f of x and f d of x right. In the next input is the array x x has two components x of 0 remember in python the index starts from 0 x of 0 and that is assigned as 2 in this case and then x 1 is initialized as 0 here that is what this line means and then we have the epsilon which is the tolerance parameter I have taken it to be a very very small number. So, that I want a very good accuracy for my root. So, these are all the inputs as we have seen previously now let us go into the main part of the code that is the iteration process. This is the main loop of my program, but before getting into the program I am just doing one iteration outside why because I have to check x 1 minus x naught right. So, for that I need the value x 1 that is why once I am doing the iteration outside the loop and then I will start the loop by checking the stopping criteria. So, this is my stopping criteria. So, when I enter into the loop I have to first have the value of this error that is why x 1 alone is computed outside the loop and now you can see how I am computing x 1 remember x 1 is nothing, but x naught right minus f of x naught remember f is given by this expression and by sending the value of x naught I can find the value of this expression by putting f of x naught that is what it means and then divided by f dash of x naught, but how we have defined the expression for f dash well we have defined it by f d right therefore, I will have f d of x naught. So, that is what precisely I am writing in this expression right. Once I compute the value of x 1 then I will go to compute the absolute value of the difference between x of 1 and x of naught this is precisely the absolute error right which I want here because I want to check this stopping criteria whether it is less than epsilon or naught that is precisely checked in this expression. So, I am initiating a while loop here and the while loop runs whenever this error is greater than epsilon our loop will keep on running then it becomes less than epsilon then the loop will break and we will come out of the loop. Now, let us see what are we computing inside this loop as you can see there are 3 lines in the loop why there are only 3 lines because we have given this indent here right. So, whatever is given with indent or considered to be the part of the loop. So, we have given only these 3 lines with an indent therefore, these 3 lines are considered to be the part of the while loop and this will keep on going again and again till this condition is satisfied. Once this condition is violated then it comes out of the loop ok let us try to understand what is happening inside this loop see ideally we should have many components of the array x because we have to take x naught as a input then find x 1 and once you find x 1 you find x 2 and so on right. So, we need many components of the array x, but what we observe is once you get x 1 to compute x 2 we only need x 1 right we do not need x naught in order to compute x 2. Therefore, why to waste the memory for so many components of x when we need only x k to compute x k plus 1 right that is why what I am doing here is once I computed x 1 and also I found this absolute error I do not need x naught anymore. Therefore, I put the value of x 1 which is computed here into the variable x naught in that way I have made this memory for x 1 free now. The next iteration which is given like this is now computed and stored in x 1. Therefore, I just compute x 2 right which is actually x 1 minus f of x 1 divided by f d of x 1 right, but what I did is I took x 1 and put that value into x naught. Therefore, I will simply write x naught instead of x 1 right I hope you understood the logic here because I stored the value of x 1 in x naught I am using this variable now x naught remember this is holding the value of x 1 now because of this line right and I do not need to create one more component for the array. So, what I am doing is I am simply using x 1 here. So, that is why I am having this expression. So, this will overwrite the value that is already computed here it does not matter we do not need that value anymore that is why I am doing it. Once you have x 1 again you go to find the absolute error in x 1 when compared to x 2 and now once you have the new value of the error again you come to check whether the stopping criteria is satisfied or not if the stopping criteria is not satisfied that is if the absolute error is still greater than or equal to epsilon again you will go now remember x 2 is actually stored in x 1 only right it is stored in x 1 that again you will rewrite with x naught and thereby x 1 is again made free and you will now store the x 3 value in x 1 by again doing this computation and then you will go to check the error like that it will keep on going it goes check the error and then does this computation again goes and checks this condition like that this process will go and this is the iterative process for the Newton Raphson method and now we are into the output of the code. Let us see what we have to take as the output as far as the output is concerned we will take the approximate root of the equation as x 1 when the loop breaks right when loop breaks you have the recently calculated value stored in the variable x 1 right and that will be finally printed on the screen as the approximate root of our equation I am also giving the residual error just for our information. Let us run this code to see what is the output remember we have the equation f of x is equal to x minus 2 the whole square into x minus 5 and that is equal to 0 that is our equation and we started our initial guess as x naught is equal to 2 right I am sorry this is actually x minus 1 right. Let us see what is the output of this code you can see that the approximate root is printed finally and that is given by approximately equal to 1 right. Therefore, when we start the iteration with the initial guess as 2 then the iteration seems to have converged to the root x is equal to 1 right that is what we are seeing you can also see that the residual error is pretty small and this is what we would like to do in the Newton-Drabson implementation. Thank you for your attention.