 Hey guys, welcome back skipso an episode 17 topic today is root finding aka solving equations You know p-brains like me we can subtract two from both sides solve an equation, but how does a computer do that? That's today's video should be a short one. So stick around the First question should be what is a route? Well route is an elitist gatekeeping word for where a function is zero or does this orange line cross the x-axis and ultimately the question is not so much does it cross? But at what value of x does it cross at 7 at 12? You know what value of x gives us the orange line equal and zero The next question that you should ask Would be who cares where something is zero and really the answer is nobody cares But the thing is you can actually trick society and the universe and yourself Into things actually being useful by representing useful things as this orange curve if this is a curve for which Crossing zero is useful Then finding that zero would also be useful. I'll give you an example So let's say you're you know Hamas and you want to shoot a rocket And there's you have a trajectory plan for that rocket and you also know that there's an Israeli flight You know path in some certain direction a passenger flight Well, you know you could subtract those two curves and you can find you know the impact point where that difference is zero So yeah, that would work of course you have to worry about time of course there's three variables for the spatial dimensions, but You know the principle is the same where these curves intersect that difference between them is zero So yeah, there are uses for this so how would you find the route now if you asked me how to find the route what I would Probably do is I'd say well, I'm just gonna start at some number guessing I'm gonna start guessing numbers and I'm gonna keep increasing my guess So I'll set say I'm guessing like negative five I'll guess negative four negative three negative two negative one zero one two three Once I once my guess changes sign like once the f value changes sign Also, okay, hold on there's a route there because there's no way a function got from being below zero to being above zero without crossing zero And then I would say well, how am I gonna find where this year? Well, I take those points and I would bisect between them until I I found something and that's called the Bisexual method. It's very simple to implement and I'm so simple. I'm not gonna give you any any like algorithmic Expressions here. It's just so simple you can do it, you know yourself The way it works is you first guess some bound so You pick bounds in in between which the function crosses zero if it doesn't cross you made a mistake Next step so here you can see the this red line crosses the x-axis at this point here And so I've you know, I've picked two bounds on the outsides of that So the root is contained within them then we bisect the bound So I cut halfway between those two blue lines and I make a new pink line And then what I do is I say well If I evaluate this red curve at the lower bound I'm negative if I evaluate it at the upper bound I'm positive and I evaluate it at my bisection again, I'm negative and So I do I replace the old negative with the new negative and I keep repeating that process until you can see here until I get bored which works and This will slowly converge, you know 50% Reduction in your guess in your in your bounds every iteration until you get to your to be close enough to your route that you'd stop Next question would be well are there other ways and the answer is of course other ways There's plenty of other ways, but honestly, they're all worse in my opinion So Newton's method is a is a very common thing that you'll hear. It's also relevant to other fields like optimization But the process is simple basically what you do is you Make an initial guess somewhere random ideally it would be close to the to the root And then what you do is you use a linear approximation Essentially to improve your guess so basically you evaluate the function value and the slope at your your guess and Assuming it was a line you compute Well, if I have this point on a line and this line slope. Well, where would it cross zero? And it would cross zero at If it was a line you that guess And so basically you can see here if we follow this the slope Pink line from the orange point down to the x-axis that would be where we would anticipate our route to be Unfortunately, this is not a red line. It's a red curve. And so it's not going to exactly hit zero there But that's our next guess. So we would again reevaluate our Slope and function value at this orange guess and we would again improve our Next guess in a linear way So we have pretty straightforward process. Here's the expression The problem is is that it requires for so your future guess Requires your current guess the function evaluate your current guess, but also the slope And the issue is is that you don't have slopes in real life In general, you're not gonna even have analytical functions in real life. And so this is going to be a matter of you Running some code and and pulling out values Analytical expressions don't exist at least they aren't very useful in real life. And so for that We have the secant method and what it does is basically it constructs an estimate of the slope based off previous guesses So here's the expression you can take a look and see how that works It's pretty straightforward. You're basically estimating the slope with previous guesses and using that to formulate your next guess The last question that you should ask is well, when do we stop this iteration process? And of course I kept saying oh stop when you get bored. That's fine You know if you have dinner at six o'clock or you have to go sleep at nine o'clock stop. That's fine No one's gonna rush you But there are other ways to stop as well one would be some number of iterations that say well, I'm I Only have time for a hundred iterations no more And I'm not gonna have a good answer. That's like they do at least a hundred So hundred hundred rations is what I'm gonna do. That's fine. You can do that You can also say well When my function gets close to zero, I'll stop when this when this red line gets close to zero when the distance from The x-axis to my guess is below some tolerance. I'll stop that works as well That's a good way to do it and A final way to do it would be well when my guess stops changing very much Then I'll stop when my first when my 27th guess is 6.5 and my next guess is six point five zero zero zero zero one I'm close enough. So that's how you could also stop and there are many more ways to Condition to stop but these are I guess some Simple ones So now let's check out the code three examples one for each method that I just discussed all aptly named the royal example 17 in the so you have suppository. Let's take a look So again, it's all in the it's in the code. Take a look. Let's take a look at the functions themselves. I Have it here. I believe so They all have the same overall format and I'm not going to go into the math necessarily that they're implementing It's all straightforward and you can tell about the code yourself, but I'll show you how the the functions In the IO goes how are we pulling in inputs to our function and giving out outputs? Here's how the bisection method works. So the bisection method takes This function takes us like four inputs. The first one is in RDI That's a function point that points to the function of interest. So that that's the f of x So that function pointer will be the address in memory of a function of this form down here And so it would be basically a function that takes a floating point number and returns a floating point number in XMM zero So you'd have to make that separately. That's your job. If you want to optimize something you want to solve a route somewhere You have to write the function yourself. It's not pre-made for you. That's your job to make the function and This function should only affect registers X of M zero no other should be affected Besides that there are three other inputs and they're all floating points So the first two are going to be the bounds the lower and upper bound and the last one would be your tolerance So here we're using a tolerance to Stop our iteration process But besides that that's all the inputs it does return two outputs in Rx it will return the number of iterations that were required to Converge and the last input would last output would be the actual route itself in XMM zero And here you can take a look yourself and see how it's how it works It checks for valid bounds it checks against tolerance. It replaces the bounds with the algorithm I described previously and at the end of the day it Preserve registers and returns your route as expected The new method function is very similar. The only difference would be that now Same outputs, but you are now passing in two Function pointers one would be the function itself and the second would be the slope function the derivative function f prime Whatever you want to call it. That's also passed in and it only takes in two 20-point inputs it takes your first guess and your tolerance and You can take a look and see how it works basically it it just loops in the same way checks your tolerance and Returns your solution. It's a very primitive algorithm So it's very simple to implement last would be the secant method Very much the same thing but remember secant method. You don't have to pass in a slope function So you're only passing in the function pointer to the f function itself as well as in this case You have to pass in two guesses two guesses to construct the slope Right, so you pass in two guesses in XMM zero XMM one as well as a tolerance and XMM two Put a parenthesis there and that's that's it That's that's the functions that we're talking about in today's video So let's now look at the actual code that we're running in these examples So first example would be the bisection method and always examples. I Am solving square roots, so let's take a look at the code here In this case you can see we're including a few things we're including some functions to print as well as the bisection method function that I just described and Here you can see we're trying to solve the square root of two And so the way this works is if you want to solve for the square root of two What what numbers satisfies square root of two? Well Whatever that number that is if you square it and subtract two you'll get zero, right? So that's what we're doing. We're this function takes a value Squares it subtracts to and returns so when this function square root of two hits zero that meant that the input was the square root of two And so here you can see we're passing in the function pointer in our DI Passing in lower and upper bounds and a tolerance and calling that function and then printing the results What are our bounds? Our bounds are one and three obviously square root of two falls between those bounds and our tolerance here is point 1 0 0 0 0 1 so if I Leave this and I run You can see that function worked We estimate the square root of two to be one point four one four if I open up octave and I Evaluate the square root of two you'll see that it comes out to be the same number two within that tolerance Okay, very cool Next example Be so the secant method. Let's take a look in the code here in this case We're including a different function. We're including the secant method function but the rest of the code is the same except now we're trying to solve the square root of three so what this Function here does is it multiplies number by itself? Subtracts three and the idea is when you pass in the square root of three this function will return zero and So again, you can see we're passing in function pointer to square root of three passing in our guesses This should say guesses so I'll say guests one guests to and Tolerance and I'll change those down here really quick said change the name so Guess two Is that how it was guess one and obviously square root of three falls between one and four? Oh Crap a lot of copy and paste going on here guys. Sorry I'm not in a rush. I swear If I run this you'll see oh crap. Oh, it's underscore. Come on scuffed video. I Knew I did it for Square root of three one point seven three it looks familiar to me. How about you? Okay last example example C This was Newton's method now this one is interesting because it requires us to have an Implementation for the slope so in this case we're passing we have a square root of five Function as before you pass in a value that hopefully is a square root of five When we dump when we square it and subtract five we'll get zero but now I have function called slope square root of five which is the derivative of X squared minus five which is just 2x so all this function does is it multiplies the input by two and returns With this basically now we're able to Pass in the actual function pointer itself for the function as well as for the derivative of that function a guess and a tolerance and call the newton's method assembly function and Yeah, our first guess you could see here is three and tolerances point zero zero zero zero one if we run this We get two point two three six. I open up octave and calculate the square roots of five That's the answer that we get Professionally speaking so yes, our root finding methods do seem to work We're able to compute these extremely important things like scurridive two three and five great great work But that out of the way. I want to thank you for watching. That's the entirety of today's video wasn't that long at all If you're interested in hanging out we have a discord server check out the description see you around