 All right, this is gonna be a very off-the-cuff video just showing you the process of how I learned How I learned programming so? We're gonna be I'm gonna be learning Lua today by making a polynomial root solver Now I don't have much experience with Lua Only watched the 15 minute minute video about how to use it so Let's start with the What you have to Do the language is a hello world now here's how I'm gonna do the Root solver so and over here So when you have a polynomial, let's make a little graph here And you want to find a root basically You're gonna want to take the You're gonna want to take a random guess So let's say we go over here and then And then do a tangent line. I know the tangent line was over here I know it's do a tangent line right here. This is Newtonian iteration now. We see where it hits zero We go up here and We're hit zero we choose that point in the graph and We draw another tangent line We repeat the process Until until we reach close enough to zero now, obviously there are Some caveats like what if what if our tangent line is here Now you have a perfectly flat tangent line And we won't be we won't be going anywhere Or what if we're stuck in a loop like this Let's say let's say we kept let's say we kept going between here and here But let's not worry about that for now. Let's just focus on solving roots Now first things first you got to figure out how to do I O So Good a stack overflow All right, so let's Prompt the user for let's ask them how I forgot this isn't see and You take Degrees go It's print just to make sure we have it as a number as one plus degrees All right, so it is a number to miss a little know what that means Um Let's see Now we want to read a seven multiply it read seven characters from standard input Read two numbers and Assign them to a So we're going to do a for loop in order to get all the different other grays All right. All right, let's do what it says Santa says Lou has weird syntax All right, that works. I wonder if it doesn't like the Java because this doesn't work when it gets the second Degree Wonder why it doesn't this is space a little bit. Okay, it doesn't matter. Let's do a sizeable Yeah, that works because you got the zero all right No, there's a space we need a table. I think they're called tables Stop bothering me should replace this degrees Actually call this degrees Evil sir, I know it starts at one. Oh wait, it doesn't matter Have a another four. Let's see. It should be G's 56 I'm gonna do this just for clarity to All right now for the fun stuff that might be a little little harder to get into so Now what we want to do is we wanted to Oh local fishing Because we want to take the derivative Spacing is still bothering me though. All right, so how So we're gonna be using the power rule for this Yes, let's just do it in the same as print degrees. So Should start at one artist. So the first degree is Hmm Doing if we're doing this right dang it Yes, so it does start with the highest degree Just making sure Fishes Was table dot ad right? Yeah, okay. It's not table coefficients of derivative I I don't know why they made Lua erase start with One I'm sorry if I'm not talking that much. That's just That's just the way it is. All right, so the coefficients is gonna be I Whoops, ah, what was it? I was right the first time. Let's do three four five eight Hmm. He's a intense life. Oh wait. No, yeah. So it is working So this should be all right. So the first one is it so three is a quadratic and then Okay Hmm Because it's been bothering. Why is there a space? Actually, but I just With print in print Let's just say multiple Instead It's primarily for debugging in a simple tool if you want a more rigid control. Well, that's Silly, you know custom function. This is way off topic, but string dot format So, yeah, right amount of spaces. Why does it do though? Why does it give you tabs? I Get if it was single spaces, but tabs Kind of weird Function, it doesn't need only automatically so we can All right still though, we don't need to do that. Okay now that we have basically The polynomial and the And the derivative we're gonna need to find what I inverse the table maybe Make it make it right Because it actually maps out pretty well. All right. No, yeah, let's flip this Zero times four. I mean two times what? Degree three. Was that a bad idea because I want the You should have kept it for This one instead because I want the degrees to match to match the place in their table. So if I have So first Why is it saying unused it's totally used sir the first one is going to be the first degree Table Three is nine Two is eight Why does they all say degree four? my way six and Two degree one is two what? All right, so the first one makes sense degree three is two shouldn't it Make sure So you don't remember that. Let's do Since you don't have a lot of code earlier here. All right. The first one should be fifth No, yeah The first one should be that's four. It'd be four times five times Just doing it backwards They're doing it right though four times six is two five eight Let's say from small to largest Very good reasons for marches to small is a little bit 20 bad arguments number three to four man. Yeah, this should be I'll laugh if I'm Not yet, so Valid conversion dash L to four. Why doesn't have to do tabs though to is nil Why is it now? First of all Since it's a quadratic that has to be a linear one and I've been spending too much time on this Man, I really wish allure a started with zero, you know make things easier But why is it nil though? That's weird this one I think all right, let's go The heck did I not save? Attempt a little 20 lucky thing, but it works. Let's try it again Four I should be 18 and negative. Yeah. Oh, yeah So I guess it starts it starts with the largest degree and ends with the with the smallest Yeah, I don't think I have this bottom part anymore Um Okay, now so so now it's time for the fun part kind of So if we want to find the coefficient Let's get a desmos to have f of that and let's Just write it like minus five. It's just making a cubic Plus one and with all real roots Let's keep it Like that. All right. I think it makes it easier to see now. We have negative one five and nine our roots If we have f prime Prime of x we're not really gonna I'm not really gonna Care about this. So if I put a So if I want a line over here Say I want a tangent line at five More to let's do a hold on. I may need to fix my themes all right fixed so You have a f of a that is going to be the Slope at that point. So put the extra parentheses. All right. So what we want is for this to be kissing this point or kissing the That line. So You find where it is Uh, so if you remember mx plus b And in this case m is f prime of a so let's That would be for f of a So if we want to get b, it'll be f prime minus f of a I think Was a dealer around minus f prime Plus all right Let's do this again f prime of b plus b equals That would be now if we multiply this by So we got this. This is a so f of f prime of a times a Times f of a So I got an answer for my calculus homework or my calculus test for All right, so now all we need to do is find when this equals zero. So it'll be So it'll be f prime of a minus f of a prime of a times a all prime Of a that's when it equals zero So we want to take this thing And make it our new our new guess Let's copy this number Put it over here And as we can see or getting close enough This is the last iteration we need to do All right, so we need to know what close enough to zero is Now obviously we are going to need to do this Is A far so let's say we have an actual Let's say now we don't know what the What the things are Now this one this function has complex roots, so We might I don't know if we I don't know if we have we have time to work on that Aren't finding complex roots But as you can see We're already pretty Pretty close. I guess you could say So this is going to be our new a And we're going to we're going to keep doing this until we find the a zero So we're just going to we're just going to be looking for one zero Uh So let's do Is there a way to do it cons? All right, so let's call this This To let's do it. Let's make this function function Function I have to do my functions in pasco case Not pasco case I don't think that's not what it's called. Uh, so we have the original Maybe you could do functions, but original polynomial and then the derivative That's not how low it does it. I know you have to do end Man Curly I like my curly braces All right, so remember the tangent line What's this? So how do we get We pick a random number. Let's start with Let's call this input So let's do local Let's do d for the derivative local d slope. Oh, what should the other thing? What is the other thing? Let's do dm I don't need to say any colors. The language doesn't All right, so our first thing our first thing is this so dm That equals our If we put our input into our Into our Try to think so. Let's do Take this unused Let's make this global global and Based on the degrees All right, so we start at one one is the highest Number of degrees minus one all right So it'd be number of degrees minus i I think yeah okay, so see underscore equals input times time Number of degrees minus one would it be number of degrees minus one? I don't I don't know it'd be it'd be yeah. Yeah. Yeah. Yeah, which is a higher presence So this work the way you need to use power value I shouldn't get ahead of myself, but i'm gonna do it anyways All right, so it'd be dm underscore times input minus I put too much of this function. I should have Done it kind of different In fact, i'm gonna fix this later. All right return To legal db Why doesn't it need two dashes? That's weird. Uh, what else? What else? All right, so I just want to make sure I got these right I'm gonna have to refactor this A little later So let's print local Wait, no What's efficient such derivatives? Was it degrees? Insert two degrees I need to fix my variables All right, we don't have print statement here Uh, okay Attention line one It's four negative one two three Minus five attempt to perform arithmetic on nil valley local db in 38 All right, so first of all I did this wrong function All right, this is how you're supposed to write stuff How long is this video so far? Oh, yeah, good thing. I'm still recording. Uh All right, so the way it should work is we take a Get and get and get what is good and Frequently low as soon as it never ends just before it's nil elements I don't have a nil inside an array for an example Set and all right, so get and just takes the The table defines two functions to manipulate array sizes get and she turns the size of an array and set and Which sets the side of the array. All right, so we just need to do table dot get All right Okay, so what are all these? You need to do a dot I put it in here All right, look at some of the largest is the polynomial minus one. All right Mythics operators Nice that powers replicated. All right, so this is okay It's return value So this is a boring video, isn't it? Uh, okay I don't think my problem is like s Index i and multiply by Input the power of deprecated defined in kindness to it symbol miscorresponding and I don't think that's I disagree. Oh All right to get the size of a There is no one way, but it might be disappointing in addition No, wait, I should just use a for each loop for each. Is this really really just a hat the hat symbol? Uh, let's see in the Although the original one degrees We'll attempt to perform arithmetic on nil value Is it because I didn't sign anything? Then why is there a zero at the end? um five plus nine plus six All right, that's 20 20 plus 10. All right, that's working fine. What's your name are? things Okay, so we know that uh, we can't evaluate all right. Now if we get now we can try and get the tangent line So Function All right, I forgot to make this a get repo. All right, so oh, jeer Depart things consistent Input I forgot that lord doesn't use curly braces Should I like sweet it is Maybe have a chat so I can actually have a reason to talk. Um Is it again? Let me go back to desmos Let's uh, okay This time let's actually name it local What is the m called height I guess Or I meant the b. Oh, yes, just the y intercept. All right, so Decept Start at zero Wait, no Validate polynomial Polynomial slope Damn This is how I figure out that when I got that Question on the calculus test. Well, hopefully the curve is good enough. So quite silly Okay, remember that for later. All right, so here's how we we're getting the tangent line turning slope Y intercept Oh, it's another function. Maybe we should change all of these to that's fine line that space is only Oh name There's zero from Return how do you get a zero from a line? All right, you get mx plus b Equal zero. So that's negative negative b over m so f line to divided by Really? At b equals zero No, yeah. All right, and now We test it we should get from this Negative 2.25. All right print Get zero from line Get tangent line This is so disgusting Oh What is it called? Polynomial derivative And then we put negative one negative one is there input All right, I forgot Here This one is four Uh, the first degree degree. That's negative one two Three negative five And yep. All right, this works this works. We're getting somewhere. All right. So now we just need to know how far away this this little this little guy this a Is to zero which is pretty easy. It's just negative a Or it's literally just a The absolute value of a so we don't really Or is it I know it's not a I think Is it a No, it's f of a. All right So we got some There is no constant lower, but you can use a little To protect. All right Whatever local Wait, do I already have one called closeness to zero? Minimum closeness to zero and now We do off or loop All right, but how do we stop these cases when it's the top? Now they're pretty rare, but they're gonna happen most Not most likely, but They might So if we do So how about instead of closeness to zero We have a variable. That's uh Uh Now we have a triple and now we're gonna do a a four loops of four Those one Minus one Dude, because that's the amount of let's see we can do a maximum of 20 iterations. All right, it's just another zero max iterations Do this Roots choose One How do I know is it is there a way to know like when These functions get really big if they're too when they get too big For now, I'm just gonna say three for now the input is just gonna be three Hmm Let's make another function function does Whole thing And first it takes the polynomials No, I don't really need to do that input equal Get zero from the line and the line is going to be Tangent line polynomial The derivative and input closeness to zero Will be evaluate polynomial. What was it? Oh absolute function It's just If like this We should do it here abs Reliate polynomial And we take the original polynomial and we put the Input We got right our input again is a Maybe we should change it to i. I don't know. Why am I putting this here? Actually, maybe it's the random Function rule I need to run math or random c before watch easy All right, so let's put this at the start Does it change it every time you call math that random? Yeah, just to be safe All right, but how do I because I want one that uh Yeah, it's different negative 10 to 10 This guy tells us to do os clock. So I guess I'll do that I feel like floats might not be that big Right inches. No, they are all right. It doesn't matter Mm-hmm Trent, let's see if it works Whoops has no Louisville on bad argument to random seed number has no integer representation. What is up with computers and randomness Best way to generate I love these best way to generate whatever is Uh as the pig said using no this time. All right, I'll use time. Let's go back to my desmos How big is my polynomial? four negative one two three negative five all right 2.337 And uh, all right, so it's got the same number twice See, this is what I was afraid of. Um, so far so good though. Huh? Hey, all right, let's see Let's see what we could do Uh, what I do want to know if this is if that there is There's a way to figure out how Like the boundary of where the for the zeros are going to be Of the polynomial So look if we run this again Oh, we might actually We're lucky See, all right, so this time we actually got got this one here. Um Okay, let's see I'm happy with this For now Uh Keep in mind Ritz of a polynomial in an integral Let R be a ring. Oh my god I already don't know this This guy's tired. This is the fact that community of ring identity is See if a polynomial if and only if some polynomial man man man Should he watch this or will he get copyright stricken? Oh, these are you have these are youtube All right, again, 1010 is not gonna be a uh an exactly big thing So I think this is this is we're gonna keep it at that at this for now Um Maybe I'll update it for now. I'll put it on I'll put it on I'll put it on get hub or something That's it