 Hi, in the last lecture, we have studied the convergence theorem for bisection method. In this lecture, we will continue our discussion on the convergence of bisection method. We will prove an interesting corollary of the convergence theorem of bisection method and then we will also see how to implement the bisection method as a python code. In the last class, we have proved this convergence theorem where we have assumed a function f which is defined on a closed and bounded interval a naught comma b naught and the function f is continuous on this interval and the points a naught and b naught are chosen in such a way that the sign of f at a naught and b naught are opposite. Then, we have seen that the sequence generated by the bisection method converges to a root in the interval a naught b naught and further we also proved a nice estimate for the error involved in the bisection method at every iteration. The error estimate is given like this. Now, from this error estimate we can also derive an interesting formula that is what we are going to see in this lecture. Let us take this formula in the form of a corollary. Let us take a real number epsilon greater than 0 and let us assume all the hypothesis that we have assumed in our convergence theorem and also let x n be as in the bisection method that is x n or given by x n is equal to a n minus 1 plus b n minus 1 divided by 2. That is how we defined x n and r is the root of the non-linear equation f of x equal to 0 for which the bisection method is converging. So, these are given to us then what the corollary says is that if you take the integer n greater than or equal to this number then surely mod x n minus r will be less than or equal to the number that we have chosen that is epsilon. How can we use this corollary well? Suppose you say that I want my approximate solution x n to be very close to the exact solution remember we do not know what is the exact solution. We only know what is x n because we are computing it through bisection method. Now, what we are demanding is that I want my approximate solution x n to be very close to r. How close it is? Well it should be less than or equal to the epsilon that I have given. See epsilon is something which we take say suppose epsilon you take as 10 to the power of minus 5 or something then I want to compute the iterative sequence of bisection method up to some n such that x n minus r should be less than epsilon. If so, what is the what is that n? That is how many iterations should I compute in order to make my error? This is the error you see in order to make my error less than or equal to epsilon. If you ask this question then our corollary says well you have to at most compute this many iterations. What is that? That n should be an integer greater than or equal to this number. You see the interesting part of this number is all the quantities involved in this expression are known to us b naught and a naught are given as the input to our method epsilon is something which we have chosen generally called the tolerance parameter and of course, log 2 is a constant you can always compute. So, you can in fact tell how many iterations are needed in order to make sure that the error involved in x n when compared to the exact root is less than epsilon. That is the interesting part of bisection method. Let us prove this corollary the proof of this corollary is not very difficult you just take this estimate which we have proved in our convergence theorem. Now, what we want? We want mod x n plus 1 minus r should be less than or equal to epsilon. Now, we can achieve this by imposing the condition that the right hand side is less than or equal to epsilon. That is all is the idea to make sure that x n minus r is less than or equal to epsilon. We will impose that the right hand side is less than or equal to epsilon. Now, what we want? We want this n that is all we want that can be obtained by taking logarithm on both sides. Now, I will leave it to you to derive the expression by taking log on both sides and then finding the n from that expression. I will leave it to you as an exercise. Now, let us take an example. Let us consider this equation sin x plus x square minus 1 equal to 0. Now, I want to use bisection method to approximately find a root of this equation. Now, what I want? I will tell you that I want my approximate solution x n to be such that x n minus r is less than or equal to epsilon and that epsilon is taken to be 0.125. This is what I want. Now, my question is how many iterations should I compute? Now, you can use the above formula to find the number of iterations. One way is that you can just go on computing the iterations and at every iteration you check the number x n minus r, but for that you need to know the root r, right, but we actually do not know the root. That is the problem. So, you just cannot check the condition that we need, but we can use our formula to actually give n without going to the explicit computation of the iterations. Now, for that remember first step is to choose the interval a naught comma b naught in such a way that the function f of x equal to sin x plus x square minus 1 takes opposite signs at a naught and b naught, right. Let me take the interval as 0 comma 1. You can check that f of 0 is negative and f of 1 is positive. Therefore, the interval 0 comma 1 can be a choice for our initial interval. Now, coming to the question of how many iterations are needed? Well, we will use this formula that we have derived just now with b naught is equal to 1, a naught is equal to 0, epsilon equal to 0.125, right. For that you can see that the value of the expression is nearly 3 and therefore, you may have to compute at most 3 iterations. Remember it is at most 3 iterations. Why? Because we are using an upper bound, right. That is the worst error that we can have in the computed root. That is what the estimate says. Therefore, you need to compute at most 3 iterations in order to achieve an accuracy such that x n minus r is less than or equal to 3. In this you see we do not know what is r, right. Without knowing what is r we are able to conclude this. Let us go ahead and use bisection method for the given equation and compute 3 iterations and see how it goes. Well, for the first iteration we have to give our interval a naught as 0 and b naught as 1, right. And now for x 1 we have to use the formula which is nothing but the midpoint of the interval 0 comma 1 that is nothing but 1 by 2, right. Now we have to see whether the function f changes sign in this interval or in this interval, right. For that you have to find f of 0 you can see already it is less than 0 and f of 1 is greater than 0. Now f of 1 by 2 that happens to be minus 0.27 that is what we have computed here. Therefore, this is also less than 0 that shows that the root is not going to lie in this interval that is the root that the bisection method is trying to capture, right. That is not going to lie in this interval but it will be lying in this interval that is what we can see from this. Therefore, we will discard the interval 0 to 1 by 2 and we will only take the interval 1 by 2 to 1 and then we will proceed to the next iteration and see how the next iteration is going to go. For the next iteration you have a 1 equal to 1 by 2 and b 1 equal to 1, right. Again you have to take the midpoint of this which is nothing but x 2 equal to 0.75, right. And what is the value of f at x 2 and that is 0.24 which is greater than 0. Remember f of 1 by 2 we have seen in the previous iteration that it is less than 0. This is greater than 0 and similarly f of 1 is also greater than 0. Therefore, you have to discard this interval and take the interval 1 by 2 to 0.75. Therefore, a 2 is taken as 0.5 and b 2 is taken as 0.75 and that completes our second iteration. Remember our analysis says to achieve the required accuracy you have to do utmost three iterations. So, we will do three iterations. Let us go to the third iteration. You can see that the midpoint of the interval 0.5 to 0.75 is given by 0.625 and that is it. We will stop here because we want the solution with accuracy only 0.125 and our analysis says that you just have to carry three iterations. We have done the third iteration and take that value as the root. The problem ends here, but let us cross check whether what our analysis says and what is the reality, right. Our analysis says that you have to stop at the third iteration. Let us see what is the reality. The true value of the root is actually approximately given by this value 0.636733 and what we got is 0.625. Therefore, the error involved in x 3 when compared to r is something like 0.01 which is well below what we can tolerate, right. We told that we can tolerate epsilon which is equal to 0.125. For that our analysis said that you do utmost three iterations. We did three iterations and we saw that we got the accuracy. In fact, more we got that does not matter. We want at least this much accuracy. For that utmost we need to do three iterations. This is what the analysis says and the reality is well respecting the analysis that we did, right. So, with that we finish our discussion on bisection method. We will develop a python code for bisection method. Well, let us go to the co-lab to develop our python code. Let us quickly recall what are all the inputs and how the method goes and what should be the output. For that let us first see what are all the inputs needed for the code. Well, we need to give the function f that defines our equation f of x equal to 0 and then we have to give the initial interval, right. For that we have to give a 0 and b 0. Also we will take the tolerance parameter epsilon that we will take as per our wish. There is no special property for epsilon. You can choose this arbitrarily, but if the understanding is that you will give epsilon very small that is the understanding. Then the method goes like this. Remember that we have to choose a 0 and b 0 such that f of a 0 into f of b 0 is less than 0. In our code we are not asking the computer to do that, but we are choosing such a 0 and b 0 and supplying those information as input to our code. In that way our code is very simple. The method goes like this for every k equal to 0, 1, 2 and so on. We know what is the interval in the previous iteration, right. You just take k equal to 0 and understand this. You know what is a 0 and b 0 because that is given as an input and then you will compute x 1, right. That will be the midpoint of the interval a 0 and b 0. Similarly any k equal to 0 or 1 or 2 anything the understanding is that we know what is a k and b k. From there we will find x k plus 1 and then we will go to check the condition that whether f of x k plus 1 is less than z equal to 0. If it is so, then we will come out of the iteration, declare that x k plus 1 is the root of the given equation. Remember mostly this condition will not be satisfied because on a computer some rounding error will be involved. Even if that quantity is going to be exactly equal to 0 mathematically it may not be so on the computer quantity, right. Therefore, mostly this condition will not be satisfied in your code, but just for the sake of completeness we will just check this. If this condition is not satisfied then we will go to check whether f of a k comma f of x k plus 1 is negative. That is if there is a sign change between a k and x k plus 1, if so then we will take the interval for the next iteration as a k plus 1 equal to a k and b k plus 1 equal to x k plus 1. If not, suppose if this is not satisfied then surely your root is going to sit in the second half of the interval a k comma b k, right. Therefore, we will take the interval for the next iteration as a k plus 1 equal to x k plus 1 that is this one and b k plus 1 equal to b k. So, this is how we have built our algorithm in our previous class itself. I am just trying to recall it here. Now, coming to the stopping criteria we have to check whether b k minus a k that is at every iteration the length of the interval is less than epsilon or not. If the length of the interval is less than epsilon then you will stop the iteration. If it is not less than epsilon then again you will go to the first step, you increment your k value and then find the midpoint of that interval a k comma b k and then you just check for where the root lies and then this process should go on in a loop. Now finally, when the stopping criteria is satisfied then your loop has to be stopped and then declare the value x k plus 1 as an approximation to the root of the equation f of x equal to 0. This is what we are going to do in this code. Let us see how to develop the code. First part of our code is the input. Here we have to take our function f for that the format is f equal to this is something which is a part of this command and then x up to this is the part of the declaration of a function. Now, from here onwards we are starting the expression of the function. We are taking the expression of a function as x minus 0.09 into x minus 0.15 into x minus 0.063. It is a cubic polynomial. You also know what are all the roots of this polynomial. The next is we have to give the initial condition a not equal to 0. If you give a not equal to 0 you can check that f of 0 is negative and then we are taking b not equal to 1. You can also check that f of 1 is positive. Therefore, there is at least one root lying between a not and b not. Now, let us take the tolerance parameter epsilon is equal to 0.0001. So, I want to continue my iteration till my computed value x n minus the exact root r is less than epsilon. That is what I want, but we have seen at least in the bisection method it is enough to check the length of the interval. It is enough for us to check the length of the interval. If the length of the interval goes below epsilon then surely your computed value will be close to the exact root of the equation whatever it may with error which is less than this epsilon. Now, let us see how to code the iteration process. Well, we have already taken b not and a not. Therefore, take the error e r r as b not minus a not and if the error is less than epsilon then we do not need to compute anything. We will just come out and say that x is equal to a not plus b not by 2 is the root of the equation at least approximate root of the equation we will say. Therefore, if this error is less than epsilon that is this part is less than epsilon then process will not get into the loop at all. If that number is greater than epsilon then the process will get into the loop and let us see what the loop does. It first finds the value of x which is the midpoint of a not and b not that is actually the formula for the bisection method. Therefore, if you are developing a code for some other bracketing methods like we will be seeing regular falsely method in the next class. So, if you want to develop a code for regular falsely method the only line that you have to change is this line where you have to substitute the formula for bisection method with the formula for the regular falsely method. Otherwise, the algorithm reminds the same. So, since we are developing bisection method the formula is given by a not plus b not by 2. Now, you see we have to check three condition if f of x itself is 0 then just say that the root of our equation is x. So, for that you have to write this line. So, what the processor will do if this condition is satisfied then it will print root is equal to whatever may be the value stored in this variable x it will print that value and then comes to the next line. What next line says next line says break the loop whenever the processor sees break it breaks the main loop in which it is currently running. It is now currently running in this loop whatever is the body of this loop it is given with an indent here. So, you can see there are two points it means this line is written with an indent and therefore it is a part of this while loop and similarly this if condition is a part of this while loop and these two are written with two indents. Therefore, these two are part of this if condition once it comes into this if part it will break the loop and comes out. It comes out and then simply it will go and compute this, but then when it comes to this it will check whether f of x is not equal to 0. If f of x is not equal to 0 then it was an approximate root otherwise it is since you are coming from here that if condition is not satisfied. Therefore, it will not get into the if condition it will print the residual what is the residual error is equal to it will compute f of x. What is x? x is computed here because you came from this if condition you have directly jumped to this part. Therefore, it will take the value of x computed there and it will supply that to the expression f given here and it will find the value of f as per the x computed here. So, that will be the residual error all this will happen if this if condition is satisfied. As I told you most likely this if condition is not going to be satisfied almost sure that is you can say that the probability that the computed value f of x is exactly equal to 0 on a computer is almost probability 0 right because of the rounding error. Therefore, it mostly it will not be satisfied. So, your control will only come to the next line which says else if that is if this is not satisfied then what else if you check this condition f of a naught into f of x is less than 0 you keep the algorithm in hand and then see the program then you will understand f of a naught into f of x is less than 0. If so, then a 1 should be equal to a naught but I do not want to spend one more memory to store a 1 because once I have a 1 and b 1 a naught and b naught are not going to be used at all. Therefore, I will always update my interval of the next iteration in the same variable a naught and b naught. You can carefully observe that once iteration is over then we will never use a naught and b naught right we will have a 1 and b 1 and that only will be used to get a 2 b 2 like that it goes on. Therefore, on a code you in order to minimize the usage of memory you just simply store the value of a 1 and b 1 in the variables called a naught and b naught itself this is just to minimize the usage of the memory. In this case I have to take my a 1 equal to a naught right which I am not going to do whereas, b 1 I should take as x. Since I am going to use the same memory for a 1 and b 1 so, I am storing x in b naught only if that also does not satisfy that is if this condition is also not going to be satisfied then I will go into the else where b naught is retained as it is whereas, a naught is over written by the value x. Therefore, once the loop is done then your interval is bisected and you are getting that part of the bisected interval in which you have at least 1 root right and now you go to check the length of that interval. Remember now it is b 1 minus a 1 theoretically why I wrote b naught minus a naught simply because I want to minimize the usage of memory. Therefore, I am not using a separate set of variables a 1 and b 1 I am just using the same old variable, but the values stored in this variable are now updated once. Therefore, the error now will be surely different from this error right it will be half of that error surely. Now again once the error is done that is all this loop is over how do you know that the loop is over it is not because of this line these lines will not be executed at all these lines are not going to indicate that your loop is over, but any line which starts without an indent is the line which is outside this loop. So, this is the first line which has no indent therefore, this is the first line that lies outside this loop. So, when the control of the python code executes this line then it sees that that is the end of this line. So, it goes back to the while loop and then checks once again the condition. Now when it checks the condition the value in the variable err is now stored with this new value. Now the value in this err is the one which is now newly computed right. Now it will check whether that value is greater than equal to epsilon if. So, it will again continue to execute all these lines and it will keep on going at some stage when this interval length becomes less than epsilon then it breaks the loop comes out and takes the midpoint of that interval and declares that as the approximate root of our equation. And of course, it will also find the residual error at this x now it will find at this x. Let us try to run this code and see what is the root that the bisection method is capturing if f is given by this expression and the initial guesses are a naught equal to 0 and b naught equal to 1. As I told you when you run the code for first time it has to make some space in the server. So, it takes some time for the first run only well it has given a space in the server and it also ran the program. The approximate root that it obtained is 0.1499 and so on you can see that 0.15 is a root of this polynomial. So, the bisection method with this initial condition is converging to the root 0.15 and the residual error is something really very small. So, that is very nice I am just giving you a slight variation of this program here where I am not telling you what is the tolerance error that I want to take rather I am telling you how many iterations I want to perform in the code. Then what you have to do is you can simply use the for loop the format of the for loop is that for k is equal to 0 to n minus 1 that is once it gets into the for loop it takes k equal to 0 it performs whatever is given under the for loop and then goes back take k equal to 1 does the loop then k equal to do does the loop like that it does still whatever n value that we have given. And once it performs that many iterations it comes out and gives the answer. So, let me not explain this you know how this main part of the program goes on the only difference is now the way the loop is made previously we made a while loop where we have checked the error now we have made a for loop where we have specified how many iterations we want to run and then we made the loop to run that many iterations. I hope you have understood this code you can also run this code and see if you want to play around with different functions you can change this expression you can also play around with different intervals and see how the convergence is happening. Thank you for your attention.