 Welcome back, in the previous segment we discussed numerical integration. In this segment we are going to discuss the bisection method for finding roots. So the root of a function f is a value x such that f of x equal to 0 or in other words it is the point where f of x, the graph of f of x crosses the x axis. Many problems can be expressed as finding roots. For example, if I want the square root of some number w then I claim that that is the same thing as the root of the function f of x equal to x squared minus w. Why is that? Well, if we do find the root that is if we find f of x such that, if we find x such that f of x equal to 0 then we know that x squared minus w equal to 0 or x must be square root of w. Now the bisection method has relatively few requirements as to when it can be applicable. So for example, we require the ability to evaluate f given an x. So we should be able to calculate f of x given an x. Then we want f to be continuous and then we must be given points x l and x r such that f of x l and f of x r are not both positive or not both negative. So these are three relatively simple requirements but once we have these then we can use the bisection method and the bisection method can be used for finding roots but as we just remarked finding roots is really itself useful for finding for calculating functions such as say the square root in this case. So again to reiterate we need to be evaluate f, f must be continuous and we must be given points x l and x r such that f of x l and f of x r are not both positive or both negative. Or in other words what must be the case? So if we plot the graph of x, a graph of f, so this is the x axis, this is the y axis and let us say this is the point, this is the point with x coordinate x l, this with x r. Then the points f of x, the value of f at x l say it is here then x r should be on the other side or it could be at 0 but both of them cannot be on the same side. So that is what the point is, that is what the requirement is. So I am going to describe the bisection method now but we will assume these three properties. So in other words we will assume that we are given x l and x r satisfying this property. So the basic iteration of the method is as follows. So we will start with the user given x l and x r and then we will bring them closer. As we bring them closer we will maintain the invariant then that x l and x r must have different signs or they are 0. If they are 0 it does not really matter. Now I want to observe that at the beginning this invariant is true that is because the user gave us two such numbers. Now before proceeding let me discuss the implication of this. So suppose f of x l is indeed positive and f of x r is negative. Now because this function is continuous what does it mean? The graph of this function must start somewhere over here maybe it will increase, maybe it will do whatever it needs to but it is continuous. So this graph, this line must be continuous and beam and while it is continuous it must still reach this point. So what does that mean? That means it must cross this x axis at some point otherwise it cannot go to the other side. But the point at which it crosses is exactly the root. So what we know is that the root must be contained in this interval. So effectively when we set to the user please give us x l and x r the user is effectively giving us an interval which contains the root. So invariant plus continuity implies that the root exists between x l and x r and the root might be exactly at x l or exactly at x r. So f of x l or f of x r are allowed to be 0. So we are going to execute this basic iteration until the distance between x r and x l becomes smaller than some error bound let us call it epsilon. If epsilon is really small then these two numbers are really close and the root is somewhere in between them which really means that we can declare one of those numbers say x l as the root. So we will have error in this but the error is at most epsilon. So that is the whole that is the whole that is the gist of the argument. So we are going to get we are going to get an approximate answer but you will see that the approximation can be as good as you want no matter what epsilon you give us we will be able to iterate until that point. So let me tell you now how this iteration is going to happen how are we going to shrink this interval. So first we calculate x m which is x l plus x r divided by 2. So what is that well that is the midpoint of this interval x l to x r. Next we check the signs of x m and x l. If these two signs are the same then we are going to set x l equal to x m. So again let us come back to this picture. So this is x l x r we find the midpoint which is over here then we look at f of x l. So the sign of f of x l and the sign of f of x m. So this is x m. So the sign of f of x m is positive and the sign of f of x l is also positive. So that means f of x l and f of x m have the same sign. So in which case we will execute x l equal to x m or in other words we will move this point over to this point. So I claim now that this is going to keep our invariant without violation. So why is that? Well x l and x m had the same signs. So the signs did not change as we moved x l over to x m. As we assign x l equal to x m the signs did not change. So that means the sign of x l continues to remain different from the sign of x r. So if it was different before then it will continue to remain different. But we are assuming that this invariant was true at the beginning of the iteration and therefore what we have proved that in this case the invariant will hold even after the iteration. Or in other words the root will now we contain in this smaller interval which is true in this case in our picture. If this condition is not holding that if x m and x l do not have the same sign then we are going to set x r equal to x m. So why is that? Well in that case x r must have the same sign as x m and so again we can conclude that the sign of x r has not changed. And so even in this case we can conclude that the invariant is going to hold. So basically what we have is a simple method by which we can shrink this interval. So let us now see the code for it. So we are going to write this code for the problem that we mentioned earlier which is finding the square root. And we will make the problem very concrete by saying that we want to find the square root of 2. So in that case as we discussed earlier we should be finding the root of f of x equal to x squared minus 2 because if x is the root of this then x squared minus 2 must be equal to 0 or x squared must be equal to 2 or x must be square root of 2. So the root of this equation will indeed give us the square root of 2. So first we need to supply x l and x r. So I claim that x l equal to 0 will work. So let us see what that means. So x l equal to 0 means f of x l equal to 0 minus 2. So f of x l is negative. So long as we can supply an x r such that f of x r is positive we will have satisfied our requirement. So we are going to set x r equal to 2. So f of x r is 2 squared or 4 minus 2 and therefore it is positive. So our x l and x r are really satisfying what we set out that the science of f of x l and of x r are indeed different. So then we need to have a variable to store this x m value and we need an epsilon as well and let us say just as an example that we pick epsilon to be 10 to the power minus 5, 10 to the power minus 4. I am sorry 10 to the power minus 5. Anyway it does not matter. Now we are going to just have to write our basic iteration and what is our basic iteration? Well we check whether the interval x r to x l or the length of this interval which is x r minus x l is bigger than epsilon. If it is bigger than epsilon then that means that our interval is still larger than what we want. So in that case we should execute our basic step. So what is our basic step? So we are going to find x m which is the midpoint of x l and x r and so it is x l plus x r by 2. Then we are going to check whether x m and x l have the same signs. So what is this checking? This is checking whether f of x l is bigger than 0. So does x l have the positive sign and this checks whether f of x m also have the positive sign. So this is checking one side of the condition that we want that f of x l and f of x m both have positive signs. So if they both have positive signs or if they both have negative signs which is the second condition over here, then that means they have the same sign x l and f of x l and f of x m have the same sign in which case we are going to set x l equal to x m. Otherwise we are going to set x r equal to x m. This is exactly what we said on the previous slide. That is it. So we are going to repeat this. We are going to repeat while this condition holds. We are going to stop as soon as this condition stops holding or in other words when x r minus x l becomes smaller than epsilon or in other words our interval has become smaller than epsilon. So at that point we are going to print out x l as the root. So this is what we said on the last slide and this is what we have here. So this should work and we will, so before moving on I just want to ask you a simple question. So suppose instead of finding the square root of 2, square root of 2 I want the square root of some other number. So say square root of 3. How would you choose x l and x r? So we can follow, we can see what is happening over here and maybe we will try to do something that is over here. So here we had 2, so let us try if we put, what happens if we put 3 over here. So indeed you will see that it works because again this will be negative and this will be 9 minus 3, so this will be positive. So you can use the same idea to solve this for any number larger than 1, for any number, and we are, so let me leave it at that. So let us do a quick demo of this. So this is the program that we had on the slides. There is a slight difference, so we are printing our final answer that we calculated over here. But in addition in each iteration we are also printing out the current interval, so the current values of x l and x r and we are doing this at the end of the iteration. So at the end of the first iteration we will calculate what x l and x r are and so on for each iteration. So we are calculating the intervals, we are calculating the final answer and then we know that there already exists a library function sqrt which gives us the square root. So we will print that also, so that will tell us how good our answer is. So let me compile this. So let us see what it has printed. So at the end of the first iteration the value, the interval was 1 to 2. Does that make sense? Well we started off with 0 to 2, x l was 0 and x r was 2 but then we moved x l to the midpoint, so it was 1 to 2 and so the interval keeps on shrinking and eventually it has got down to this. So it did take some amount of work, number of iterations but it eventually did get to this and as you can see this is indeed identical to what the library function tells us. Of course there may be additional digits after this which are not being printed over here and maybe the final answer is different, so if you want to check that you can print the additional digits also. But anyway to 5 digits this is certainly the same. So let us get back to the slides. So a few remarks. So because we are picking the midpoint in each iteration this interval x l to x r is halving. So in each iteration the interval halves. So the uncertainty in our estimate goes down by a factor of 2 or in other words we get one more bit of our final answer. And the size of the interval gives the error in the root we said and so the error halves and so if you want the answer correct to k bits you should use k iterations. Now the code that I have given over here is doing a few additional calculations over and above what is strictly needed. So I do not want to get into that detail right now in this lecture but you can take a look at the book and see how some of the work that you are doing could be reduced. And let me leave you with an exercise. I would like you to modify the program so that it calculates the cube root of any number w. So the number w should be read in from the keyboard and your program should correctly initialize x l and x r now. So think about how you would have to do that and then it should print out the cube root this time not the square root. So what did we discuss? So we discussed the bisection method which is a very simple method for finding the roots of a function. So it requires that we should be able to evaluate f for any x and that f should be continuous. And we need to have x l and x r such that f of x l and f of x r do not have the same sign. So if these three requirements are met then we can use the bisection method. And I should point out that the method is simple but it is slow in the sense that there are faster methods available. And next we will see the Newton-Raphson method which is one such method. But before that we will take a break.