This college calculus course is different than other college calculus courses. Like many other ones, you will be learning all the concepts from an experienced university professor. But in this course, the instructor Ed Powski will also show you how to implement all the concepts in Python. This course is for everybody, but especially those interested in computer science. Welcome to Calculus with Python. In this course, you're going to learn how to write Python code to do your calculus. And that's going to save you, you know, pages and pages in your notebook, a lot of time, and we're also going to look at the calculus so you understand the math. And then when it comes to doing the calculations, finding that answer, instead of spending all that time writing out all the factoring, it might be like one line of code in some cases. and we'll look at how to set up your Google Collab notebook so that you can use this in the future and set up, you know, oh, I have this sort of situation. I know what to do. Organized table of contents in my collab notebook. Go right there. Boom, boom, boom, solve. And so really then you can concentrate on a lot of the applications and some of them it might involve setting up a more complicated formula. You can spend your time with that so that when it comes time to doing the solving, you can say, "Okay, I'm going to type this in here and run it and get my answer." So, it'll be really exciting. We'll look at a lot of the applications and a lot of good things you can do with calculus and with Python. This will be like the springboard for even more complicated math that you could do in the future. So, enjoy. And here's calculus with Python. To start talking about calculus, we first need to talk about limits. And this is going to come up in terms of how we get from our algebraic way of thinking to calculus way of thinking. So let's start with a simple limit. We'll just briefly talk about this rem. So if I have a hole in the graph, what's going to make a hole in the graph? Well, remember limits come up. If I can't have a certain x value, then can I get close to it? You know, then you know what's my y value if near that x val? So, if I have, you know, if I look at this, x can't be two because that would make the denominator zero. So, I get y-v values, y values, but around right where x is 2, because that can't happen, there's going to be a problem. Now, this is what makes a hole in the graph because you might notice I have x - 2 in the numerator and I can cancel that out. So, then I really am graphing what looks like y = 3x. Now, given that, now I have no problem. my, you know, x can be two because I I cancel that out. I have no denominator. But the limit happens when that was there in the original and then it canceled out. So it can't be exactly that value, but it can be really close. So what we have is if I graph that y= 3x, you know, there's there you go. That's what my graph would look like. But only exactly at that value or x= 2. Most graphing utilities, you're not even going to be able to see this. But if I was drawing this out, I'm going to really exaggerate it. You know, when x is 2, now this works. 2 * 3, y is 6. So at that point 26, I'm going to really exaggerate it. And there's a hole in the graph because this is the limit. you know what would the y value would be six but you know approaching from either side but it just can't exactly at that value exist so I have a hole in the graph and these are the some of the themes you know we're going to look into the code for this just um a little bit to kind of prove this theory a little bit of how to get to this even if I didn't notice what canceled you know things like that and then we're going to build upon this and we're going to see how limits and especially this notion of being able to cancel something out like that doesn't exist that x can't be two but I can cancel that out and that's going to be a key feature in the development of calculus. So let's just take a look at a first thing in our code lab notebook and how to demonstrate this with code and and we'll build upon that. So let's take a look at the code. So here we have our equation and we see that x - 2 would cancel out. It'd be y= 3. But because it's there in the original, x still can't be exactly two because that would make it undefined. So what we're going to look at here is, you know, we we can do that. We can picture that. But just as a method for whatever other equation comes up, um, you know, I'd have this set up, you know, any given x value and then h is what I'm going to use for that value of something, you know, a little bit more, a little bit less. So h is that little difference here. Um, and so approaching from the right, I made my variable y, right? Um, I'm just plugging in x plus h here. 3 * x plus h minus 2 for the numerator x plus h minus 2 in the denominator and from the left. So then it's going to be a little bit less than that value x minus h. And that's what we have here. We want to set this up that you know if I want to test something out without graphing it um approaching from the right or the left I can just set up you know any given x value and h value and then I'm going to print out these two values here. both of them would be three because that that's how it would work out. And I even put this if statement in here supposing it didn't work out, you know, rounding it. If it did if they were not equal, then I'm going to say the limit does not exist. And that way I can try this with other equations. I can put them in here. I can, you know, try this at different x values, different h values. And, you know, we that's really what we want to see that from the right and the left, they're exactly the same. So the limit would be three and you know it's only exactly at that one value when x equals exa uh exactly two that that would not work and we'll see how we get some other applications where sometimes this doesn't match up and it's more than just a hole in the graph. Here's a limit problem where we look at the denominator can't be zero. So x can't be two because that would make that zero or it can't be neg -2. And so we'll put these if I was drawing out the graph, I'm going to put these dotted lines here to show it can't be two or therefore no y values associated with that. And these are asmmptotes. So vertical asmmptotes for that that x value cannot exist and so therefore no y-value anywhere near there. So if that can't happen if x can't be you know let's just concentrate on the two if x can't be two but it can get really close. So x could be let's say like 2.001 001 really close approaching from the right or it could be something like you know x= 1.999 again really close but approaching from the left. So, one of the ways to calculate limits is to plug in, you know, really close approximations and see if they match up or, you know, what is the trend. And yes, we that would still be the go-to method even if I wasn't doing this with code. But since we have the code, we're going to show how to plug in these values and even like really really close approximations and see what we end up with. And then we'll see you know the behavior of the graph as it approaches that uh that value from the left and the right. So let's take a look at that in the code. Let's set up a system where we can look at this and find out where this is undefined. I might see this and say okay I you know really this is going to come up when I have a denominator um because we don't want that to be zero. So I can just take an example like this and I'm just going to look at the denominator and set that denominator equal to zero. x^2us 4. So I'm going to put that in here as this equation and we'll we'll work through this examples. So finding out if that's zero. Now we get into I'm going to import simples and solvers and then I'm going to define my symbols. All right, I'm just using x. So I only need to define that. And there's my equation. I'll just call it eq. And I'm just looking at the denominator. So I want to solve it equal to zero. Actually, that's really like the go-to for senpai. Everything, you know, we want to set this equal to zero. So, here what I'm going to do is notice what this will do. And there we go. So, we solve this. You know, there's my equation. And again, simps that whatever I put in here is set equal to zero. So when I solve that, that's what it's doing. It's setting that equation equal to zero and then solving for x. And so then I have my two x values. And I like to just leave it like this. There's there's other ways you can do this. And you can anticipate this and like store these as as a list or an array. Um, I could output, you know, just one of the numbers, not the other. For right now, this is actually going to serve our purposes much better. I want to see what equations I get. Do I get, you know, I mean, I would expect that I would get one, but, you know, maybe you do this for one and you try it and you, you know, you get, you know, oh, I thought this there was a value and you get no answer. Um, that could happen. Or I could get, you know, two or three. You know, we're setting this up that I can find all the places where it would be undefined. And in this case, I get two x= -2 and two. So, I'm going to keep that and then run the next code block. Now, I want to approach from the right and the left keeping the same one. So, let's start with this. I'm going to say I'm going to say let's just say that this one here two as an example. And we're going to change our h value. All right, that's probably close enough. So, let's just leave it at that. 01 01. Um, I just put this comment in here just to remind us that that's the one we're doing here. And approaching from the right, you see x plus h and approaching from the left, x minus h. Really, the the key thing here is just to make sure you get um all the Python notation and your parentheses correct so that you're actually graphing what you're expecting that you're graphing or checking what you're checking. We're this is all doing this all this without graphing. So then I'm going to print out well what is the limit as it approaches from the right or the left and I threw this in here. If if it's not equal then say that the limit doesn't exist. So we're going to pick that one and we're going to do this and we see what happens as it approaches from the right. the x value gets really big and positive or the y-val gets really big and positive from the left it's really big and negative totally different so we're going to say that the limit does not exist and you see what I can do then is I can test the other one the other one was -2 all the exact same thing I just change this to -2 and run it and you see very similar results but it goes in the opposite direction so these are the things we're trying to put to piece together. Yes, I could certainly just like plug this in and graph it, but this is part of calculus and some other applications. Maybe I need to know does the limit exist uh at this point or I need to know what about the trends and maybe doing this without graphing. Maybe that's something important that I want to know. So that's the idea, you know, does it exist um from from the left or the right? And you know just as an example simple example to compare you know nothing canceled I I could look at this right away y= 3x and see that you know there's nothing going to make it undefined. I expect that to be nice and continuous. So doing the same thing. Oh, okay. I'm going to I can pick any x value and just approaching from the right or the left and then let's print it. And we see, you know, taking all these decimal places, it's this. But really, if I round it, I find that the limit is x= 2. There we go. So, that's really what we want to do. Be able to test any point. Where is it undefined? And then approaching from the right or the left, does the limit exist there? And next we'll take a look at approaching infinity and negative infinity. Now we have the limits right side and left side for each of these. So we'll put in the arrows. If we if we were doing this without code, then we kind of piece all this together. And then next I'm going to show you how to do all these boom boom boom uh with the code. So we determine that it's undefined too, but approaching from the right it goes up to ne to positive infinity. So we just kind of put the arrow there. We'll fill in the rest in a minute. Then approaching this from the left, it go it goes toward negative infinity. We put an arrow there. And testing this we find similar patterns but approaching positive infinity from the left and then approaching negative infinity and since this won't cross these we expect that these you know turn this turns around somewhere. So what I would do is I would pick a point in the middle and I would plug that in. And as it turns out, if I pick zero as the x value, the numerator becomes zero and the denominator is definitely a number. So it's 0 0. And my arrows then, you know, you'll see how we'll get this looking a little more elegant with code. But there we go. you become a little bit of a an artist, you know, sketching out graphs. So then here, what happens as x approaches positive infinity or negative infinity? You know, does this just keep going? Does it turn around? Now, we we'll again look at how to do this with the code, but um here's some insight. If you if you you know you're not writing code you know as x gets to be a huge number you know squared or whatever exponent that really has an effect much more than multiplying or just subtracting four or something like that. So if you if x gets to be really big you know these exponents make a difference. So, we take a look at if the numerator um has a larger exponent, then it's just going to go toward infinity um you know, the number just keeps getting bigger. If the denominator has a larger exponent, that's going to really make a difference. And at a huge numbers, that's going to be not zero, but so close to zero. You know, the denominator being so much bigger, that's going to the limit will be zero. If they're the same exponent, which they are here, I look at the ratio of the coefficients. So 3x^2 or 1x^2, then I'd expect that the limit as x gets to be really big as x approaches positive actually or negative infinity because they're squared in this case it approaches three. So what I would have is, you know, one, two, three, and I would have a horizontal asmtote at three. And so this would level out around there. And you know, a little bit of an artist, but you don't have to be that good. That gives you the sketch of the graph. This is what it would look like. So we piece together where is it undefined and then you know what's going on. What's the behavior right around there? um find the point here and then as it approaches positive and negative infinity and then we put it all together and this is the graph. So we're going to look at this and again how to write this with code and and be able to apply this to any other function you know find out where it's undefined and then the behavior around each of these um what happens when x approaches infinity and simp has a lot of these things already built in which is great. So we'll do all this and then we'll also just graph it. So you know we kind of find these definite values and then see the graph. So let's take a look at this in the code. Okay. So here's our next piece of the puzzle still before we before we graph. Supposing I want to find out what happens as x approaches infinity and what happens to that y value. And I have just a comment here for that equation. I can just loop through it. And in this case, infinity is a big number. So I'm uh instead of just jumping to infinity, I just want to show you some values. Like I can just loop through a few of these values. Is x is 10, 100, a,000, 10,000. And I'm going to find the limit. You know, plug in that x value and print it out. And we'll see, you know, do we see a trend here? So for that x value the limit 3.125 but then it gets you know three and then depends on how much you round it. Look at this. There we go. Um so we can really see this way. This works fine um as a way to see these trends of what happens as x approaches infinity. Pick a few key values. then we could put we could put it all together and we're actually going to even get better with putting all this together. But let's just take a look at this graphing. Um, and soon we'll look at an even more efficient way to graph with simp because sometimes that'll be useful. But anyway, this is, you know, hopefully looking familiar. import mattplot library um numpy and we're going to use this equation x value there's my y value set up you know my graph um you know add the zoom level x minax all right so you know my array for my x values my y values and add another equation there tinkering with but we're not even going to worry about that. Um so setting up this hopefully again this is looking familiar you know changing axis um I'm plotting these and I also decided to plot this one point here with a red dot and I'm going to have the title of the graph. So, we've looked at the limits, you know, we've looked at a couple critical values, the limits as x approaches positive or negative infinity. And we can see it all together. Here's the graph. And we see these values as x is pos2. Um, that vertical line is not a part of the graph. That's just showing the asmtote where that whole x value cannot exist. So, there's no y-value anywhere there either. And the same thing at x= -2. So we see then this trend as it approaches. We we already looked at this when x approaches positive2 from the right. This is what it looks like when it's approaching positive infinity. From the left, this is what it looks like when it's approaching negative infinity. And um similar but opposite trend for x= -2 approaches positive infinity approaches negative infinity. And that's that's what these graphs look like. That's that trend. It gets close closer to that imaginary line, but it never crosses it or touches it. And in the middle then, all right, if it approaches negative infinity, both these here, then there's some value in the middle. Where does this uh turn around? And so that's why I picked that x value right in the middle. X is zero. found the y-value and plotted that point. So, we start to piece together what this graph looks like. And then we see here horizontally, it actually will be that similar trend as x approaches positive infinity. That line kind of levels off and it should level off at exactly y equals 3. And the same thing happens when it approaches negative infinity. So, we see these different parts. You know, we found the critical values. We found out what happens when it approaches from the each of those from the right or the left. What happens when x approaches positive or negative infinity? And then we put it all together. You know, I have those definite numbers. And then I also see what the overall graph looks like. So let's look at simp even more efficient way. So using that same equation, I'm going to go right to simp. Here's my equation. Notice from I just have the full equation here. Um but I would have already um checked out and notice that that denominator has values that would make it zero. So I would already have done that first step and found those critical values. you know solve that denominator for zero again using simp um do that in a in a separate code block. So now I have these two critical values here. And so my this is the trend. This is the simp expression for the limit limit. And then it takes these three arguments. What's the expression? What variable? What value? And I've already defined symbols that I'm using X. So my first critical point X equals 2. So there we go. that equation that I've defined up there. X is my variable, X1 my critical point. And look at this. Simp has it just built in. It takes that other argument. So I just need those three. But if I have a sense that it might be different approaching from the right or the left, it has that other argument. Direction from the right. And I can do the same thing from the left. Simp.limit. And there we go. direction from the left. And I'm just going to output those values at x equals this. What is right and left? You know, what are my right-handed limits? You can write it out differently if you want. And my second critical point x2. So that's the only, you know, all the rest of that is the same, you know, same equation, same variable. Now, x2 approaching from the right or the left. And there we go. And since I already did this and printed it out, it's okay that I overwrite these variables here. So, you know, that's I'm just going to use that. It just works out. I I could have kept them and make it one, you know, these one and two or something, but I don't need to. So now I have these two critical points and I have the information about what happens as they approach the right or the left. And same thing here simp has something built in when it approaches infinity. Now this is kind of cool the equation the variable uh and so you know as it approaches what value in pit infinity and that is 0 0 or sorry lowercase o because I can write that as you know o that looks like infinity and then that works out uh typing wise so uh positive and negative infinity so simp has that built in what happens as I approach positive infinity and I just had this printed out as it approaches infinity approaches negative infinity and we can run this and we see um so just like it's si0000 for approase O for approaching infinity if it does approach infinity the output will just be lowercase O and that indicates that that's infinity. So at x= 2 approaching from the right infinity approaching from the left negative infinity at x= -2 the difference as it approaches from the right or the left and as x approaches infinity the limit's three negative infinity the limits three. So we could do all of that and then throw that into a graph and there you go it works out. So um we can just test other limits here. Um I'll I'll I'll leave that for some other examples. Uh but we can use this just to show you just any other um what I had this one is um yeah some some trig values. We'll bring we'll come back around to that when we uh when we look at some of the trig functions here. But you can use the same approach to test other limits as well. And you know find the critical values what happens as it approaches each of them and positive infinity negative infinity and then throw in the graph. So we're going to start now using all this and building your calculator. Your calculator is what I called it. Uh so let's take a look at just putting that together and building your calculator that you can use for any equation. So here's one way to organize your C calculator or whatever you'd like to call it. And we went through a lot of things looking at how to do limits and building upon that. But if you just want to make it that how you can use this from here going forward, you could have your heading limits and notice you could put the hashtag there. That'll make it a heading. It'll show up in the table of contents. And just some directions I put in here. What what x value makes the function undefined. Set the denominator equal to zero. So you could put your code block here. Then importing simp solvers symbols. And right here, whatever equation you have from somewhere else, you would put the denominator here and then solve it. And there we go. Solve it. Uh, remember it's it simps to when you do spi.solve. And sometimes it will work without doing spi.solve, but I just like to put that in as a habit. Um, so there we go. When will the denominator equal zero? All right. And given that you're going to you would get maybe one, maybe none. You know, you get some critical values and then you would set this up approaching each critical point from the left or the right and as x approaches positive or negative infinity. So given those answers you would get here, there's your critical values. go back and put the full equation here. Um, if you run it right away, you might not to re need to reimpport these, but I tend to default to um, putting them in there just in case. Um, there you go, the symbols and you put the full equation here. Um, you could for your own organization put the comments in as well. Um, just so that when you open it up the next time you it reminds you exactly what to do. critical values from the previous step. You see, maybe I just have one or I just commented out maybe I have two, maybe I have three. Put in as many as you want. First critical point. And then again, you know, this is the setup for finding the limit. And here we go. Find the limit from the right, from the left, and print that out. I just commented all this out. But supposing you had the second critical point, you would do the same thing for the second critical point and then that same full equation as you approach positive or negative infinity. So you have this then all set up finding those critical values setting up all these outputting these and then let's put it all together on the graph. Graph with simp one line of code from simp.plotting plotting import plot okay two lines because I didn't import it but then there we go I still have that same equation so a lot so re remember the Google collab if you're running this all like right after one another I think it'll time out maybe half hour maybe a little bit less but if you're going running this then running this then running this um that same variable that you would have set up you know two code blocks before would have been just fine. You're that's still there. So you know and that's often what you would do. You would run the one run the other and you know put all this together and so yep si.plot and then it'll plot the equation. So that way you have, you know, you can put your full equation, um, get the denominator, critical values, put the full equation here, get all this information that you need, graph it, and then now you're building this with your, you know, you're building your calculator here that you can find the limits and see the graph for anything. So let's take a look at one of the big applications of limits and how this leads into calculus. Let's take a look at how limits apply to slope. And this is really the origin of calculus. So if you know I have if I have a linear equation, it's the same slope everywhere. But if I have something like x2, there's a curve to it. So the slope keeps changing. But what if I could get a good approximation of the slope at one point or really close to that? So let's start with just two points and all I did was change the x value from one to two. So this point 1 one and then if x is 2 squar is four. So you know the x value only changed by one and I get these two points. You know, I can calculate the slope there. You know, change in y, 4 - 1, change in x, and all right, that's a linear approximation. It's okay, but we can get better. What can I do to make this better? Well, let's say it's right around one one, right around this point. That's where I want the slope. So, I'll keep this. And supposing I move this point closer. So my x value then instead of adding one I'm going to increase it just a little bit by a half. So you know that x value would be 1.5 and then squared that y value would be 2.25. And let's calculate the slope here. So if I have this then my slope is going to be 2.25. 25 - 1 over 1.5 minus one. And then that's going to be, you know, 1.25 over 0.5. And yes, I'm mixing fractions and decimals here in my in between steps. But I'll put the final answer as a decimal. Then uh that's going to be 1.5 or sorry 2.5. Don't mind me. So all right the slope here it's okay approximation but then they got closer and my slope is 2.5 and as we see this gets steeper so I expect the slope to decrease but what happens if it gets really really close you see so that's the thing this is really what Newton uh invented and we're going to talk about doing this with code but remember he did all this by hand um you know trying all these things and then eventually certain patterns emerge. So we will definitely get to that. Um but this is like what it builds upon. So I want to make sure you know that you get this. So if if I take this well what if my next uh my next point would be I'm just going to put a notation here. Um I'm going to keep that same x value. So my x value is going to be 1 plus and I'll call it h. So that's going to be this variable that I'm going to introduce for whatever little bit I add to that x value. So now h in this case, you know, um h would be one, h would be 0.5, and then h could be any tiny decimal, you know, however many decimal places you want. And remember, you know, you could do this by hand and as much patience as you have to like plug this in and, you know, do the calculations, we can get a y-value. But the general sense is if this is the x value, my equation is x2. So my y value is going to be um 1 + h squared. And I'll close those parenthesis. So that's my point here. You see the x value is 1 plus h and then the y value because I'm I'm just getting really specific with this function it's squared. So it's going to be 1 + h squared. And then we can use this to calculate the slope as then I have change in y. So I have 1 + h squar minus 1 squar. I'll just leave it squared even though we know it's one. And then the change in x is 1 + h minus one. So that's really just going to be h. That is the change. I've defined that as that's my little change in x. And so we can do this for any h value. And what we're going to do is we're going to look at the code and see how I can get these points closer and closer and get a better and better approximation of that slope like right around that one point because I'm still doing slope formula, but my second point ends up being so close. And again, Newton did this all by hand without calculators or anything, but we can do this with code and get them really close and really see um what patterns emerge here and like what's my best approximation of the slope at that point. So, you know, this is the notation that we want to use and let's take a look at how do we how we can apply this with code and see what we get. Let's continue on with this way that Newton did this. And remember, he did this without calculators, without graphing utilities, and certainly without Python. So, we can see, you know, how to plug in these numbers. I'm I'm going to have my main x value, like what's the slope at this point? So, then, you know, calculate the y-value and h is that slight difference. So then my second y value is x + h^2 and we have this um let's graph this. I'll setting up the graph as we did before. For this one the mattplot library numpy look work works well for this. So then now I have this I am going to calculate the slope change in y and then the change in x is h because it's x and then x plus h. So that's that difference. Print out the slope and then I'm also going to show this plot of these two points. So we see our graph here and the x values you know these two points and then the slope there is three. Yeah. Okay. And now we want to get what happens when these two points get closer together. I'm just going to make them I'm going to pick another h value 0.3. That should be small enough uh that they get really close. But then you'll still be able to see them as two points. So now we see these two points got really close together, you know, and look at the slope at this, you know, from these now it's 2.3. So that's the idea. Supposing they get really really close together. Now if I just kept changing that or if I loop through that there, you know the display here, you will see them as one point. And that's the whole idea. But let's look at the the actual values there. So what I did is let's loop through this, but then we'll skip the graph on this. Just looking at the values. So I have my x value and y value. And then as h gets smaller. So here's what I did in range from 1 to 11. 10 to the exponent of a. So that's my h value becomes one10enth 1/100th you know 01 01 01 that's where h gets really small and we're going to go through that same thing change you know x plus h um find the slope and then take a look at what happens here. So already the slope is you know different here but as that h value gets smaller I get 2.01 2109 look at this we get more and more zeros for some reason this one changes a little but more and more zeros and we see that it seems to be approaching two. In fact, for a while here, if I rounded this to like two decimal places, my slope would be two. So interesting. Let's look at this uh you know we we will get to how I you know how it approaches a particular number and how to get that without even doing this loop. But I want us I want you to see that that's what happens. This is really what Newton was saying again without seeing all this printed out in code. He did all this by hand that it actually does seem to get to a certain number. Here's another example. Again, ju, you know, not not too complex, but complex enough to illustrate the point. y= x the3. And I'm going to go through all the same things. Here's my h value is one. So, when I plot these two points, so there we go. And the slope between them is seven. Yeah, that's really a big difference there. So, what happens if I change the x value here? Instead of one, I'll make it 0.3. And we go, yes, they got a lot closer. And then look at the slope. 3.99. And we can imagine them getting closer and closer. You know, how would the slope change then? So then we go through I'll do the same thing. I'll loop and no I'll do the loop and no graph. So x value my yvalue and the same type of thing. H gets really really tiny and what do I get? The slope then a little bit lower but then it keeps even getting lower and lower and then it drops down to three. Um, down here, I actually was thinking about showing this with sliders, but the display doesn't really illustrate the point. You really need to be able to either see these getting closer or see the numbers. So, now we're starting to see in two different situations. Now, it would be different if the x value was, you know, two or something like that. Um, so we, you know, we could do that. Um if the x value uh there we go if if I just make my x value two but the second um the second y value is going to be off the you know off the charts here. So that's uh you know this gets really big really quickly. So we have um you know we see it approaching this. So what we're going to do is we're going to look at the code or we're going to look at the the factoring. Newton did see this and he figured out a way to factor out that difference to get it to be the slope at for all practical purposes one point. So we're going to go back and look at the factoring and how we get to this. And then we're going to come back and look at how to do all that with code and stand on the shoulder of giants and have all this long explanation get boiled down to like one line of code. So we saw how we could get approximations of the slope at a particular point. And what we're going to do is we're still going to call those approximations because we're going to find a way to get the exact value of the slope at any point. And I'm still going to use this specific example y= x^2. Later on we'll talk about how we can generalize this. So if that's my y= x^2 then I have xy um as any point on the graph. But let's write it instead of xy I actually just want to put that actual y value in there. x x2 like there we go. That's that's my y-value. And so then my next point, remember we're still introducing that variable h of like the tiny change in x. So if this is x + h as my next x value, then the y value is going to be x + h squared. So we see that's how we can put in these you know x and then x squ specifically for this for this function that would be my y value whatever x is squared and then if then it's x plus h then it's x plus h squared as my next y value and we see now we have these variables that are all number one based on x and also then I don't have even a specific number so I can find the value of that point. So let's look at the slope. So it's going to be x + h^ squar minus the original x^2 change in y over change in x plus h - x. It really is h because that's been defined as that is that that change in x. So when we look at this we can see the slope and certainly we can say well okay what happens uh you know h can't be zero but it can be really tiny do I still have to go through the what we were doing in plugging in these values no we're going to see uh this math acrobatics that uh Newton invented and we're going to say we're also going to bring in the limits back so this is the slope but let's take a look at this as the limit as h approaches zero. So what can I do with this? You know, as h approaches zero, I can factor this to get to what h would be. Um what what what what the slope would be at any given point. Um so let's take a look at how we can factor this. But I'm going to need some more space here. So, so now we have more space to do our factoring. And remember, this is still the slope at any given point as h approaches zero. So, I can't I want it to be so close, but it can't be zero. Let's factor just the numerator here. So x + h 2 if you remember from algebra that becomes x^2 + 2 xh + h 2 and then I'll bring this down minus the original x^2 all that over h. But let's cancel this out because I have x^2 - x^2. So now we're down to 2xh + h 2. And remember all this yes is over h. So hm still have h in the denominator but I'm adding in the numerator. Let's write this as two separate fractions. So 2 xh over h + h2 over h. So mathematically just a different way to write it just as correct. But this one works for us here because if h is any number however tiny as long as it's not zero these cancel. So I'm down to 2x + h. Look at that. Now when we remember this is the limit as h approaches zero, it can approach zero. And my slope is 2x. And that's what we have here. The slope is 2x at any given point. So if I have y= x^2, all I need to know is the x value and at any given point 2x that is the slope. And what we'll look at then is this and some other functions. If you really wanted to do this all the long way by hand, you actually could factor out different functions and can, you know, as h approaches zero, but cancel out that h value. Um, but then there's all these other patterns that emerge for calculating the slope. But th this is this is really what makes it calculus that Newton figured out that, you know, we can get to that place and cancel out these values. And now I have the slope at all I need is one x value not two points and I can find the slope at that point and that's the actual slope there not even not an approximation. So for as complicated as this was going to take a look at how to do this with one line of code but it's important to see this background of what the you know what we're doing with that code so that you know what the output makes sense to you. But let's take a look at the code. So, here's where we're really going to see the efficiencies of writing code to do your calculus. We're going to import simp and we're going to import symbols and we're going to set that X and Y represent X and Y. I I always use both of them here even though the next several examples really not going to make use of the Y symbol, but that's just my go-to. And so, I have that here. So my expression is going to be in this case x to the 3 power as an example. Here it is the derivative si. Uh differentiation is really the long another way for taking the derivative. So simpi. And then what the the arguments takes that expression and then I'm going to use x as the variable. There we go. That's all your derivatives in one line of code right there. And just I I like printing out that this is the derivative. And then in simp because I'm going to store this. I took the derivative and I stored it as this variable. If I just make this my last line of code and see there's my comment. We'll do the derivative at a point in the next block. But this is my last line of code. So when I run this, it'll output it with nice math print. If I instead did like print derivative, it actually will not print it out in nice math print. So that's why it's worth it to just break these up, you know, this block of code and then do something else in the next block of code. So there we go. Um, we're going to go through some other examples of different derivatives and different sorts of formulas, but you can use this for any of your derivatives. You know what? Just change the expression and this will do the derivative of anything. How great. And remember, the derivative means the slope at that point. That's really what it means. I derive this and I find the slope at that point. So often we want to take the derivative and then plug something in. All right. So that's why I stored it as a variable called derivative here because you know keeps the nice simp output because now I'm going to put an x value in and I want the derivative. This is so I take that derivative uh variable dot subs short for substitution and I'm going to sub for the value x I'm going to plug in the x value and so there we go. So I take the derivative I see that that formula that's the slope at any given point you know plug it all I need to do is plug in the x value to find the slope. So that's what I do here, you know, subs and my answer is a definite number. So that's fine. I store it as answer and I can use that in the print statement because I don't need any fancy math print. I just want the slope of that. So the slope there we go at x= 2 is 12. And there we have it. take the derivative and plug in whatever x value you want and then find the derivative at that point. We're going to build upon this and look at um just different types of derivatives for different formulas because even though this does all this for you, boom, done. Uh you almost might be tempted to think, oh, I don't have to think about it. But you want to have a sense of what you expect that derivative to be or you know uh something. So we'll we'll look at just some general patterns of derivatives. But there we go. Any expression find the derivative find the value at whatever particular point. So we saw one example of a derivative and how we can calculate the derivative with one line of code. So let's look at what the derivative means and then how we can get a general pattern for this. So remember the slope if it was linear this is we could stop here but it's change in y over change in x. So you know change so subtract y2 - y1 over x2 - x1 and then y I could also write it as f ofx. So I can look at the slope as here's my second y value x the original x value plus h some tiny difference minus the original y value. So f ofx plus h minus the original f ofx and then that way I have you know I can define this in very general terms whatever that original x value was plus you know some tiny difference and then that's the change in y and then the change in x is that tiny difference which is h. So for slopes like x squ or anything else where there's a curve and it keeps changing then I want to get these two points closer and closer together. So that difference h it's this is the limit as h approaches zero and remember it can't get to zero but it can get so close and we looked at how we can get closer and closer approximations but then we see these calculus patterns that I can actually figure out and there's certain ones we'll memorize there's certain ones we can factor out and we can see what is the slope at any given point. So remember our other one we did y = x2. So then we said that the slope is you know 2x at any given point and we said okay then you know all I need to know is the x value and then I can figure out the slope at any given point. So you know at zero the slope is zero and we see that and then as x increases remember that x squ it curves upward the slope gets steeper and steeper and that's exactly it given the x value two times that that's the slope at whatever point that is and this is really cool. So let's take a look at you know what are we doing here to get this there's this definite pattern and yes you can write the line of code and see the answer but you want to know what it means. Well, one of the things that we're looking at here is that this is and I'm going to do one other example and then that'll really illustrate, you know, the what what's going on here. So, if I have um 2 x to the3 and then while we're talking about it, the slope is change in y over change in x. And you might remember this from some other notation. It could be deltay over delta x because that's a notation for change. And then in calculus somewhere along the way instead of delta the notation evolved for it to be dy over dx d for delta. So we see that you know this notation evolved. Yes, it is the slope. I can write it the slope like this. delta y change in y over change in x delta y over delta x dy over dx and I'll tell you more advanced calculus classes they it's still dy over dx but they write the d a little bit fancier you know at some point so anyway so what am I doing here and this is one of the patterns that we'll look at and this one's the power rule because I just have some you know coefficient but then I have x to some power some exponent so this pattern is that exponent times the coefficient and then exponent minus one. So 3 * 2 is 6 and then that would be x^2. So if I have something like this, yes, uh Python makes it easy. I can just plug that in. But also there's a few patterns we want to take notice of so that when we do plug this in and we see the answer, they kind of make sense to us like, oh, okay, yeah, that's not what I expect. So if y = 2x the 3r then the slope or dydx would be 6x^2. And once again these functions all I need to know is the x value. I could go to the take the derivative then plug in whatever x value and I can get the slope at that point. And that's kind of what we really want to get to. You know I get this general pattern and then I get the slope at that one point. So then here's the other interesting thing about about doing these calculations and I will just keep it as you know dy over dx and sometimes people just say it dydx as the slope. So if I have a few things, so if I have like you know x the 3r plus 4x^2 + 2 let's say I have something like that. Really anytime you're adding or subtracting you can take this as the derivative of this plus the derivative of this plus the derivative of this. three separate things. And then if we take a look, so I would take this three, this would be 3 x^2 because my understood coefficient is one. then plus 8 x and then here remember every monomial we might you might remember this from algebra that there isn't understood this is x to the zero because each term has a co has a a positive or negative sign so the first one's positive but since it's first I don't have to write the plus so this is still positive my coefficient if I didn't have a coefficient it's one um for something else like let's say x to the 4th that would be a coefficient of zero and it zeros out so I don't need to write that but then I have that sign the coefficient and x to some exponent anything to the zero is one so then two is really 2x to the 0 so then we then that then the next step makes sense because when I do exponent times coefficient it's zero and it zeros out so I don't have to write that. So just as an example of one of the many patterns that come up as we do derivatives. So even though same same step you know uh si.diff the you uh you can take the derivative of each um and we're going to go through next like all these different patterns. um you know just even though you can do this with one line it's still you want you want to have a sense of what what you know what the derivative would be and um you know if if you were doing this without code then you would be in a calculus uh memorizing a lot of these derivative patterns um some calculators will do that for you that the TI 89 will actually do the derivatives for you but the 84 won't um and the 84 4 with Python. Um, what's going on here in the SIM is more than what the uh TI 84 with Python can handle. So, that won't do the derivatives, but the TI89 uh will do the derivatives for you. But we're going to concentrate on doing this in collab um with simp. So, let's take a look at the code and look at a bunch of these derivative patterns. Besides looking at this general power rule and that pattern for derivatives, let's just take a look at, you know, one way to organize uh your Google Collab. Um you could even split out these import statements and the symbols and do that once if you are going to do a lot of derivatives. But I just put that there, you know, importing the symbols. There's our equation. Um I just called it EQ instead of writing out the word expression. There's my equation. derivative and then you see I have just the equation here so that when I run it I can see in nice math print what equation I'm taking the derivative of. So it's good to check that because I I know what this looks like in math print and something more complicated I might wonder did I you know write the Python code correctly. So we check to see that that's what I expect it to be and then I run another code then to take the derivative and then I have another one where I'm going to substitute. So you see my this code block literally just one one work do that because up here I did the the derivative is simply diff stored it as that but I didn't output it yet because I want this to be the last line of code so it prints out nicely. There you go. this so it prints out nicely. And then this one, pick an x value, you know, 0, one, two, you know, three. And then there we go. So we see that this is the original function. This is the derivative. And so I would plug in an x value to get the slope. And that's what I'm doing. So when x= 3, the slope is 27. Yep. 3x^2 I mean or x the 3r gets to be really steep really quickly. So when x is three the slope is 27. Next we'll look at some other derivative patterns. Now that we're starting to get into some derivative patterns. Let's take a look at the chain rule. And this is really important because it comes up in so many different functions. All kinds of other derivatives. So we if we had if we just had like x to the third power then I would take the derivative it's power rule but instead of x I have this whole inner function here x^2 + 1 to the 3 power. So the outer function is everything in parentheses to the third power. Oh I'm going to use power rule. So that's exponent times this unchanged to exponent minus one. But this is where the chain rule comes in. If I have so whatever this is here, that's my inner function. Then the next part is times the derivative of this inner function. So I'll do this. That was power rule to get me to this. And then the derivative of the inner function. Oh, I'll do power rule with this 2 x * 2x out here. And usually, um, especially if you're doing this, uh, with Python code, simplifying for you, um, a lot of multiple choice tests, you're not going to see this as an answer choice because it really is obvious that, hey, you can combine these and simplify this a little bit. So I'll write this as 6x * x^2 + 1^ 2. And so there we go. Chain rule, you know, derivative then times the derivative of the inner function. And again, this comes up all the time. Um, let's take a look at another one that the actual derivative without the chain rule is the simplest derivative in calculus. So y = e to x and remember e to the x e you know that constant oilers's number 2.71828. So the derivative of e to the x is e to the x. How cool is that? So that exact graph the y value of that graph is the slope at any given point. It's one of the things that makes e so useful. It's all these other patterns that are associated with it. But anyway, so this if it was just x, now this still follows the chain rule, but the derivative of x is one. So times one, I don't have to write that. But supposing it was the derivative like instead of just e to the x, it was e to the x^2 + 3x, let's say. So now my exponent has a lot more going on. So the derivative is this exactly e to the x^2 + 3x. So that first part it that doesn't change here. But then it's times the derivative of this inner function. So then that times derivative of this which is 2x + 3. There we go. And so you see the chain rule comes up all the time. time I have these o overall derivative functions but then it might not just be x as the exponent might be some other more complicated things that I can do the derivative of that inner function and so we'll take a look at the code again you can you know simp will output your derivative no problem but some of the things you you want to be kind of aware of what's you know even if you're not memorizing all these aware of kind of what you expect so that if you typed in something wrong and it outputs that you're like, wait, hold on a second. Let me let me double check this. And also for some of the more complex things we'll do um down the road in the in the near future, it it's going to really help you even if si does certain things, it's going to really help you um work with that to get the answer to certain problems. So there we go. So chain rule, it's always that, you know, then times the derivative of the inner function. and we'll take a look just a couple other examples in the code how we can see this play out. So let's take a look at the code. So because the chain rule is so useful and important um we're going to do a few examples here. Um, and again, this will just do the derivative for you no matter what you put as the equation here. But you want to have a sense of like, you know, what it's going to do. So, um, that's actually making use of the chain rule will will be really important in a couple other things we do later on. So, you know, the derivative by the inner function. So, just showing this here. Um, again making sure yes, this is the one I wanted. So you see I have this inner function of x^2 + 8 in the parentheses and then all that to the fourth power. It's not just x to the 4th but it's this whole thing x2 + 8 to 4th. So then the derivative would be this. Yep. So four times this to the 3 power x^2 + 8 in there doesn't change. and then I would have multiplied it by 2x and then notice that simplifies it too. So we see that that is the derivative using the chain rule. Um another one so if I see square root now square root if you remember from algebra I could write that as all this to the 1/2 exponent. So when I take the derivative that to the 1/2 but then it's always exponent minus one. So then the exponent would be negative - 1/2. Negative puts it in the denominator. There we go. And 2x and there would be a two. They cancel. So we see um if you if you weren't writing the code, you would be doing the factoring for all of this. But just seeing that you know all these different some of these different options here and then I could always you know pick an x value and substitute um you know here's one now we'll talk about trig derivatives but we have sine of x to the3 and so the derivative would be this you see the derivative of x the 3r is 3x^2 and And so it's the co the sign becomes cosine and times that just seeing some examples here. Um yeah cosine the so you see the sign of the cosine. So see my inner function can be another trig function. And so that's really making use of the chain rule. you know, I many many examples, but you you get the idea. And just again, this this is going to do your derivative for you, but just to see some examples of what this would look like. Um, I think that's going to be really important. So, we're just going to look at a couple other general rules. Um, even though you're just going to type this in siiff to calculate the derivatives. But again being able to be familiar with what uh what comes out. So one of them is the product rule. So we're going to learn all these different derivative formulas but then supposing I'm multiplying two things and what's the formula what's the derivative formula for that? Well I can actually take them each individually and you know there's a pattern to this. So if I had something like you know x^2 e to the 2x so two separate things you know and we see that as two separate functions because you can't really combine them any more than this. So I can take the derivative and the product rule is first time derivative of the second plus 2* derivative of the first. So I would do x^2 times the derivative of the second is e 2x and remember chain rule it would be then times 2. I'll simplify that in a bit plus 2. So that's e 2x times derivative of the first which is going to be 2x. And so we see that I have this nice pattern these two separate things and you know we talked about derivative patterns for this derivative patterns for this but if I'm multiplying two of them I have product rule and certainly the simp output and you know most other places that's not going to be a good like that's correct that's not going to be a good enough final answer. We want to simplify this. Let's bring the two out front. So it would be 2 x^2 e 2x plus and if I actually that is the convention but certainly since I did this already put the algebraic expression first then e so I'll bring this to the front so then that's 2x e to the 2x and then that's the derivative and that's probably more likely what you'll see for the simp output. All right. And so that's product rule. And then there's one more quotient rule. So here we have two separate two functions that could be separate that I'm dividing. So I don't have a formula for this exactly. I have a formula for this and this. And so I use quotient rule which is denominator time derivative of the numerator minus numerator time derivative of the denominator but then all that over denominator squared. So let's take a look. We have the denominator which is e to x times the derivative of the numerator which is 2x minus numerator. So there we go. x^2 time derivative of the denominator which is e to x all of that over the denominator squared. So e to the x and then all that squared. Okay, so that's the formula. But we can we can simplify all of this. Again, let's bring out the algebraic part first. 2x e to x minus and I already have x^2 here. x^2 e to x all over then that becomes e to the 2x. There we go. And possibly I could factor out e to the x out of here and then that becomes e to the x in the denominator. Like that's another way. So we could say then you know e to the x divide everything by e to the x and then you could get then that cancels that cancels and e to the 2x is really then e to the x. So sometimes if it's you know if you're doing the factoring if it's really obvious then you would simplify it. And there's so many things, you know, there's the focus of this course is writing the code. So really, a lot of calculus could potentially just get into factoring, factoring, factoring. And what we want to do is get the concept of what this would be so that you know it kind of makes sense, you know, understand what the output is. But we're not going to focus on factoring. we're going to focus on, you know, here's some examples of some of the rules and then let's write the code to do it all all of it. Um because that's that's our goal like to understand the concept of calculus, write the code to do the math and then we can really use this for some you know other applications without getting caught up in all the factoring. So let's take a look at just a few again different uh derivative um functions different functions and their derivatives and just going through and just seeing what what that output looks like um in collab. So let's take a look at a few different uh other types of functions and the derivative examples. So going through a few of the main derivative formulas. Each of these does not require anything new if you set it all up like this. The only thing you need to do is change what equation you put in there. And you see like like we did before calculating the derivative, but I'm just displaying this equation. And then in another code block, I'll display the derivative. So I have this run it, you know, just to display the equation. Yes, this is the equation I want. And I could distribute this, but I'm doing it this way to illustrate the product rule. You know, I have one thing and then in parentheses something else as the product of two things, I could do the derivative and it would look like this. Then again, you could combine like terms if you wanted to, but just to show you, you know, an illustration of the product rule, quotient rule. Again, the code's the same. So, you know, you really could just could just put in whatever you want here, but just showing a few of these this equation and it would be something like this. If you were doing this by hand, you would need the quotient rule. Um, and then calculating the derivative right away. You can see some of the time it's uh saving you. It just took two clicks, you know, click here, run this. There's the derivative. And you could always plug in various x values, but you don't don't need to for our our examples here. Uh chain rule we already talked about. So all the different derivative functions. This is worth mentioning with Python because in si exp you know a lot of times we think about our common log 10 to the x but exponent function here in si is e to the x. So simpi exp. I do want to output this so that you can see yes that's e to the x. That's the function that I want to do. And the simplest derivative in all of calculus, the derivative of e to the x is e to the x. So there we go. If I have other numbers um that I will I will use e, you know, e this function ex simp.exp. But if I have another number like 2 to the x, I'm just going to write it like this. 2 to the x and it'll output it this way. 2 to the x. Then the derivative of this is 2 to the x times the log. Now that means natural log. So you know that comes up in the next step log in simply ln in other math notation. So you have to make that adjustment coming from your calculator. So notice if this is ln so the natural log of 2 which is the base if I had something like 3 to x there's 3 to the x and then the derivative of that be 3 to x times the natural log of 3. So that same formula because here the natural log of e is one. So it would be times one. I don't need to write that part. So you see we have you know any exponential function and I get the derivative and we we remember remember that log in simp is natural log in math notation. So you have si.log log. And if we run it, it even still just outputs the log. Just doesn't change it to natural log. But we know then that that's the log. And then when we run the derivative is 1 /x. That is the derivative of the natural log. So change your base formula if I wanted any other base. So it's simp.log. But then I have to do not just x but comma and then whatever the base. So I put it here like this so that I can just change the base easily here. You see and then if I change the base to you know I don't know five. There we go. And that change of base formula it just simply just doesn't want to write it as log with a little subscript for the base. So it's written like this. You might remember this change of base formula using it on a calculator. Solve anything. But there we go. Change of base. And even if you put a 10, you know, it it doesn't do anything because normally you would think just log without a base written there was 10. But there we go. So we have that base and then the derivative. So remember um 1 /x and then in the denominator times the natural log of 10. So you know up here the denominator if it was times the natural log of e that would be times one. We don't need to change that. So we get this and then the derivative. So there we go. Just to quirk with logs all the trig functions. Again, I'm not I'm not going to go through a lot of this, but to show you know all these trig functions simp cosine tangent and you know there we go. Show the equation the derivative. That's great. Cosine simply cosine show the equation the derivative tangent. If we take a look at tangent, there we go. Um, fine. But when I do the derivative, it takes it as, you know, fine. It displays tangent all normal. But the derivative is tangent squ + 1. Now, I always remembered it as seeant squar. This mathematically does equal secant squar. It's one of the pagorean identities. But simply just really wants to output it this way instead of seeant squared. Then I'll take this derivative and one of the other built-in functions if you might remember from pre-cal simp.trig simplify uh trig functions. So if I take this and simplify this derivative, it still it wants to put it as 1 over cosine^ squar which again is seeant squared. So it outputs this correct not may you know if you're familiar with these it might not be what you expect. Same with this one correct might not be what you expect. And then so I really tried to see spi.seant squared to see um you know if it output this. So spi.seant squared. There you go. I just output that. It it can do that. And then so I thought maybe if I just force this in here and output this but no it just goes right back to one over cosine^ squ. it somehow unless I just say for you know no math involved just output seeant squared it will do that every other time it wants to output it somewhere else so this is just something to be aware of um sometimes you know if you're uh using this to check other work or something like that um some things might the a couple of the quirks here they might be mathematically compatible but you know again if you're trying to check your other work. It might not match up, but yet it's correct. Co-angent. There we go. And the derivative really again I remember it as negative uh co uh coseant squared. But this is mathematically the same just written differently. I'm not going to go through all the other but to show you there we go. That is the derivative seeant. There we go. Yep. Seeant. Fine. Outputs it. The derivative tangent seeant. Um, at least that that seems normal enough. I always tend to write seeant first, but it doesn't matter. It's this exactly the same thing. Cosecant. There we go. And the derivative. There we go. Negative cosecant cootangent. Correct. um for whatever reason that displays it as I expected it. But um anyway, so just showing again you could all any any of the trig functions equation the derivative just there's a couple quirks that might not output exactly what you expect. And the same with all the inverse trig functions. Um I just wanted to show you just another thing that you can do. You could have all of them. So inverse trig or inverse sign would be like arc sign. So all of them just have the a in front of them. And you know another way to do this if it's something you use a lot you could put all of them in there and then just like comment them all out and then just only uncomment the one you know you want the one you want. So you totally could do you know there you go there's arc sign and then the derivative. Yep. And then you could say, oh well, let's see. Supposing I wanted uh tangent. And so arc tangent run it. And there we go. So another way, you know, other things you can do hyperbolic trig functions. So again, you go through all six of them and just have the h at the end. That's the common notation for all the hyperbolic functions. This is good because you can, you know, see it will just output it as hyperbolic sign and then the derivative hyperbolic cosine. That all matches up nicely with um the original trig functions. So, you know, that helps you. But then you could have inverse hyperbolic functions. And same thing, I can you can put them all there. you know, a for arc because it's the inverse and then h for hyperbolic. So all of them have this just to show you that all these are still built into simp. And again, you can output any of those equations, any of the derivatives. And these are kind of just like the core basic ones. And any more complex functions or derivatives, you can mix and match all of these. So one of the other twists and we'll talk about this in the next section um would be implicit differentiation. Certain aspects you're going to imply some things but it's all the basic derivatives and you could always run them all and then you have you know a whole uh reference sheet if you wanted to. All right. Now let's take next we'll take a look at implicit differentiation. Now with implicit differentiation, we're going to bring in some of the concepts we've been talking about with chain rule and some other things. So implicit. So that's the adjective for imply where we imply things. Let's contrast this with this is the an example of a derivative we were doing and this is explicit. It's very clear that y is a function of x. And so it still follows the same algebra rules. Do the same thing to both sides. There's my equal sign. Take the derivative of this side. Derivative of y is dydx. The derivative of x^2 is 2x. So you see, you know, one of the things we can do to both sides, take the derivative. And that's explicit. It's clear y is a function of x. And it's clear that that's what we're doing. So now when we get to implicit the implicit differentiation is when we have like x^2 + y^2 = I'll just say 25. Um so I have y on the same side as x and it's squared. So remember, you know, it's not a normal, it's not a function of x strictly because there's going to be um for all all these x values, there's going to be two possible y values. So I definitely can graph this. You know, that's going to be a circle. But I'm implying that y is a function of x. So when I take the derivative, I still want to do that. I'm not going to need to rearrange it in this step, but I'm just going to go through and then take the derivative of each term and remember that y is a function of x. So the derivative of x^2 is 2x. That part didn't change. Plus now y is a function. So y itself is this inner function chain rule. So I'm going to have two y because exponent times this unchanged and then exponent minus one is one times the derivative of this which is dydx and then derivative of any constant is zero. So you see we go through and we take the derivative just anytime y comes up chain rule because it is a function and that's what we're implying here. So wherever y shows up if I had something like x y in my function I'd have to do product rule. So you see how implicit differentiation works. Now that's great but I still want to get dydx equals. I still want that actual derivative. So I'm going to have to do couple steps of algebra. Subtract 2x from both sides and I get, you know, 2y uh 2y * dydx = -2x. Divide both sides by 2y and I get dydx equals the twos cancel so it's -x over y. And you see now in finding the derivative before when I took the derivative and then I wanted to find the slope at that point I just needed an x value. Here I need both. So if I had just an x value then what I would do is I would need to get that x value and plug that in to find possible y values. In this case there would be two. So my slope just having the x value not enough x and then the y one of the y values that goes along with it I could get the slope at that point and remember then this being the graph of a circle you know for one of those the slope will be positive one it will be negative um we'll we'll take a look at that in the code and see how that plays out but you know implicit differentiation this is the math behind it and you know useful to realize when you when you're doing implicit differentiation because as we'll see in the code, it's a slightly different simp instead of diff because we need to account for the fact that the y value can show up anywhere. So we're still implying that y is a function of x and we're you know if I was factor I take the derivative and then solve for this. Okay, so let's take a look and see how all this plays out in the code. So here's the way to do the implicit differentiation with code. And you'll notice some similarities. We're still importing all the same things. And now it's going to be really important that I use both x and y as my symbols. Um I have a couple equations here, but let's look at this one. So setting the equation equal to zero. Before we were setting the equation equal to y because they were definite functions, but it's y could be on either side of the equal sign. y could be squared, you know, whatever, all kinds of things. So everything here is set equal to zero. So in this case I have x^2 + y^2 minus 25 then would be equal to zero. And the next thing is when I do the derivative instead of diff it's iF. So implicit this requires some different things. So it is a different simp and I need one more argument. I have the equation and y is a function of x. So I need to mention that first. If I switch these my derivative won't be exactly what I expect and it will be wrong. So y is a function of x. There we go. Um and so when I just run see the equation there we have x^2 + i^2us 25. And then the implied equals zero. And that's really that the implicit means that we're implying things compared to explicit um where we really spell it out. So there we go. And take the derivative of that. -x over y. Now that's great all correct but I'm going to throw in some plotting here so then that we can put that in perspective here. This x^2 + y^2us 25 if I were to graph it now also very similar import the plot and instead of plot it's plot implicit. So again, slight variations because of the implicit function. So uh Python likes to stretch out the graphs, but this really is a circle just stretched out. But you see four and then a little bit more. It really is. So you might remember this x^2 + y^2= r 2. That's a circle. And then now when I look at this derivative the slope at any given point I see why we do need both x and y because if x is zero you know if we take a look at this look at the top if x is zero then my slope is zero it would be a horizontal slope up here horizontal line tangent line up here. Same with down here actually. But if y is zero that happens over here on the right and left that'd be undefined because I'd have a vertical tangent. And then we can see that u in the first quadrant x and y are both positive. So any point along here would have a negative slope. x and y are both positive but then make the slope make it negative. That's the slope. And down here uh where I would have x and y one of them would be in this case the y value would be negative and times negative makes that a positive slope down here. So we just it just puts it in perspective here seeing the plot. All right. Now one of the other things if I need an xy point. Now for this part I'm just going to I happen to know you know 34 works. 3^2 + 4^2 = 5^2. So I'll just start out with just putting those in. But I need two steps. So we see I need that derivative and I'm going to subs the x value. That is actually the same functions subs. But I need that stored as answer X because then I need to take that and substitute in the Y value for Y. So that'll give give the slope at any point. So there we go -34 because if X is three, Y is four. There we go. Somewhere over here then the slope is negative 34. Now, that's great. This is a nice example that I have set for you. But here's what you would do. Certainly, if you're expecting multiple y-v values, which happens a lot in implicit differentiation, you know, each x this, you know, these x values each have two different slopes depending on your y-value, and they're both very different. So, I could start out with an x value that I know is on the graph. And here's what I'm going to do. I'm going to substitute that into the original equation. Get the x part and then set that equal to zero. So simply eq set that part equal to zero. That's my y part. And then I'm going to solve for that. Solve for y. And then that's my y value. So we see these multiple steps here and then I get four andg -4. So if x is three yes four is one of the is a y value but neg4 is also there and I can take this I have two values each you know yvalue now is actually an array because it has two elements and I'm going to loop through them and notice however many elements I get this loop still works. So y loop and y value. And I'm going to take the same thing. I'm going to say um you know substitute the x value and then substitute you know that first y value and then now I have the slope at this is this [Music] and it'll loop through the slope at 34 is 34s the slope at 34 is negative 34s So we can take a look at this and we see you know this the subtle differences with implicit differentiation. Um you know I need to do the iF I need to do u you know plot implicit and then when I plug in I need to do this in a couple steps because I need to plug in for the x then solve for y. I can't just plug in both in one step. So, I just wanted to put another one on here because this is really comes up with implicit differentiation and it's probably my favorite equation. All right. And then as this is set up, it'd be equal to zero. This is the folium of Decart because you know Renee Deart this this xycoordinate plane he invented this and so this is his equation. Um, now we can take the derivative of this. There we go. Nothing to sneeze at. But then what's this graph look like? Because if you know, if you're the guy that invented this whole coordinate plane, you can graph a loop. So, I just think that's really cool. That's just, you know, they didn't have microphones back then, but if he had a microphone, he would drop it. Boom. loop. And so we see, you know, some of these x values, let's say like, you know, x values like uh one maybe you have like three possible slopes here. So you could do that. Um I'm going to skip this because I don't want to do this. Um actually 33 works for this too, but I'm just going to show you. So supposing if I have an x value of one. So what I can do is plug that in and you see I have all these possible y values. Now one of the things I can do is if I comment this out and then just put y value here. No, didn't get any better. Um, there's a way to make this nicer print. But anyway, just to show you that we have these multiple y values. So then if I loop through them, you know, it just becomes this whole whole mess. Um anyway, and we do get, you know, three different slopes. Um one of the other things you can do is uh there's another one like three. Um if I plug that in, that's not as bad. And then I can loop through that. Yeah, not as bad. At least it's like manageable in this one screen. So you see some of the things that we can do. You can have you know all kinds of weird answers. But the key is you know implicit differentiation. How can I still get the derivative? How can I plug in different values? How can I find the slope at these points? It it's a few extra steps here. Um you know there we go. And this next we're going to get into just putting this all together and you know again building your calculator so that you can just plug in any uh any derivative graph it do all kinds of things. So there we go. We'll take a look at that and I'll leave you with the volume of Decart. Now, we're going to get ready to put it all together. And when you think about calculus or the derivative, what are the steps? What are the things you want to do? You want to see the function, the equation. So, we'll look at like just putting that in, displaying it to make sure the code you write matches what you think this function should be. You want to see the graph, and you have the function, the original graph. You want to be able to take the derivative. So see that equation for the derivative. Then graph the derivative. Maybe graph the derivative and the original function on the same graph. Then what you want to do derivative at a point because very often okay what's the slope at this point? That's what you want to know. Then maybe you want critical values which is take the derivative set it equal to zero because those values tell you when is the derivative zero which is a horizontal tangent which gives you the minimum or maximum points. Um so derivative equal to zero to get the critical values and then you might want to go to the second derivative because very often that's important then you know how quickly is the is the slope changing you know the second derivative and so we can go through that second derivative see the equation see the graph see maybe all three graphs the original function derivative second derivative all in the same graph and also you may want to take the second derivative at a point or the second derivative set it equal to zero because then that'll give you the inflection points. So all these things that you know that's we're just going to look at putting this all together into your calculator and setting this up so that you can just put in one equation and then just run the code. All right, now that I have that original equation, I can go through and see all these different things because if I'm trying to answer specific questions, each of these is going to give me the information that I need. So let's take a look at how to put all this together in the code. Uh notice in any of your collab notebooks up in the top left you can click on this for the table of contents. So we put all these we already had limits and everything we did with limits graphing. So now we get to derivatives and over here. So we see uh what I did here is derivatives. If I double click this one hashtag. So that's our heading. And then to make everything indent, the next things are two hashtags, a heading, but it's indented under this one. So that way you can you could minimize all that or you know minimize just one thing and we have all the headings. So you would go to your table of contents. What do you need to do? And if you go to derivatives, we can even X out of this. And we have the first thing. Hey, what do I want to do? Remember, we went through all those different derivative formulas. But all of it boils down to this code here. I just want to display the y equals equation. So all this importing um again by symbols I use x and y, but for the next few steps I probably won't use the y variable. Um, and whatever the derivative, you just put the equation there. Um, this would be the y equals equation. And I just have this extra print out that this would be y equals. And then it displays the equation. Then we get to the derivative. Um, notice that I just completely did as a separate thing here. So equation, the derivative, uh, si. And I just had it print out here the derivative equals and then just so if you look if you go back to and look at the glance look at it you'll see um y equals you you know if you're making yours you could eliminate that print statement if you want. So we display the equation, display the derivative, find the derivative at a point, and then you have your one line here, x value, pick an x value, and just put that right there. And then all the rest, you know, derivative.subs, and then it will print out the slope at x equals this is this. There we go. um you it probably will look okay, but you could put another space there just to you know this is where you you could always tinker with that output and have it display. So we we find the equation the derivative slope at a point set it equal to zero. Again common things we might want to be doing so I go I still have that derivative. So make it an equation. Um simp.eq set the derivative equal to zero. This will display it and you'll see the equals zero equation and then solve it because now that's first and then we'll solve that to get the zeros. Again you could the way that that outputs you could see multiple values. So at this point, we could plot the original function and the derivative. And if you just wanted to plot the original function, you could just delete something here, or you could add another code block here. Maybe you wanted to display the original function and then and then graph just that. That's also possible. Original function and its derivative. There we go. Remember the one line of code to plot it. So it will center it nicely for you. So now we get to the second derivative and that's why this variable here was called first because it was the first derivative. And now we get to plot it. Now we get to the second derivative because sometimes that's important. So I just made this long variable named second derivative. And there we go. Just simp. Take the derivative of the derivative. Display it. Find that at a point. We go through all the same things. There we go. Second derivative. And there we go. Um the and it's the it is the slope of the second derivative. So you could keep that word in there. And remember um at a point it was really the inflection. It's how quickly is the slope changing. But it is the slope of the second derivative if you wanted to keep that word there. um second derivative set equal to zero because that also gives us and now that's why I use that variable second. So, senpai.eq set the second derivative equal to zero find the zeros. SPIs solve and then output that. I did use the same variable here zeros um because the other one would have already printed out. So, it's okay to overwrite it. You can always change those variables. And then I decided to plot all three functions. There we go. Because then sometimes you can see that you know the the the y value of the derivative is the slope of the original equation. The y value of the second derivative is the slope of the derivative and you know other uh implications here. So you see all of these and then that way you know derivatives you see all the things you want to be able to do with it and you can jump to one of them if you want. it kind of all builds. So it kind of makes sense to just go down the list and you know you start with one and do all of it and then I have all of it for implicit differentiation as you see hashtag that's its own heading and you know just the setup here all those all the same code equation equal to zero and then siidis eq comma y comma x takes the three arguments there. Yes, I want to plot it. Plot implicit. So, display the equation. Plot it. Find the derivative. So, notice this one I did want to plot. Where's the you know it just it just helps finding the derivative um an xy point to find the slope. So you start with an x value and looking at the plot or having an idea of the equation just what x value pick an x value that will work and it will go through and find like we were doing before put the original equation you know substitute that x value in then that becomes x part then set that equal to zero siq set the x part equal to zero that's the y part solve the y R, get the y-value, and then you print the y-v values. So now we have an x value and a y-value. You can loop through those yv values and print out this the slope at this x value, yvalue is the answer. Uh y loop because that's what it's doing here. All those different y values, each of them become that variable. And again just the variables I picked you can slightly verify vary them you know that's the idea that you know you want to understand this well enough that you can you know put in your own own values if you want all right so notice we're going to get the slope at a particular point maybe a few different points and I will plot the equation um now notice this is just the equation um which you could have uh just plotted this here. That actually works. But this is another way um to have more uh arguments in here. Especially you need all these extra arguments when you start throwing in, you know, a couple points here. So I want to plot this equation, but then I also want to plot these markers, which are these points. So um and because this got really long and you know Python convention is that you know the lines really lines of code shouldn't be so long it even gives you that line here to you know guide um so I could I could continue this past that line but Python convention says you know keep it shorter than that. So I just made these two other variables. I could have just stuck with these variables. And notice of that x value, I'm going to pick one of the yv values. I just picked the first one um and just storing them so that so that when I plot them here that's a little bit shorter and I took out all the spaces. So there we go. Very often I might want to plot the original. I might want to plot the original on a particular point. Um but we often for the implicit differentiation we often don't really plot derivatives or second derivatives. I mean you absolutely can go back and find the you know we have the derivative and we absolutely can go back and find the second derivative if you want. Um sometimes that's just an interesting exercise in doing that. But usually it's the what's the original graph look like? What's the derivative at a particular xy point? um that's usually what we'll do for implicit differentiation. So there we go. Now you have it. You know, as you you know, we've been going through a lot of different examples, but then you can put it all together here. You're, you know, you're ongoing building your um calculator and then you come across other things. What are the things you're going to need to do? Oh, I might need to find a limit. I need to graph. I might need to find you know a lot of the other applications to answer the questions what are you often needing to do derivative you know at a point. So now ready to go and we can get into some other applications of this and using using these. So we're just going to look at an example here of putting all this to use with like one particular equation here. So y = x3 - 9x^2 + 24. So let's say you know your math intuition tells you you see the x^ the 3 power and other stuff. So you get the general sense that the graph looks like this. But more specifically without just graphing it you know it looks like this. Where is that maximum value? The local maximum and where is that local minimum? Remember it's local minimum or local maximum because this will continue on forever and then that's going to be the absolute minimum and then this will continue on forever. That'll be the absolute maximum. But you know what what x value gives us? this maximum, this minimum and then this inflection point because if we look here, you know, this is concave down if I look just this side. And so then if I look at this side, it's concave up. Well, somewhere in there, it changes from concave down to concave up. And that's the inflection point. So we get that from the second derivative. So what we're going to do is we're just going to take this and you know we have this general sense of what this graph might look like but we're going to walk through the steps and you know look at this look at the equation look at the graph take the derivative derivative at a point you know set the derivative equal to zero to find the local minimum and maximum and then we will you know take the second derivative look at the inflection point and you know put it all together. It's just putting together pieces of the puzzle for things you might want to know about a particular equation. So let's take a look at how to do this in the code. So this is how we can put it all together and use that calculator notebook that you're building to calculate the derivative and just you know whatever you need to know about a function. So all within the derivative all of this code you'll keep and if you just keep that variable as eq for the equation all you need to do is change this. So we'll put this equation here x cub - 9x^2 + 24 and just you know that's written in Python. So if you want to display it might be a good idea then to display this and you know there we go we see it nice math print. So now we're just going to build upon that. That's the equation. And then when I do the derivative, I'm just going to apply that equation. So I don't have to change anything. I have this notation here. You'll see in a second because the uh simp it's great. Look at that. Plot it all with one line of code, but it seems to compress it a lot. See how really big this yaxis gets? You know, -250. So if you added these parameters here and if you made that y limit. So what's that minimum? I I want to at least see this turnaround. So I might even just call it -200. And that's going to be plenty. And then the maximum, let's even trygative -100. And then the maximum would be 100. And we remember that I need to close that. So you see we added this other parameter and there's other ones you can add but this is kind of the big one either x limb or y limb or both. So that when we plot this we really see instead of stretched out you know that y val that y- axis is negative 100 to 100 and now even that is still more way more than that x axis but it's enough that you can see that nice turnaround and that's kind of what we want like what what's this graph look like? So there we go. There's the equation. Plot it. And what's the derivative? So, we'll have this just built in here. Derivative. And all this, it's, you know, I'm taking the derivative of that equation. Nothing needs to change. You just run it. So, once you put the equation in, once it should be able to cascade down nicely to each of these. What's it look like? What's the derivative? and slope at a point. Now, I just put a default in here x value zero and you you could pick a point here, you know, oh, what do I want? Well, what's the what's the slope at two? You know, I expect to be kind of a steep negative slope. Whatever value you want, you can put in here, two, and slope of two is -4. Set the derivative equal to zero. Now again just in two steps here just to show that that's what it looks like and then solve. We're going to solve that one for the critical values here. So we see 0 and six. So when we graph the original function and its derivative. Now I can put that limit in there if I want again. Um because again it's not you know it's kind of compressed. And look at that. You know, even just this little bit is 500 to negative 500. So maybe I want to put that in there. Um -100 to 100. That's probably again that's probably going to be plenty. And it's only on the y value. So you you know you can do this on the y or on the x. And we see just adding that other parameter. So we still see that nice curve. And we see this. So that the derivative we see the y-value of the derivative is zero right here where the slope should be zero. and we go down here over here at six because we found that exact value that right here the slope should be zero and the y-value of the derivative is zero. So we found those values at zero and at six and then we see what it looks like together with the graph and second derivative. So I just take the derivative of the derivative and there we go. second derivative at a point. Um, yeah, I'll I'll put this one. The y-value is six. You know, you can leave a default value in there, but at six and second derivative equal to zero at three. And we see that's the inflection point up to up to here, it's concave down. And somewhere in here, it changes to concave up. Where is that point? I set the second derivative equal to zero. Uh so the second derivative at a point uh I still kept this as slope. It's the slope of the first derivative though. And second derivative equal to zero is three. And we can plot all three functions. You could still get in there, manipulate it and add things, but you have a lot of it just already in there built in. And that's what we want to be able to do. Just go through and you know, you can even take out this y limit if you want. But the big thing is that you start out with just changing this equation. You know, if you didn't want to change that y limit, you can just go through and just it cascades down. See everything you need to about derivative, second derivative, what all the graphs look like. And you know you can apply that to all sorts of things which is what we're going to do. So let's take a look at some other applications. Let's look at a financial application here. And you would need enough information enough enough data points to get this equation. But let's just say um you know tickets to a concert. So if you had enough information about all these different um concerts and you know how many tickets were sold, what was the price, find some way to get some correlation or regression line and develop an equation like this. Um I kind of uh made this up from like some information that I've seen. Don't take this as like oh yeah this is a good uh use but with some data points you can find something that uh you can develop an equation like this and that's kind of the key link to this to some financial applications some data science this is what data scientists might do can I get all that information to develop this equation of you know supply and demand can I get an equation for this demand because Then I can turn this into a y equals equation. Oh, okay. You know, y equ= 31,500 - 100x, let's say. And then we see that um, you know, might not be perfectly linear, but that's okay. That gets us a good example here. Um, because, you know, if x is the price, you know, maybe a lot more people uh would go if it's free. the f if the price is zero. So there's a little curve there. Um but and you know and then whoever it is there's definitely going to be a price that is so expensive that you know people will decide okay I'm not going. So if I have this then for the concert promoters you know the business side of it they want to maximize their revenue. And what you want to get into this point is anytime you hear minimum maximum you want to think calculus. So maximize the revenue. What is my revenue? It's the demand. How many tickets did did we sell times the price? So I could have that um you know equals the demand 31,500 minus 100x times the price which is x. You know that's what we said here. So if I have that and let's just distribute this 31,500x - 100x²ared I'll make this its own y equals equation and again your algebra your math intuition might tell you hey x2 that this graph is going to look you know like an upside down parabola and there is some point there you know some price that maximizes revenue What we want to think is calculus, okay, y and then you know shorthand for the derivative I might be might do y prime and all these are just power rules. So that would be 31,500 minus 200x. And then that would be the derivative. And I would set that equal to zero. Solve for x. And we remember then that that would just maximize. And then a little bit more. A little bit a little bit on either side. We're getting close to that maximum revenue. So this is where, you know, people might make that decision. Oh, okay. Well, anywhere close to that maximum revenue, you know, what what's the size of the venue? Is it worth it to go to a larger one or a smaller one? You know, because we might not always exactly get, you know, that number, you know, we could try, hey, what exact ticket price uh maximizes that revenue. But there's a lot of other factors. You know, we we want to do the math, but life doesn't always work out so neatly like that. But as an example here, so we'll take a look at this in the code and just see how you can actually set this up that from some other source if you were able to do that, get that regression equation, get that equation that then you could set up that then it would just cascade down that once I have that I can figure out what would maximize that. And people would do this for all sorts of things. if I can get enough information um you know what price should I charge for something so that that's you know using calculus for the financial applications but let's see how we can work this out and get get these answers in the code so here's the example I have for you here this actually comes from somebody online who actually wanted to ask a question about his particular store some product they raised the price from $6 to $7 And notice the demand dropped by four people. So the question is is was that a good move or the larger question is what would be the price that would maximize revenue. So I mean if you wanted to just maximize customers then you know the cheaper the better. But to maximize revenue, there's a price in there that you see that price $6 times 480, there's the revenue. $7*476, that would be revenue. And we'll we'll look at those and and and check those uh later on in this example, but we'll say that you know what, there might be a whole another price that would maximize revenue. And the way we have this set up, the way I have this set up that if you just put in different prices, you know, um, so as an x value, because if I were to graph this, x price one and y is demand here. Why demand one? So if you have two data points, there you go. Price demand, price, demand. That's enough because the more the better. But really what we're doing here is we're doing a linear approximation. So if I have two points, I can figure out the slope and there's my slope formula. And then I can figure out the y intercept. Remember y mx plus b. So it's that y value minus slope times price or y - mx. So then I can print out the demand equation. There we go. And we can check the math. This works out. So the y intercept would be 504. Um but that would be a price of zero. Nobody really wants to give away stuff. And yes, we see an increase of $1 decreased the demand decreased for customers. So that's why the slope's negative4. So we see this. There we go. And I have these as definite numbers. So now when I want to get them to simp, I really want to put them into that equation so that I can solve. I like the way si solves. It's really the theme we're going to be going for in a lot of the stuff in calculus. Simpai does a whole lot. So I import simply symbols x and y as my symbols and my estimated demand equation. So we have y= mx plus b slope times x which now is a symbol and then plus the y intercept. So now that's my demand equation which then I will I could print that out. I'm not even worried about that. I'm going to generate my revenue equation which is the x value times demand. And that's what I'm going to print out here. So there we go. Um with simp first it puts it second. That's fine. And it doesn't distribute. It just says x times this. There's my revenue equation. And you might remember from algebra when we distribute that's going to be a -4x^2 and get that that parabola that opens down. So somewhere in there there's there's an x value for price that will maximize that revenue and that's what we're going to find out. So max revenue. So calculus the calculus answer I'm once I have that revenue take the derivative with respect to x in this case and then simpi.solve automatically solves it for zero. So there we go. So I'm going to set that as max x. That's my x value that would maximize revenue. Then I'm going to take that and I'm going to go to the revenue equation. substitute in for x max x x and I store it here as the variable max x x but the way simp works it has to be max x and then that zero in the square brackets because it really is an array there could be multiple zeros there could be multiple answers so even if there is only one and in this case there is only one um I still have to put it this way so now I solve you So there we go. Maximum, right? Uh calculus, you see here, minimum or maximum. Take the derivative, set it equal to zero. And to get that yvalue, I'm going to go back into that revenue equation and substitute in that x value. So there we go. All these things that if you couldn't already set this up with code, you know, that's a, you know, you get into a bunch of factoring, some tedious calculations. This is already set now. And we're going to set here's the maximum revenue is this when the price is this. And we get this surprise maximum revenue $15,000 when the price is $63. Now, this might sound ridiculous for some item that he he was talking about going from $6 to $7, but the math of this works out. And so, looking at this, I just did a quick check here. And, you know, you can even leave this in here. Maybe you know, you doubt some of the answers you get. So, you can leave this check in here. So the revenue one I take I'm just going back to those original equations price times demand revenue two price two times demand and then revenue three um rather than relying here I am just going to use that that x value times demand and I needed to do that substitution here too because the demand also had that x. So now I'm going to print this out, but also I'm just going to actually solve for demand because I think this is interesting too. Given that demand equation, where where is that zero? And when we do this, the numbers really just confirm everything even though it just seems ridiculous for that $63 that revenue 200 $2,880 or revenue 2332. Now at the very least for that person's specific question you know oh I see that raising it a dollar lost four customers but the revenue absolutely increased. So for that specific question then that's answered. Yeah keep it at $7. This takes it even further saying hey you can raise it up to 63 which is kind of ridiculous but we'll talk about that in a second. and the revenue three just the way we do this it just confirms yeah that would be the same 15,000 so what what did I do here demand not subs so um this one here gives me that demand just because I wanted to see that too so we do see that yes from the original demand of 480 and then yes we are losing Let's say every dollar increase we lose four customers that it really would be if the price was $252 that would be uh you know that's where the demand equation goes to zero. So you know that would be that's you know the point where people be like oh that's rid that's so ridiculous there's no way. But it comes back to that yes the demand definitely decreased. You know we were talking 480 you know the original we're saying from 480 476 every dollar increase and it goes down. Yeah. You only have 252 people to take it even further. I just was curious about the zero the 126. So if the price was $126 nobody would buy it. But this is just interesting. It shows that yeah 252 and then if we do this um you know do that 252 * 63 you know the price said it was should be 63 * 252 you actually get to that 15,876. So you know that works out too. So what can we do with this information? You know, does this really mean that this person should make this item that he was thinking of $6 or $7 be $63? Probably not. Though, here's the the the danger of it. Even though this particular one seems to be very extreme, what happens is people can do this math and people do and if they have enough data to really confirm that whatever they're selling should be a lot more expensive to maximize revenue, they'll do it. And you know there's uh you know greed greed's part of economics. Um so you know people will you know maybe maybe try it you know make something really that expensive. Um but the idea that you know we could uh you know it it answers that simple question. Yes. Was the slight increase worth it? And the answer to that simple question is yes. What should that price be? I mean, if we're saying $63 is seems extreme, then really anywhere, you know, whatever that person decides to increase that price, it's probably going to be just fine because that's still on the upward slope. We really need to also have that human interpretation here, too. Just because the numbers say, "I probably could sell this for $63," doesn't mean I should. The way that this is set up, if if you're doing this based on two data points, you just need to change these values here and everything else. I have it set up in separate code blocks so you can run each part and see. But if you just change those two values, you know, run each code block and then you can see it gives you this answer. But then you can get that analysis because if if if you're running a store or if somebody running a store hires you to look into this um you you want to be able to get the correct numbers but you also want to be able to provide the human insight and do the full analysis. So that gives you an idea. And as with so many things in calculus, this is the main there you go, one step of calculus right here. Remembering that minimum or maximum means take the derivative and then set it equal to zero. So there and then the next line setting it equal to zero. All the rest of this, you know, we're certainly now getting into Python code, but then it's algebra Python code. one line of calculus. So, um, yeah, in in doing calculus, there's definitely so many other non-calculus parts and, you know, I want to make sure that, you know, you're ready with the human insight to fill in those gaps. Here we'll talk about one of the physics applications that really goes back to Newton and some of the things that he really developed as an offshoot of calculus was really calculus being the language to explain some things happening in physics. And so we have a general projectile equation here that and this is upward motion. If I launch something straight up in the air where u there's other equations that vary based on the angle um we we'll look at them too but if I had upward motion so this position equation where is this projectile and really if it's upward motion that's position is the height at any given point. So y would be the height and then x in this equation would be time in seconds. So this first part 4.9 because we'll do everything in meters. So that's half of gravity. Um and we'll look at why that's half of gravity in a second. But -4.9 x²ared and again x is time in seconds. that will be the the same number in any of our equations because that accounts for gravity v for initial velocity. So I'll leave that open there as you know that could be whatever number we put in there if I know the initial velocity and h would be the initial height. So something starting at ground level h would be zero. I could even you know drop that out. um something starting on a cliff let's say or somewhere up in the air and then dropped then v would be zero and then that term would drop out. So you see you know that's it you know h could be zero v could be zero but we'll put it all together as you know anything that's possible in this equation. So and we see that you know anything launched becomes a parabola we get thatx^2 and that's the thing it really always is a parabola. So then let's talk about the calculus derivative of this and I I'll use that shorthand notation y prime. So if this is the position how quickly is the position changing y prime is velocity. So I have velocity and this is power rule. So what do I get here? 9.8 x + v. H would be that the zero drops out. And now we see this is gravity. So we have that init, you know, what's my velocity at any given point? It's that initial upward velocity minus gravity. So gravity keeps working against it. You know, you have that initial upward velocity once, but then gravity keeps working against it, working against it. So, it will get to a point and then turn around and then come back down when it you know can't overcome gravity. And then we look at escape velocity um for you know launching something into space you know how much does it have to accelerate to escape that to the point that the height it gets to it's beyond Earth's gravity. But you know all these things kind of start with equations like this. You know there we go I have velocity. So again if something was you know um dropped from somewhere then the initial velocity would be zero and we really just see you know something falling you know it keeps accelerating. So 9.8 m/s going down and then it's going faster. two seconds it's going you know almost 20 meters per second etc. So that keeps falling. And then if we take a look at this, the derivative of this, how quickly is velocity changing? And that would be acceleration. So that would be the second derivative. And that's why I use y prime because then this will be yblep prime. The second derivative is that drops out. And then this is really just 9.8 gravity. So we see this that I have the position at any given point of a projectile either launched upward uh or dropped and velocity I can figure out the velocity and then the acceleration and what we do is then given these you know maybe if I knew the time if I if I threw something up in the air and I calculated how you know to stopwatch how long did it take to go up and then back down to the Then I would know in this case I would know X and then I can figure out the initial velocity. You know I would know the initial height too. So if I knew, you know, how long it took, I can figure out what initial velocity. Oh, I threw it up in the air. I was wondering how fast I did throw it. Well, I can calculate that. Or if I knew the initial velocity, I can predict how high it will go because there we go at the top, you know, velocity is zero for that split second and then it drops back down. So position, how high will it go? Maximum derivative. Take that derivative when is and set it equal to zero. When that velocity is zero for that split second, it's at the top. And then I can figure out that maximum height and then it comes back to gravity then. So we can look at ju we'll look at a couple examples of just how to set this up in the code so that you can plug in some different numbers and you know uh figure out some things that you don't know. So let's take a look at how we would apply this in the code. So here's how we can put this together. And this is all very similar code to the general calculator notebook that you'd be creating. We're just modifying here, making t the symbol because time. And I'm just putting in these numbers for initial velocity and initial height, but it really could be any numbers here. And then you just put in whatever velocity and height you want. And our position equation as the original. So we have that -4.9 t ^2 that will be a part of it and we just put in here times v plus vt + h and that would get plugged in here. So you can run this and see that equation and the derivative. Well, we'll call this one velocity since we're using this for a specific application here. derivative and we're just going to output velocity is this. There we go. at any given point. You could also at any point, you know, put in another block of code and um that velocity subs and plug in a value. What's the velocity at any given time? And here for the height, remember taking that velocity and solving it for zero. So it will automatically if you you just say spi.solve velocity, it'll automatically say, yeah, okay, that set equal to zero. So we can get that height and once I get that x for the max height I will get the max height by plugging this in. I'll take that position function that original substitute in that max height um and it will be an array that's why I have to have that zero here because there could be multiple answers so it allows for that. So I'm going to plug in for t for that I expect only one. So you know max height zero um that first answer and I'm going to print it out. So we can do this and you see we get you know where is that max height at this many seconds and the max height at this point is you know all in meters 17.79. So this is just for fun. Acceleration is gravity 9.8. So there we go. So really you can see you know you can have other notebooks if you want for various applications but this is just one example for our physics applications and there's many more here we're going to talk about just one slight variation on the derivative but this becomes an important distinction and and we'll revisit this again in some future topics but it's the differential. So if I have my y = x^2 and we take the derivative, I'll call it dydx = 2x. But then I really want to take the dydx. That's that's its own thing. You know, the derivative of y is dydx. And that's the slope. The derivative of this is 2x. And there we go. But if I do decide to split it up and say, "Oh, instead of dy dx is the slope, I want to call it the differential." And I'll say dy equals 2x dx. You see, we just multiplied by dx on both sides. If I do that, and we'll see this come up in just a couple applications here and there, then I really do need to go back to the linear approximation. So here I can just say, yeah, that's right. the derivative slope at a point calculus here. Even though I did this and you know I know that that's calculus to get to this I do want to go back into what is that exact little change in dx that I chose and plug that back in slope formula derivative by definition. So just knowing you know derivative differential you know these subtle differences that's just uh an important distinction but in putting this into practice we're really going to look at this in the code and just see how we can write some code to do that when I need that linear approximation to u find the derivative at that point for some you know exact value of dx that I choose. So let's just take a look at how we would apply this in the code. So here's what this looks like in code and we'll see we'll use that similar example. y= x^2 derivative is 2x. So then that would be dydx. So then dy is 2x dx. Splitting it up like that. And we'll set it up here in a way that you know these imports we you know this equation you could use this again for any other equation um besides this because then we'll just set up eq take the derivative great and now looking at the differential we can just pick any x value here and you know dx how how much how many decimal places you want um this is probably fine. And so dy so let's think back to the slope formula. So dy here we'll calculate it this way. Um there we go. Going back to that original equation subbing you know for the x value x plus x value plus dx minus that original sub x value. So we have, you know, change in y is the true dy. And knowing calculus now, hey, if I just want the slope, I'm going to take that derivative, substitute in for the x value. And so that's just the slope at that point. This is our go-to. That's what we want. But we're going to show how similar of these other answers are. So I'll print out that slope, which is using the derivative. That's going to be our go-to best answer. But to compare dydx and this way. So I will have dx up here whatever I defined it as this tiny value and then dy and I'll actually just do that division. You know what's that answer or dy and just going back to here dy this way. And I'm going to point out dy the other way, which is in this case 2x dx or that slope times dx. And when we run this, we'll see really how close they are. Slope is four. Yes, that's right. That's the exact. And if I did it this way, dydx, it makes it really similar. Uh just, you know, off by so many decimals. And dy calculating it one way. We see again uh dy calculating the other way. So all these if I take it up to four decimal places they're the same answer but it's beyond that that I go and that's probably not much of a coincidence that you know that's how many zeros I had here. So just another way to see you know um how this works and um sometimes the differential you might get in a situation where you want that differential instead of just the derivative and you can use this code to do that. So sometimes we want the equation of the tangent line and that could be the well the tangent line is the straight line that touches that curve at exactly one point. So we're you know we're going to get to a linear equation here. But sometimes that that line is just you know I want to see that tangent line but sometimes it actually just has to match up. Like let's say I wanted to design, you know, a highway or a roller coaster that has a curve and then at some point it straightens out and then from that point on I want it to be, you know, linear, perfectly straight. So that's only going to happen and be nice and smooth if at that point the slope is the same. So what can I do? I can take the derivative here. Derivative, I'll just say approximate it with x squ. And then there's the derivative is 2x. So at that point I have to define you know what point do I want? Let's just say I want it to be where x= 2. So when I plug that in * 2 is 4. So at that point the slope is four. So um I want to see okay for that point x is 2 the slope is four. But then I want to f I need that y value. So I'll move this out of the way. If I have x is 2, 2^2, y happens to be four also. And I want that linear approximation. So if I have my x value, my y value, and the slope, yep, it goes back to y= mx + b. And you might be thinking, hey, I thought I was doing pre-calculus with py or calculus with Python. What's this algebra stuff? Yes, algebra comes back up. We're doing a lot of stuff even in code with Python, you know, Python to do your calculus. Um, but if you weren't writing code to do all this, there's a lot of problems that are like one step of calculus and a lot of steps of algebra and some things like this. Hey, we knew we used calculus to get the slope at that point, but this plugging in stuff that definitely goes back to algebra. And then I want the equation of that tangent line. That's a linear equation. There's my go-to y mx plus b. So then I know my y value is four. My slope is four. My x value is 2. And I'm going to solve for b. So 2 * 4 is 8. Subtract 8 from both sides. So then I get -4als b. Now I have all the pieces of the puzzle. So, I can put this all together as y = 4x - 4. A little throwback to algebra there. But you see, like you think you're done with algebra, it comes back up throughout calculus and other math courses. So, we're going to look at how to do this in the code that, you know, take the derivative, get the derivative at a point. We've been doing some of that but then also get that actual linear equation at that point. So bringing back you know just plugging this in plug you know plugging these values into the equation we need to getting these values getting the linear equation and yes it ends up being these quite a few steps. That's really what calculus ends up being and and math from here on out quite a few steps. So then hey if I can write a few lines of code to do this then maybe I can get this point to ask that question slope at a point if I plug in you know my particular equation and a value x value um the code would then do all this for you. So that's what we're going to look at how to do all this and set it all up uh using the Python code. So let's take a look at the code. Here's the tangent line equation in code. And we see in the beginning we're going through this the usual imports set up the symbols. I'll stick with calling the original function eq and we'll do we'll stick with this one as an example. x^2us 4. So we'll calculate the derivative and I'm going to pick a point some x value. So what am I going to do? So, I'm going to substitute that in uh to get the slope in the derivative. I'm going to substitute and then I'll substitute that in in the original equation to get the y-value. So, now I have, you know, all you you can set this up and all you have to do is change whatever equation you want and what x value you want. And there we go. We go through the substitution. Now I have the slope from the derivative. Now I have the y value and y = mx + b. So y - mx gets you b. So the y value minus slope times the x value and that's b. And we're going to output this. So for our equation here, there we go. We just run it. And there we go. y = 4x + agative8 4x - 8. There we go. And all you have to do is change that. Maybe I want just a basic x^2. And what's my tangent line equation at an x value of three? There we go. Plug it in. y = 6x - 9. So there we go. The slope at that point is six, but the y intercept is all the way down to9 because that steep slope. And here we go. So you can set this up again very often. Some things we want to do get the tangent line equation. So we can just change whatever my original equation is and what x value and go from there. Now parametric equations uh par meaning going around. So parametric equations have I can something that I can graph on an xy axis our normal cartisian plane but I can define x and y as their own equation each as a function of t which is time. So I can have my two equations X defined by some equation Y defined by some they can be any equations. So on the XY axis on our cartisian cartisian blade we can see things that look like not a function. I can have you know circles. This actually would be the graph of a circle. Um uh but then you know each of them time is the one variable that increments sequentially and then you know okay now the time is zero then one then two and you know it's continuous so even the in between values time can be whatever and then once I know that time val value you know I can figure out x time value then I can figure out y and then I would plot that xy point. So let's talk about this in relating to calculus. Then if I would take the derivative of this, great. I I've graphed this and this happens to be a circle. But now that I have these points, but what was the derivative mean? Well, if I have this derivative, let's do the y first. It would be dy dt. So not dydx, but dydt because t is my variable here. So the derivative of s is ne is cosine. There we go. And I'll just put the other one here. The derivative of of x would be dxdt. So again we see things each of them is a function of time. And derivative of cosine is negative sine of t. So I have my two derivatives there. Co derivative of cosine derivative of s. Great. But how does this dydt and dxdt relate to my slope on the graph? Which I wanted to be dydx. you say change in y over change in x because it's still these are separate but then I graph them together as this one xy point. So I can look at this and what if I have dydt over dxdt dydt and I'll put a big line here over dxdt. So if I have, you know, the fraction over a fraction, dydt / dx dt. Well, what does that equal? That would be dydt times dividing by that fraction is multiplying by the reciprocal times dt over dx. You see dividing and then multiply by the reciprocal. And when we do that, dt cancels out. So then it really is dydx and what is dydx then it's this derivative divided by this derivative. So in our specific example here so when I do that when you do parametric equations you would take them and then take the derivative separately relating to t. But then the derivative the slope in that cartisian plane dydx is my dydt in this case is cosine of t uh over dx dt which is in this case negative sine of t. So that would be the graph of my equation then. And there we go. We see that we can graph these. We'll also look at in the code as how do I you know how do I do this? How can I separate this out in a nice way that I you know it works every time. Um defining these variables. Now I'll define t and we'll define these as separate v separate in the code. I'll have um you know different equations here and then take the derivative. We'll look at how all this plays out. But you see this is this is how it comes together as parametric. um x and y are each defined separately but I'll still graph that as one as xy points on our cartisian plane. So I'll take the derivative separately but again I can still combine that and we can see how that those two separate derivatives actually do combine to get dydx. So pretty cool how it all comes together and let's take a look at how to put this together in the code. parametric derivatives. I'm going to do this first part here, just the usual imports, x, y, and t as symbols. And I'm going to define y as this function of t. x is the function of t. I'm not going to worry about any output here. I just want to run this and just have it that this is my x and y. Then just to see what the graph looks like because I'm going to graph those on an x ycoordinate. Here's one way to do it. And I want to introduce this because sometimes you might have data that's an array or other lists or values. So this is how you know my x and y values empty arrays. And I'm just going to loop through it for some a value um you know for angle. And then I'm going to come up with these, you know, xt. I'm going to substitute um that in for t. And what this is really doing, because I did s and cosine, I just had like pi over 12 um as the increments, you know, 1 pi over 12, 2 pi over 12, etc., and go around the whole circle. So that's just what I did here with with this and, you know, the range of 36. So, you know, substitute in uh for t in in my x equation and then y and then I'm going to append append that to my array and then plot this. Um notice I imported the mattplot library um to plot these. Again, I want to show it. It works better for an array. And then you see just because of um the limited number of values we see not a nice smooth circle. And now let's look at the simp graph. So I can go right back to this. I could even you know since I have that I could just put the graph in here right away if I wanted to. And from simp if I haven't imported that yet import plot parametric. And there we go. Plot parametric. What am I plotting? X Y. And then as T goes from 0ero to 2 pi. There we go. And so we get that nice smooth graph because it it has all those values. So you see we can have those. And just, you know, I wanted to show you this, but you could easily put that with the import. And once you have the first two, just, you know, automatically plot that. So we see what this looks like. And separate derivatives. Here's another one where you don't have to separate these. I mean, you definitely want want to have there's my dydt. I made that a variable si. You know, the derivative of y with respect to t. And I'm just doing all these separately just to show that. There we go. So, the derivative, we know it's sign, so the derivative is cosine. And then this one, dx dt, derivative of x with respect to t. Divine is negative s. And then dydx is dydt over dxdt. And I just made these variables so that we can just see each step. This is another one. You could put all this in one code block. Um there we go. dydt dxdt and then this one and maybe display it at the very end. So there we go. And now I have that dxdt as this. Now cosine over s is co-angent. So this could have been negative co-angent and uh si does simplify trig values but certain ones it just seems to be resistant to. I don't know it's kind of funny. So um it won't put out co-angent. It'll just do this as cosine over s. And there we go. But now we have this that's stored as dydt dydx. And so I can put this you know import math. And I'm doing this to again compare some different things. Um here we go. I'm going to substitute for t. So now you know to get the slope. Um I'm going to substitute here um for t. And this is why I imported math. I want to show you these different answers. If I want that numerical answer, I'm going to use this math.pi12. I just decide to pick that v value pi over 12. Um and then I'm going to print out the slope. I'm using actually the same variable because I have this and I print it out and then I'm just going to overwrite it again. So substitute again, but this time notice I'm doing simp.py / 12 and then I'm going to output the slope. So we see the math.py, it's going to give me a numerical answer and sometimes that's what I'd rather have. and senpai will give the exact answer including radicals and however weird it gets that's the exact answer. So there we go. Just showing you a couple things along the way, but this is really, you know, um, parametric equations, you really could just right from the beginning, you could shorten this a little bit and have, you know, define your x and y, maybe a lot, you know, another import, and then maybe one line of code here to to graph it. And then you could combine all of these, you know, defi, get dydt, get dxdt, get dydx, output it once. All that is one block of code. So this could all simplify and get shorter. Um, I just wanted to show you, you know, some of these in between steps. And then there we go. Uh, you can have, uh, I think it might be nice to have both of these in. And I would comment out the one I don't want. But actually, you don't even need to do that. It'll do both. And you get the slope. So now we see you can set this up for any equation. I just picked s and cosine. I feel like that works out nicely. But you can do this for any two equations. Um parametric. Get the graph it, get the derivative, plug in a value, and you'll be all set. If you remember when we did implicit differentiation, so we still took, you know, x is my independent variable, y is a function of x and, you know, we took the derivative that way. But now we're going to say that everything is a function of time. So everything is going to be derivative related to time. And that's the related rates. Things are moving at different rates, but they are related. So if we take a look at one example of related rates, um if you have a ladder, so if I have a wall, there's the ground and I have a ladder and there's a person on the ladder um at the top and you know this person doesn't know yet, but the ladder's about to start slipping. So when I when the ladder's slipping like so I can define this value here from you know the wall outward I can call that x makes sense it's horizontal and this ladder let's say is sliding away from the wall at a certain rate then I can talk about my vertical distance from the ground up to the top of the ladder. So that is y makes sense. It's vertical. And when that's going away, this then is going to be sliding downward. Here's the interesting thing. They're not moving at the same rate. And you know, x is moving away, y is moving down, but then they're not move, but they are moving. And it's relating to time. You know, how many feet per second, meters per second? um are that you know are they moving you know whatever the rate but it's it's going to be something probably per second um and there we go that's you know the change in x related to time the change in y related to time so in setting this up what I can do is let's set up a an equation for the snapshot of the situation what do I have this vertical this horizontal nice right angle there. And I have my ladder. Definitely looks like a right triangle. So I'm going to do Pythagorean theorem. And I'm going to say x2 + y^2 = Now the ladder won't change. Yes, I know there's some ladders that collapse, but we're going to say that this ladder won't change. So I'm going to give it a value. I don't know. Let's say it's a 10ft ladder. That way um there we go. Pythagorean theorem. But that one that's a constant. So I'll know that. So the ladder is not changing. But you know this is the situation at any given point. x^2 + y^2= 10^2. Wherever the ladder is, it's going to fit into this equation. So now let's talk about the derivative of this because it's all changing. So the derivative will be the derivative of of x as a function of time is going to be I I'll write it down here 2x times dxdt you see because that's a function so I'm doing a I'm doing chain rule you know 2 * x one less power but then times the derivative of that function And the derivative of x related to time is dx dt. So that's that rel, you know, that's the derivative related to time. And then y is going to be the same plus 2y times dydt. And the derivative of this or any constant is zero. So that's the big thing with related rates. Can I get an equation for that situation and then take the derivative with everything re changing related to time and if there's anything I know that's a constant that helps because that derivative of that constant is zero because even if I took this as you know Z or something like that another variable and then I took the derivative there when I plugged it in as soon as I say that you know uh derivative of that and and times you know dz ddt then how much is that changing that's zero so it would zero out anyway because that rate would be rate of change for that would be zero so now I have this you know we draw out the situation sketch it out you know with this guy who doesn't know he's about to plummet um you know probably get hurt but you know don't try this at home but on a marker board that's okay so you know sketch out the situation which leads me to my equation take the derivative everything related to time and then I would know you know at any given point like maybe I know x and y or maybe I know um one of the values you know um and then you know how you know what uh how is each of these changing maybe I know that um So right now we see you know 1 2 3 four things that I don't know. If I would know three of them I can figure out the fourth. Um actually very often for something like this I might know either x or y you know one of the two and then I have to plug that in to to this original to get the other one. you know, maybe I want to know like, okay, I'm saying that, you know, the rates are different and they're changing as this slides away and he slides down. So maybe I want to know if, you know, if this is in in feet, then uh maybe I want to know, you know, what's what's his change in y, you know, how fast is he sliding down when he's like one foot off the ground? That might be interesting. um or you know maybe that you know or or when it the ladder is so far away you know these are some of the things so I would know maybe it's the y value that I'm interested in you know when y is one plug that in to get the x value now I have those two and I would then have to know one of the other two um different situations you'll see um you know textbook or real life that you know I would know how quickly one thing is changing. You know, maybe I know how quickly X is sliding away. You know, how quickly is that base that ladder sliding away from the from the wall? And then I can figure that out. So, um you know, so let's let's say this is let's say this is in meters and yes, that was a bit really big ladder. Um you know, 10 meters. So, you know, he's up there and, you know, worried because, you know, he he's starting to realize that he's in the math problem and he's about to start sliding down. So then I want to know let's say yeah how uh if I'm saying that this I would define this somehow that maybe I'm going to say that you know dxdt I'll put that over here that dx dt equals um one. So maybe I I'll just we'll we'll say that that how quickly is this sliding away one meter per second. we would already know that. So I want to know you know when y is one. So if I would do this so then I have two when y is one I want this is really what I want to find out dydt. So if dxdt is one so I don't have my x value here yet. So what do I what do I need to do? I need to plug this in. x2 + 1^2 = 100 and x2 = 99 and square<unk> of 99 you know it's like 9.9 or something you know close to 10 but not so you know if I put the square root of 99 uh times dxdt which is one and then we can solve for how quickly he is sliding down that ladder at that point. So all of these I can divide out a two everywhere and then I get I can you know divide out the two everywhere because that's zero and I can then sub you know that's times one I can subtract that. So it would be that dydt at that point equals<unk> of 99 not quite 10 m/s. So even if this is sliding away at a constant rate of 1 meter per second, this ladder is still sliding down slightly accelerating. um not like gravity because it's not a total freef fall, but at that point when you know this guy's one meter off the ground, he's going you know that fast. Um and uh you know I mean who knows it might be like really close to 9.8 uh meters per second. Um but again gravity would have been you know just a drop that would have been an acceleration and he would have been going a lot faster than that. He would have been going that fast if it was just dropping you would have been going that fast like right at the beginning but no just sliding down the wall while the other l are sliding away and see we can figure that out. So knowing the calculus, somebody else on the ground, you know, who doesn't know calculus might say, "Oh, that's only sliding away at 1 meter per second. That's not that fast. He's going to be fine." But then you do the calculus and say, "Oh, no. By the time he gets close to the ground, even uh you know, that's going to be going pretty he's going to be going pretty fast and that's going to hurt." So related rates, you know, um you know, save people. But that's you know this is the idea here that all these things are changing related to time. Um so you know draw out your situation sketch it out write the equation and then when you take the derivative everything is related to time and we'll see how that looks in the code because simp um it I feel like it doesn't look as uh organized as this. you have to like just be ready to decode the simp uh way of displaying it. But we'll we'll take a look at that so you can see um you know what this is and how you can plug in some values here. what this looks like and how you can plug in some values and we'll look at how to do you know this is a good straightforward related rates um because the equations Pythagorean theorem and we'll look at um we'll look at this we'll look at a couple other related rates examples um how we can set up the situation for things that are changing but at different rates but they're related to each other how we can take the derivative how we can do some substitution and what all this looks like in the code so let's take a look at the code so here's the example worked out in code and you'll see where you you need to have some calculus understanding. It'll display but um it's not going to do every single thing for you. And we'll use this example here. I'll just make it u 25. So our ladder would be five meters and then their squared is 25. So we are going through the imports. Notice I'm also importing symbols and derivative. We'll make use of that. And I want to use x, y, and t as our symbols. So I'm going to set up the expression. Um, remember I want it to be equal to zero. So I include everything. Just move it all over to one side. If I wanted to output the expression, I can make that the last line. Comment these out. See that? But this is the derivative here. So remember, I'm still thinking chain rule beyond the code. So I want that times the derivative here of x with respect to t. And then we're going to do the same thing expression derivative with respect to t here. And we see these different parts. So that first part got me the 2x and then the other part got me the dxdt. And notice it just displays it puts the x here, but it still takes it as one thing. And then that would be equal to zero here. 2x dxdt 2yd dydt equals zero. Here's another way to do it that I I kind of like better. That that one works fine, but I think this is a little bit better. And I'm using the same expression, same imports. So broken down here, the substitution works a little bit better. That's why I did it this way. So again, this XPR line here, uh, I definitely need this. There's my expression, but this line won't end up doing anything because it overwrites it in the next line. But there we go. Just breaking it down into dx, dy, and then I have dxdt, and then dydt as variables. So you see then putting it out this way the derivative is this. Um and again there we go. It should it outputs the same. I just like breaking it down this way. That might be a good useful way. Again you want to think about how this all fits together and the chain rule with respect to time. So I could just plug in you know I could just plug in something if I want to just substitute you know for dxdt. or then notice uh this dx up here I get the derivative so it's 2x and down here I can plug in all the unknowns look at this dr sububs and I'm plugging in all the unknowns uh notice the syntax here the square bracket and then each of the things I'm plugging in in parenthesis dx and then I'm going to plug in six for that for that dy I'm going to plug in eight for that and dxdt I'm going to plug in two. So you see I get this. So um notice because I had dx and dxdt. So you see that becomes six and that becomes two you know and so that that's why we get 12 here. So you see we we substitute for this whole thing dx this whole thing dxdt and it will multiply them as it as it is here. So then you have 12 and then with dy I plugged in eight but then I still have the dydt. Now I've plugged in these three things that I know. I still don't know dydt. And that's where I can solve. You see, I'm gonna set this up as an equation equal to zero and then solve for dydt and then I'm going to output the answer and see there we go. So then we get that -3 over2. So that is the dydt value of how fast is that ladder sliding down the wall. Notice I I even had like you know I plugged in for this whole dx. So I would have to think about what I'd want to do. I need an x value a yvalue and they have to work in Pythagorean theorem um for that ladder which is five meters. So 345 like that's just what I did here as an example. But if you're thinking about what you're plugging in, you're still plugging in dx the whole thing here. So it's not like some of the other ones where you're going to just plug in and just have it done. But if you have an idea and you're expecting what this is going to look like, then you you can set it up a lot like this and then substitute for different values. So, you know, that's that's really something to think about. All these related rates, there's a few key types. Um, but all of them it it is that uh similar thing, you know, derivative related to time. And you know, we'll do a couple examples. You can't do every single example, but hopefully you get enough of an idea of how to take this, you know, what am I doing here to create this derivative and then substitute in what I need. So hopefully this is a help and then we'll look at another example next. One other classic related rates problem is inflating a balloon because the volume going in usually I'll have something like you know I know the rate at which you know it's inflating but then what else is changing the radius is changing. We could also do this with like surface area of the balloon or something else. But um usually then we approximate it that a balloon even though I know it it it it's rarely a perfect sphere but we're going to say that it's a sphere for this. So I'll take the volume of a sphere 4/3 pi r cubed. And if we say that the balloon's a sphere then you know as it's inflating the volume is changing and the radius is changing. both related to time. So when I take when I take the derivative, it's related to time. Then I have dv dt. How quickly is the volume changing? And then when I do the derivative of this, so 4/3 pi, that's still a constant. So I'm just going to do power rule. 3 * all this becomes 4<unk> r^ 2 times the derivative of this which is an inner function of time. So times drd dt and this is what it's going to look like if I was going to write it down and what we'll see what it's going to look like as we do uh the python code simp. So we want to remember that chain rule notion so that sometimes if if you have to you know work that a little bit instead of just like plug something in and just you know what is simp output and take it you want to have a sense of what you're expecting the output to be so that when we look at how si displays these related rates problems then you know what's what and we'll look at this you know setting this up what's that derivative look like if I do this with senpai and then taking it from there, you know, plugging in values for really I just have three here, you know, knowing two, I'd figure out the third one. So, we'll look at how to do this. Um, this part then, you know, the code might be straightforward and notice how much is beyond the code that you need to do. Being able to set up the situation and say, okay, yeah, that's the appropriate model for this. Yes, it's a balloon. I know it's not a perfect sphere, but this will work. um so many of these related race problems that first part sketching that out the figuring out what equation sometimes that's even beyond the code that's just human creativity we need to take a look at what does this look like um you know we're we're not going to necessarily get to the point where I'm just going to type in a word problem and it's going to output some of that we you know we want to be able to figure this out what you know what equations appropriate what model is approp what sketch um you know what do I kind of expect the derivative to look like so that when I see it I recognize if it didn't you know if I miss something if I didn't do it correctly and we want we want the code to do something that help us to be a timesaver but we still also have to bring our own uh intellect into the situation. So let's take a look at see you know how to do this um just for this example as a sphere derivative plugin and we'll see what that looks like in simp. So let's take a look at the code and here's that volume example. Volume is 4/3 pi r cubed. So I'll just have this one block of code here that I'm going to import everything set up my symbols and just identify this expression that I want because it's really going to be helpful to see it. And yes, this one I mean I want that 4/3. I'll write it that way because you know that's really the exact it's going to put it out like this as the as the 1.3 repeating instead of the 4/3. Uh for our purposes that's going to be okay, but I mean not as elegant as I'd like. You want to see each step. You want to have a sense of what you're looking at because it it's not so much like click and set up once and it'll do everything. you really want to, you know, uh, be able to break this down here. Notice I'm going to break this down. So, the substitution in the next step works. I know I'm going to be taking the derivative of this. So, I'm setting up this variable dr as the main expression. So, I'm going to differentiate r. Great. That would work out. You know, I'll I'll see that and I'll take the derivative as r being a variable. Nice. But then remember we know that r is a function of time. So I have to have this and I'm going to put this as drdt its own variable the derivative of r with respect to t. And that's that's where again you you kind of have to have that uh chain rule awareness that okay I'm going to take this you know I really want to set up as this separately and then this and then my full derivative I'll put it all together. Notice I can just put this this times this. And when I display it, that's exactly what it's going to do. It becomes 4 pi r^ 2 times drdt. Simply displays it this way. I' I'd kind of rather it be the r also in the numerator. It it is what it is. But that is the one thing that drdt. So now I can see the original expression, the derivative, and now that separate I'm I'm going to use that separation to plug in a value for R. So if I just want to plug it in here, I'm going to create another variable here, dr plugin, dr.subs, and in this original dr, I'm going to plug in three for r. And then now I'm going to just create this other variable plugin which is notice dr plugin times drdt. I didn't change that drdt. I just plug this in for this part. If you take it and plug it in a step later, it's not going to display well and it might actually affect your answer too. So we really want to see like where am I substituting in and where do I still want to see that drdt. So I'm going to do this plugin. And now we see, yep, I plugged in r is three. So remember squar is 9 * 4 is 36. And that's where I get that 36 pi. Simpai leaves that in there. Times drdt. Now you see we really want to see, you know, you can still set this up for all these different situations and we can, you know, work through this. um you know we want we want the collab and we want the Python code to help us do the calculations but in you know we still need to have that like math awareness. So now I'm going to solve for drdt remember which I still kept as its own variable. So now remember the original dvdt is nine. So you see remember I've been just working on the right side here and then on the left side volume V the derivative is just dvdt there's nothing else going on on the left volume derivative is dvdt which I will make its own variable you know rather than the fraction looking in you know just the letters dvdt is nine so I'm going to take that as this is something I would have known you know now I have that value nine And you see what I'm doing here? Then set this equation plug in on one side which you created here. That's this. And then dvdt on the other side. I was set that's what I'm setting equal to each other as my equation here. And then I'm going to solve that equation for drdt. So there we go. So this is really the use of related rates. I want to, you know, take the derivative, plug in for the things that I do know, and then for what's left, I want to set up that equation and solve it. And that's what I get here. That if this is the volume, then you know this is DVDT, then the rate is just this tiny amount. also here when I plug in this r value you know that's the rate the change of drdt at that point so there's two things I can change the dvdt or I could also change this r value supposing I just make the r value one and so I have that so if I plugged in one I still have my derivative here supposing that's one oh now it's just 4 pi. And then let's say dvdt is still the same. I can plug that in. And you see it's a little bit more. And maybe I can plug in um you know if the r value is even higher. Um I'm even just going up to four. We'll notice that that's going to make a difference. 64. There we go. and even smaller than it was before because if my volume is changing as that radius gets bigger then it takes more volume to increase that radius. So we would expect the the volume change to be um you know that that drdt to be smaller and smaller. So you know that that's what we really want. We want to be able to set this up that you know what that expression is and be able to break it down. What do I expect to be this you know derivative in this case dr I made it and then drdt. So we can set this up and then do the substitution and then now you know this substitution here I can you know in this step here I can plug in for different r values. This is probably something for this example like you know inflating a balloon let's say that's probably the DVDT is probably something I would know. So you know there we go. If for some reason I knew everything else and wanted to solve for DVDT then that I wouldn't know. So I would have to um you know do another step of algebra and set up a slightly different equation. But this is usually for this one. This is usually what we want to know. So these are some of the things that we can set up um in our related rates problems. So hopefully it all comes together and you see how you can break it down um using some of your math intuition as well as uh making use of the Python code to solve for these unknown values. So the mean value theorem states that if I calculate the slope which would be the average slope if I did from like any two points on this line that somewhere in between there the actual slope will be that same average slope. There we go. So the mean value because the average slope the mean. So that's it. the um why this comes in calculus is because we're going to use calculus to prove it. But that's what it is. And if we take a look at this, so the idea is it had you know if I'll take these two points, whatever they are, I'm not even going to worry about putting numbers to them right now. But if I calculated that slope, then you know, I could draw the line and maybe I can match that up, but somewhere along there, that's one way to look at it. Um but the idea is it it has to get from here to here. So, it has to get to that slope. But of all the ups and downs, and it could be even more complicated than this, the slope somewhere in between at least one place will equal that average slope. And there we go. That's the mean value theorem. What we're going to look at is we're going to look at in code um how to use calculus to prove this. We'll we'll find that average slope. Then we'll take the derivative and match it up. you know, do I find a point where that derivative um the value of that derivative or you know, the slope at any given point equal that average slope? And we're going to see that yes, it works out every time. So, pretty cool. This might come in handy sometimes, you know, filling in the gaps of some other problem we're trying to figure out. But there we go, that calculating the average slope, the slope, the exact slope will equal that at least some one point in that interval. So let's take a look at how to do this and how to prove this with code. Here's a good way to test the mean value theorem using code. Remember it's at least one point on the curve will have a slope equal to the average slope. So I'm going to set up my normal in inputs. I'm going to pick our classic example here x squar because the slope changes. So any x values I pick x1 y1 x2 y2 then you see whatever x value and then I'm just going to automatically plug that in any points I pick I can then calculate the slope you know old school algebra there. Now that's the slope the average slope however the curve changes. So we're saying that the slope will exactly equal that at least once. So now that I have that slope from the two points, any two points I pick, I'm what I'm going to do, I'm going to take the derivative. So first of all, I'm going to take that derivative equation and solve it. Now notice the extra eq here because I'm going to take this and set the derivative equal to the slope. And then that's my equation. That derivative equal to the slope. and then solve. So then I'm going to solve for x at that point. That's my test, which is actually be my test x value. And then the slope, I'm going to take that take that derivative and substitute that. So test it. It could come up with multiple answers. So I definitely want the first one. And I'm going to substitute that in for x. So there we go. So now I have at that x value then the slope equals this. And there we go. Um I actually could just put it like since I'm doing this test zero, but it actually works this way. So I'll still put the test zero, but that's either way for this one it actually works. So any equation and then I can run it and at x= 12 the slopes are the same. So there we go. Um you know because and we can go through you know long way and plan it out. And what I can do is any other x value like let's say I just have like there we go. We'll just take this for whatever it gives us and zero. But maybe I'll make this I don't know five. Any equation I can come up with. Then automatically calculate the slope at any at any of these two points. Take the derivative. Compare the slopes. And this is the x value. What if I try this capital N because that'll give it a nice numeric answer. And I'll do the same thing over here. There we go. So this actually should work to be the exact same. Let's just give it a numeric answer. You see at this exact value the slopes are the same. So you see this is just a way to test this and the mean value theorem. It is really interesting because sometimes you you know you might need to use that assumption to solve something else. So there we go. And you know you you can try this with any examples and it should come up to be again continually proving the mean value theorem. But then we'll look at Rolls theorem as a specific example of of this mean value theorem. Rolls theorem is a specific subset of the mean value theorem. And rolls theorem says that if I have two points that the you know this that have the same y-val so the slope between these two points is zero. And let's pretend that my drawing is good enough that that's exactly it. So if the if the slope between these two points is zero then the graph would hit zero the slope of the graph would be zero at ex at least one point in between. It could be an actual straight line and it's zero the whole time or it could be that it turns around. And that one is probably easier to even think about even without calculus that you know if if you get back to the same y value then if you went down you have to come up or if you went up you had to go down and then therefore somewhere in there in that turnaround the slope's got to be zero. But that's what it is. It's a specific version of the mean value theorem. If the slope is zero between these two points, then somewhere in between the exact slope of that line must be zero. Even if I have a line that curves up and down a lot more, but that's it. And that's rolls theorem. So unique enough that you know you got a theorem named after him, you know, definitely when when you get a theorem named after you, you know, you've arrived in life. Um but nonetheless, so let's take a look at this at just an example of this in code and how to uh prove this and find this uh when writing the Python code. So let's take a look at the code. Remember rolls theorem is a unique version of the mean value theorem. So instead of saying you know what's the slope over this range we're saying that if rolls theorem is if the y values are the same at the beginning and the end of the range then the slope must be zero somewhere in there. doesn't have to be zero the whole time like you know a flat line. The slope could change. The graph could go up and down but somewhere if if the y values are the same at the beginning and the end that the slope must have been zero somewhere. And that's you know a version of the mean value theorems. So we're going to use all the same thing x squar and here's what I'm going to do. We know that that's nice and symmetrical. So I'm going to pick four as my first x value and then neg -4 as my other one. the y-v values should be the same there with that symmetry. So, we're just going to print that out just to show for rolls theorem that the y values would be the same. And then we're going to continue. We're going to calculate the slope. Um, and then if I have these two points, the slope will be then the slope will be zero. And I'll take the derivative. And we're saying given that derivative, does the slope equal zero somewhere? And that's what we're going to test. So we see that the y values are the same. And then the slope at x equals 0, that's where it'll happen. Then the slopes are the same. Check. I could even do a modified version of this. Let's let's shift it. You know, instead of x, let's do like x minus 3 squared. So to make it symmetrical, maybe if I make the first x value 10. So that'll be 3 plus because remember this shifts it to the right. So that' be 3 + 7 and then 3 minus 7. Uh that'll be neg4. We'll keep that. So that's all we're doing shifting it. And we'll run this again. And we see that for those two values, yep, y value is the same. and at x equals 3 right in the middle the slope would also be zero. So that's it. It's just testing that theorem showing that you know I might need this information for solving something and that's what we have rules theorem. So how can I figure out the area under a curve? Well, Bernard Reemon wasn't the first to ask that question, but he's the one that came up with a really great solution. So, what can I do? I have a curve, but maybe I could approximate it with rectangles. And if I drew a bunch of rectangles, I get, you know, reasonably close an an example that's simple enough yet complicated enough to illustrate this. If I put rectangles here, I could figure out the area of each rectangle, and that'd be a good approximation. So, first of all, we notice that if I pick this, there's a bunch of gaps here. If I do these rectangles, I'm missing a lot of area. And what would make that a better approximation? If I had more rectangles. So what if I had, you know, twice as many rectangles? So we see, and yes, my rectangles aren't perfectly straight, but that's okay because we're it illustrates the point also. It's an approximation. And with more rectangles, we see we have fewer gaps, smaller gaps, gets a better approximation. And so if I had more and more rectangles, I could get really close. And this is exactly what's happening with um highdeinition video or, you know, from whatever the resolution of your computer screen is. You know, it's all just pixels, but I get a better resolution if I have more pixels and they're smaller. You know, you look at an old video game, it's very pixelated because the pixels, there were only so many and they were bigger pixels and we really see these angles and the boxes. If I get, you know, millions and millions of pixels and they're really tiny, then I get a nice curve. So, that's what we really have. Like, if I have more and more rectangles and they're each smaller, I can get a better and better approximation. And this is really what he looked at. Um, and so what's the area of this? Let's just say, you know, however many rectangles I have, I'm going to define the width of each rectangle. So each rectangle, and since this is my x value, the width of each rectangle is going to be some like delta x, what's my change in x? And then the height given my equation is going to be my y value every time. So I can cycle through you know if I've defined this curve as y= x^2 I can cycle through you know pick my delta x define that ahead of time and then go through and for each of these different x values you know um same delta x but given that x value what's the y value at that point calculate the area of that rectangle and add them up and that's called a remon sum after Bernard remon. So what I can do is I can use this to approximate the area under the curve and notice I used the left end points here. You see if I pick this x value that went up to the curve and then I went over. So you see each x value that went up to the curve and then over. So I call the I I can use my left end points here. Um in which case if I a left end point I might have a value back here that would be zero. And then this value here I'm not going to count because I'm only you know for this rectangle. You see the height of this last rectangle that left end point that's the height I'm going to use you know there we go. and then um that x value and wherever that would go we're not going to worry about that. So we can approximate with the left end points in this case and because of this curve that gives us an underestimate I can approximate them I can use the right end points instead. So if I use the right end points as we see I could have you know this is the height of my rectangle here you see so it's that zero that I'm skipping but this x value defines the height of the rectangle going over and in that case you know if I go up to three then that's going to define the height of the rectangle and we see if I use the right end points then my rectangles go over the line and I have all this extra area. So I could do that that way. That's another way. And I would have an overestimate. And as you can imagine, I could probably do the left end points and I could do the right end points and get some average and that would probably be a really close estimate. But that's the idea that the these approximations and they become remon sums after Bernard Remon. These approximations give us a way to estimate the area. And what we're going to look at is in the code then you know if I were to do this by hand you know I a better approximation would be a whole lot of rectangles. So that would take a long time. But we're going to look at if I have this function and I've defi and once I define my x value I can set up a loop to you know however you know what am I going to define as my dx delta x my width and that's the width of the rectangle and therefore how many you know what's my range what's my width of the rectangle how many rectangles and I can loop through this and add them up and that gives us a way to do a lot of rectangles and get a better and better approximation and then we're going to connect this with calculus and see how we can get like the exact answer. So let's take a look at how we can use these remon sums approximating the area with rectangles to get better and better approximations and we'll then connect this with calculus. So let's take a look at how to do all this in the code here. When we write the code it's going to be a lot better. We can set any number of rectangles we want. We can set dx to be as tiny as we want and we don't have to you know think about oh I have to draw this or I have to do these tedious calculations. We can really see see this and I just put in the comments here. We're using this example here xy= x^2 from x= 0 to x= 3. So let's just set this dx to 0.1. And you know I'll set my starting x0 ending x3. And so I'll even just put this here that we can set this because then I can determine how many rectangles do I want. End x minus start x. You know what's that range divided by dx. And I need this to be an integer. So, I'm going to do all that and then cast it as an integer because then I want to know the number of rectangles and I, you know, I can't have half a rectangle. That that doesn't work. So, now my sum that I'll take that ve that variable set it equal to zero because now what are we doing? We're looping through that um for a and range rectangles. So, what's what's my x value here? I'm going to loop through these x values. So my starting x plus whatever that dx is times a. And remember the range, you know, if I don't give it another argument, it's going to start at zero. And then it will not do the last number. So if there are 30 rectangles, it'll start at zero and a will be 29 and then it'll stop. And there's my x value. Um, and then my y-value is take that, you know, x squar. So now I have that y value and then the area is for each rectangle that y value times dx. So once I get that area, then I'm going to get my cumulative sum here. Sum plus that area because that's what we want. We want to get the you know we're going through this how many rectangles and you know given dx here so all those x values plug it in to get y so dx doesn't change but that y value changes based on this so this is where I'd put my equation right here in the loop and I have you know the area of each rectangle add it all up. So, if I do this, you know, there we go. 8.5555. All right, not bad. But what happens if I make each rectangle even smaller? Look at this. dx is 01. All the rest of this can stay the same. And look at this. I get 8.955 because I have less empty space. Now remember this is going to be the left end point because my range will start at zero. My range here was from as x goes from 0 to three. My range here will start at zero and then I'll go through this here. But what if I wanted to do my right end point? I don't want to start at zero but I do want that 30th. So my range here I can change that from zero or not zero because that would be the default from one to the number of rectangles plus one. So that way if I didn't have this it would by default start at zero. If there were 30 rectangles then it would do uh you know up to 29 and then stop here 100th. So there's gonna be 300 rectangles and it'll go from one to uh it it won't do zero but it'll rectangle number one and it will do rectangle 300. And if I do this look at that it becomes you know if I go to my right end points because then my rectangles go a little bit over the graph for this y=x^2. So you know we see this. So we have the left end point which will end up being a an underestimate the right end end point which makes an overestimate but either way I can just change dx here um you know in this case I'm keeping the same x squ but I can you know change all these and I can go to my left end points or my right end points and you know see these estimates. Um, I could probably get really close. Like, let's say if I get 001, everything else stays the same. Now, my dx is really tiny. Look at this. So, I get from my right end points 9.0045. And that should be an overestimate. Now, if I go back to doing the left end point, there we go. 8.9955 and we see, you know, we can imagine here that the true area might be exactly nine. Kind of nice how it works out, but then we see the overestimate and the underestimate. And and that's the whole thing with remon sums. it really is the origin of integral calculus. But also, you know, sometimes you might have a situation where you want want to do these um definitely if we're talking about actual pixels or something um or things where I have to cut things that are not on a curve. This might be some, you know, some way that I have to do this. But certainly we can see um how this builds builds up to calculus getting these number of rectangles getting to be more and you know dx being tinier and tinier. So I get more and more rectangles and we should see these values approach a certain number here. So let's take a look at how how this leads into that exact number and doing the calculus. Now that we see that we can get the area under the curve with all these rectangles, let's look at the math notation for this and see how it connects to calculus. So this is the true remon sum. I just drew one rectangle here, but for any of these rectangles, I'm going to define that delta x as that same constant width each time. So the width of the rectangle is delta x and then the height is y and so you know that's the area of one rectangle y * delta x and then I want you know however many rectangles in you know wherever I define my uh my range here you know we were saying 1 to three but wherever I define it I can for that range given that delta x how many rectangles do I have so the notation for that we can go through these iterations ations um you know mathematical iterations existed before we were writing code and doing loops um but the mathematical iteration maybe I'll say you know for each subn and that's why I put the parenthesis there so that distinguishes it so this is like the first rectangle so what I'm going to do is I'm going to bring in sigma for summation and our summation notation is saying add up each rectangle angle. So, I probably don't need the end there because that's the same. But anyway, you know, for each rectangle, I'm going to add up um calculate the area and then add them up as we go through. As n goes from one to I mean, if I make this a huge number, the greater the number, the better it gets. So, let's say as n goes from one to infinity. So there we go. Great. You know, infinite number of rectangles because now beyond what I can draw, if I have an infinite number of rectangles, that's going to really approximate it pretty well. And you know, as we see with the code, we could, you know, make thousands of rectangles. That's like a even huge number, but it's a manageable number. It's a finite number, and we can get, you know, a pretty good approximation. But if I say this is you know the as the number of rectangles goes from one to infinity and sum them up that really should be the exact area and that's the remon sum there you know taking this to this you know infinite number of rectangles. So the connecting that with calculus, this summation evolved into this symbol here. And it really the idea was that it was an elongated S for summation, but also it very much looks like the uh holes in the side of a violin. um you know they they appreciated that that look but you know that's the notation that evolved this um integral integral symbol and so the what we'll say is the integral of and in this case I'll I'll make it specific for this one of x squared and then the delta x the notation for that evolved to dx d for delta And in this case as my definite integral like let's say as our example let's say we went from 0 to 3. This is all not drawn to scale. Um I'd put them here as x goes from 0 to 3. So this becomes the notation here for the integral. So this is the area under the curve x2 as x goes from 0 to 3. And that really is that infinite number of rectangles to approximate it. So let's take a look at in the code how we can get our definite integrals and we can write this with a line of code that we'll say okay now once I've defined my function let's take the integral from here to here and what's the exact area and we'll look at that we'll actually even compare that to uh the remon sums so let's take a look at how to do this in the code now that we have that notion of area under the curve kind of that rectangle method and the no number of rectangles approaching infinity that's great how do we solve that simp comes in handy again si integrate that equation and you know import and I have all these symbols x y and c we'll we'll talk about c later I just want to show you this so you see if my equation x^2 then the integral is x3r third power over 3. And every equation is going to have an integral function associated with it. And we'll look at how to um recognize these patterns and everything in a little bit. So rather than you know trying to approximate it with the rectangles like we were doing earlier, I can take this integral get that equation and my definite integral I'm going to substitute. So notice I have this and I still wanted to to just display this integral but now I'm going to go through oh my lower value is zero upper is three just you know we'll continue with that same example because I'll call this variable integral definite and what am I doing here simpint integrate so you see if I do this it will give me the equation if I do this with other arguments in here integrate that equation and and plugging in for x my lower bound and my upper bound. So it's the area under the curve as x goes from here to here and you see it does turn out to be exactly nine. So our estimates up here you know 8.99 or 9.05 or you know they were dancing around it but then we get the definite integral here. So you we see now we get that area under the curve from one place to another. So we can just change this to any different values here. I can have my equation you know anything I can have I can do you know simpi dot sine of x and I don't need to also display it so if I just do that yep the integral of sign is negative cosine and I can have a definite integral I'll you know that sine of x from 0 to who knows maybe yeah we can just keep 0 to three it has that curve that'll be you know something and you know what's the area it's actually you see it wants to give it that exact answer 1 minus the cosine of three is exactly the answer so we could have any value here simp log and remember log me you know and si is the natural log and the integral so some of these integrals you might actually commit to memory and some of them you might say you know what I just want to see what this ends up being and applying that as a definite integral we can just say oh you know 0 to three and simpi gives us this exact answer so we can have this simple what's that equation look like and then plug in those values you know from where to where like because I want that area under the curve as x goes from here to here And this is really then the exact way to get the integral from you know getting that equation which is the integral equation and then plugging in these values. If if I was doing this not on the calculator what it would do is I would get that integral and I would plug in the upper value. So it would be three here 3 * the log of 3 minus 3 and get that value. And then I'd subtract I'd plug in zero and subtract it. 0 time the log of 0 minus 0. So it would be that other answer minus 0. And that you know that's what I would have 3 log of 3 minus 3 because then the other one would be minus 0. This is where we're going to really get into calculating the area under all sorts of curves. Now, this brings us to the fundamental theorem of calculus. We've been talking about integrals. We've been talking about derivatives. And you might think, how can the area under the curve be related to the slope? You know, integrals and derivatives. So, let's take a look at this. When we were talking about the slope from two points, what do we have from here to here? we have dy and then over we have dx d for delta change. So the slope is dy over dx. Then we were talking about the area under the curve and that width is dx. And since it goes from zero up to there, it's not just y, it's dy cuz you know the difference from that to zero. So then we have dy here. And then so what do I have for any individual rectangle? I have the area equals dy * dx. And we see from dividing to multiplying two sides of the same coin. I multiply, I divide. I go from slope to area. And then I see the connection of integrals and derivatives as being two sides of the same process. And in fact, so we had Newton come came up with derivatives many years before Reman came up with integrals and it was somebody else after that that saw that hey these are two sides of the same process. It really takes a lot of different perspectives to put it all together and that's what we want you know we want different perspectives and everybody sees something and contributes something. So we see that this fundamental theorem of calculus is that these are connected as opposite processes. So when I look at the integral and I look at the derivative I can take from whatever function I have take the derivative and then from that new function I can take the integral back to the original and that's what we have. I can take the derivative of something and then the integral of that goes back to the original. really interesting. Um, and it connects it all. So that sometimes maybe I'm looking at applications with derivatives, but I have some missing piece of the puzzle. I can do the integral to find something or I'm doing integrals. I can do the derivative to find something. And then when we look at when we get into all of our just we looked at the derivative formulas. When we look at integral formulas, we'll see the ones that match up and oh the integral of this matches matches up with the derivative of something else. So let's take a look at just some examples of this in the code and how we can demonstrate this and you know um take the derivative and then take the integral and see how it gets back to the same same thing. So that's the fundamental theorem of calculus, that connection between integrals and derivatives as two sides of the same process like multiplying and dividing. So let's take a look at this in the code and we'll tinker with some things and you know see how we can make these connections. We're really just going to go through a few examples here and um just you know show you how this you could take the derivative, you can take the integral and they get you back to the same thing. So the first thing is notice I'm throwing in another variable there C and I'm just going to take just show the equation there X squ and notice we didn't use C yet and C is going to come in in this next step where we do the integral because remember now thinking about how this is the derivative in reverse. you do the integral here, but then there could have been another constant that when you do the derivative um would have dropped out. So when I start with this and I do the integral then it's really x3 plus over 3 plus c and simp puts the plus c in the beginning. I usually put it at the end. um that also you know in in your notation remember the plus C and also then that reminds you hey that's the result I just did an integral so now that is a variable and what I'm going to do is I'm just going to substitute any C value as long as I have a number here uh because if I take the integral so what what I'm doing here is um so if I just take this integral and substitute C and then I'm going to take the derivative of that and it gets us back to X^2. So we see X^2 I did the integral and then I did the derivative of that it got me back to X^2. And so notice we can just go back and forth here. So, I'm going to take this derivative and I'm going to take the integral of that plus C and it's going to get me back to x^2 plus c. And notice I did this all in, you know, just changing this equation here. So, I could do like si sign. There we go. And we can do there's sign. And if I do the integral, then it becomes the d The integral of sign is negative cosine. And so again, put the C first, but then plug in any C value and I do the derivative again, gets me back to sign. And I do the integral again, it gets me back to. So just the idea that we can go back and forth. That's really what this is just showing you that, you know, you can always do the integral, then the derivative. That's really if they're two sides of the same coin. And thinking about that, I do need to put the plus C when I do the integral because that's part of the answer, but I could get to knowing some initial conditions, get to an act an exact value for that C. So that does exist. It could be zero, but it's not always zero. So we have to put that and account for that C. That will drop out if I did the derivative, but uh we need to include that. So there you go. fundamental theorem of calculus. Integrals and derivatives being two sides of the same process. Let's say I wanted to do the integral, get the area under the curve for a graph that goes above and below the x-axis. Well, we need to see and think about like what our applications are because the area above the x-axis will be positive, but then the area below the x-axis will be negative. And remember that you know dy was the same as the y-value because it's the change from zero. So then if it goes down below then the change is negative. So sometimes I do want that. Um maybe if I want displacement um you know if I have a a velocity graph let's say the area under the curve is total distance you know the integral of velocity. So maybe I want that distance from a point. So, you know, positive distance in one direction, but then negative distance in the other direction. Does it get back to where it started? Maybe that's maybe that's the answer that I want. But maybe I do want just the total area under the curve. Um, so in that case, I need to find a way. I need to get this area and then get this area and then add them. But I need to identify where these zeros are. So I can take the area from here to here and then the area from here to here and do absolute value. I can do absolute value there too. That won't change it. But I do need to do it here. Get that absolute value. Get that absolute value and add them together because this one as an example, you know, like a sine wave. Um so if I did the the area under this if this was y= s of x from 0 to 2 pi the answer is going to be zero because this area as positive will completely cancel out that negative area and maybe again maybe that's what I want but if I don't then I have to identify where are these zeros and then integral from here to here integral from here to here absolute value and then add those up and so we'll look at this in the code how I and find a nice way to identify the zeros and then use those answers to set up my integral from different ranges and then add that all up together. So, you know, sometimes this could potentially be a laborious process, but we're going to see how we can set up code to do that. And for applying this to other situations where I have graphs going below the the x-axis, I can just change a couple things and then use the same code. So let's take a look at how we can do this in the code. Identifying these zeros and then getting the area to make it a positive area even if the graph goes above and below the x-axis. So with the code, finding the area above and below the x-axis becomes straightforward because you can set this all up ahead of time for a lot of different situations. Now, first we we do want to plot it. So I'm going to go through all these imports here in this code block. And yes, I'm going to include C if I need it. So I you know, I just keep these standard imports. And I want to graph it. So I'm just going to pick this one sine of x because that gives us area above and below. And let let's plot this. And so we see the plot. I could if I wanted to limit the x value here and just like focus on one part, but I'm going to keep all this. That's fine. And we see the zeros 0 0. And even though it doesn't show it exactly here, we know that that's going to be um pi. And then over here it's going to be two pi. So you know that's one complete cycle. And since it's has that um that symmetry to it, then if I did the area just a normal integral from 0 to 2 pi, the area would be zero because all this positive area would cancel out the negative area because remember all the integral gets the area from wherever this curve is down to the x-axis and then it' be negative below the x-axis. So these would exactly cancel and the area would be zero. Maybe there's situations where I want to see that overall displacement and cancel that out if necessary, but not here. So, we plot it. We get the view of this because then I'm going to follow this up with finding the zeros. And it really is just simp.solve. If I don't put anything else, it will default make that equal to zero. And we see it gives me the zero zero and then pi. But if I really want this whole thing, I want there's my zero, there's pi, but I actually want two pi also. And looking at this graph and knowing that then that helps me in the next step because really all of this we want to use the code to help us but it's not going to just be like blindly doing anything doing whatever we want and I don't have to think about it. We want to make sure that it does what I want it to do and includes all the values. So I have this 0 pi but I want it to go to 2 pi also. So I'm going to get the area with the zeros. I'm gonna identify my lower value as zero and my upper value as 2 pi. Now, just because I know that that's the lower and upper my overall range. But remember, if that first zero was zero, um I'm just going to keep this in here. And notice that doesn't it's going to that's going to zero out. And I kept that here in the print statement at the end, but that will be just zero. Um you know, I'll do simp.inttegrate integrate and then that equation as x goes from lower to my first zero and so it really goes from 0 to zero there's no area that variable one that's going to be zero and then my next one see I just want to like identify these I call the variables one two and three for my three different parts of this um and then I made this the absolute value that way you don't have to overthink if like which part is above which part's below and then make the other one negative. Um I just made an absolute value for all of them and that that way that works. It doesn't have to you don't have to change this for different ways that the graph other graphs might curve whatever equations you use. So the next one integrate that equ zero from the first zero to the second one. So I'm going to get that area. And then the third one I'm going to do from that zero is one all the way to the upper which I identified here. So that way we really see that you know this one has that extra one that's you know nothing but then I have that first area and then the second area. I did absolute value on both. So if I were to graph let's say you know instead of sine of x maybe the negative s of x which would flip it I could keep the same code here and I don't have to change it which one's above which one's below. So now I have these and really I have the total area and then is 1 plus 2 plus 3. And I just wanted to print this out. So really that's kind of the advantage of what you want to do with codeab. You want to see the code so you can double check it. You want to see the inputs of the different parts. We're really making use of this for all, you know, math knowledge for all the different in between steps. So, we get the area is four because that first one I did was zero, then two, and then two. And you see, if I didn't do the absolute value, they'd be -2, and then that would cancel out. So, there we go. This is all set up then for whatever other equation I want to do. I want to plot it. I want to find the zeros. And then I can identify, you know, upper and lower and take it from here. This could be all set for most any other thing. Um, you know, like I said, I have this set up for two zeros within the range. So I can have my lower, my upper, and even if you know, I had other zeros in there, that still works for this. This code still works. And there we go. So if I want the area between curves, I can remember every area area it goes from that curve down to the x-axis. So I can just use subtraction. I can identify this top curve and then the integral gets me that area and then the integral of the bottom curve gets me that area and then I can subtract them. So one of the things I want to do is identify which curve is the top curve and then I also want to identify a lot of times we wanted to go you know just the area between where do they where do they cross so I want to identify these two points and then I can set up my integral and so these are some of the things that you know I might use on a calculator I might graph something and we're going to look at in the code just how to do this how to get to this point. Set up these two. Identify the points where these two curves intersect. Use those as the bounds. And then do the integral of the first curve from here to here minus the integral of this bottom curve from here to here. And then I get the area in between. So that's what we'll look at. And we'll look at how to set up code to use this for whatever curves may come up. You know, how to identify these two points of intersection and then calculate the area between the curves. So, let's take a look at the code. We're really just going to add another step here to the other code. Finding area imports. Um, I'm also going to import EQ here. And we'll see later on why that might help. Now I have two equations that I want to graph. I'm just going to pick this as an example here. You know y=x^2 and y equ= x. So remember you know the understood would be that each of these would be equal to y. And so if I plot these together then it looks like this. Now this is the default simp plot. Fine it helps. But actually I am really looking at this tiny area here that you can't even see because of this plot. So one of the things you can do is when you plot this you can limit or show a range for x values or yv values. So if I put in here one more argument. So after I'm plotting these two comma and then in parentheses here I'm putting in my x value comma and it goes from negative.5 to positive 1.5. So, I'm going to give that x value um limit here. And you see what it does. Then it gives my graph from negative.5 up to 1.5 much more zoom. And then I can really see this little area that I' that I'm talking about here. So, my default I would go to senpai.plot and just plot these two. But just remember that you can do this. You can limit this. And that's kind of how you would do the zoom. And that that works out nicely. That There we go. Now I want the area between these two curves. And I'm I'm going to see that y equals x is the top curve and and x squ is the bottom curve because from 0 to one it does curve around. And then after that um x^2 is you know greater and it keeps getting greater. So I want to see these and I want to see that it goes from zero to one. But let's say I didn't see this exactly from 0 to one. You know where where do these cross? You know that's these points where these two lines intersect. I can find the intersection points. So you see my x points. I'm going to solve you know set these equal to each other. What x values make that true? Now this is interesting because I see that there are two points 0 0 um and that's pretty easy to see. And then this confirms it. You know, the x value was v 0, but what's this other x value? I happen to know that it's one. And it kind of, you can see it looks like it lines up there, but it didn't show up here. So, how do I know? This is another interesting trick. So, this works usually, you know, it'll give you answers, you know, solve. Just set these equal to each other. Um, or if I just had one of them, it default sets it equal to zero. But within this and you don't need to make this another separate step. This is where I can put EQ and I'm going to put another parenthesis here. So what I'm doing here is that I'm taking these two things but then the simp function eq making that an equation which should it you know it would have done all anyway and thus solved and gotten that answer but this works sometimes to give me both answers and for you know some like behind the scenes reasons um when I solve it you know I know that there's two answers. It only gave me one, but if I throw in this other step here, it works and it gives me these two answers. So there we go. So that's that other layer of officially making it an equation with EQ. So now I have these two points. That's the X values where they intersect. So remember that that we get the area under the curve down to the X-axis. So to get this this area between I can get the area under y= x minus the area under x^2 and then the difference is that area in between. So that's what I want to do. I want to get you know the integral of one the integral of the you know top bottom and then um subtract them. Now it happens to be that I called equation one x^2 and 2 x. So actually equation two is my top equation. But I want to show you that um even if you wanted to do it, you know, whichever way this works because when I do this integral simply to integrate equation one from you know as x goes from this to this lower to upper and you see I'll define because I want to use these for both my lower bound is my first zero x.0 and my upper bound is my next one x points one. So that's going from lower to upper. Um, and I have it written this way. That way, if I'm doing something else, you know, we want code that we can use for a couple things. If I'm doing something else and I want to just put in a lower and upper limit, you know, maybe it's not related to the zeros, I can do that. So there we go. My lower bound, my upper bound, and then the other one, I'm going to do the same thing. Integrate equation two from the lower bound to the upper bound. And then what I'm going to do that's top and bottom and I'm going to subtract them. Now I happen to look at the graph and I know that the way I have it written here I have equation one and equation two but really the top equation should be equation two. I absolutely could just change this and I could make this equation two and switch this one to equation one. But whether I do that or not, um that won't matter because when I it that will still give me the correct number. It's just that the area might be positive or negative depending. Now that I know that this is the case, I could just eliminate this and I could just do this here because now I know that it's going to give me a positive area. There we go. One sixth. I could um have just kept that as I did here. the absolute value and then supposing I get it wrong or whichever one I define then it won't matter because I'll just do that subtraction and it may be negative but I'm just doing the absolute value anyway. So you see it you know it works out either way. Um, and you know that's kind of, you know, you want to build these in here for ways that you can look, you know, I can always look back and change this or I can use absolute value. But either way, then I get that, you know, subtracting those two areas and the difference is 16. So then that's the area between the two curves. What happens if I want to get the volume of a three-dimensional shape that I revolve around? So, let's say I have our y= x^2, you know, nice curve to it. But supposing I wanted to revolve this around, and we'll see why I have this random shape here in a second. Supposing I wanted to take this and revolve this around the x axis here so that I make this like you know 3D shape you know maybe like a pointed hat on its side or something like that as as an example supposing I wanted to make this 3D shape but then get the volume of it. How can I get the volume of this weird shape? Now definitely we can be like Archimedes and put it in a tub of water and see the volume change. But mathematically if I'm looking at this if I have this if if I have a shape where I do have an equation that gives me the bound of the edge of that. So when I want to revolve this around what can I do? Now let's think about the rectangles when we got the area under the curve. And if I wanted to do this, supposing I slice this here. I'm not going to draw all the slices, but supposing I slice this. So instead of rectangles, I have slices that are really thin cylinders. So that's why I drew the cylinder there. If I have a really thin cylinder, each of these slices here. And what we see is then if I slice this this way, just like we have the rectangles, if I have more slices, they're each thinner. So I will define, you know, whatever my slice is, I'll define, you know, that common dx. And never mind that my bad drawing doesn't have a common dx. And certainly our slices would be thin beyond what any of us could draw, but we'll get to that. So then you know that's the height here. There's my dx and it's that constant height of the cylinder. So what's the volume of each of these cylinders? So the volume is pi r 2 height. That's the volume of any cylinder. So if I have my common dx as that height in these cylinders on the side, what about the radius here? So that radius there, if I'm revolving this around, then my radius is always going to be my y value. And so what I have here is the sum of pi * my y value squared time dx. or I could call it delta x, but I think we've gotten to the point now that we're looking at this as dx, d for delta. So if I revolve this around and then you know this is it and the sum of each of these cylinders. So remember we can just give them you know like uh I for iteration or something and then as I goes from 1 to infinity so I'm summing up this infinite number of cylinders as I goes from 1 to infinity and remember then you know now you know we've we've done this we could see that yes the summation of these infinite number cylinders is the integral of my y value times dx and in this case my y value is going to be x squared. So if I call this you know x^2 dx and let's just say you know I start from like zero I don't let's just say I I decide to cap it off at three here then it would be you know from 0 to 3 there's my definite integral. So we see how just picturing this slicing this you know the same way we were you know we get that calculus logic of yes I can break this down but can I you know these slices you know a few slices it might be crude might be choppy then I get more and more infinite slices then we start talking calculus you know each of these we can get a handle on it oh it's a very thin cylinder so then that would be the volume of each cylinder and then I add them up which then infinite number becomes calculus. So sometimes these can get really you know um if I were doing this by hand really um intense factoring and uh you know some more complicated things but we'll look at how to set this up in the code that since it's this you know if I just set this up that I can define what is that radius in this case my y value and you know what's my range lower bound to upper bound and work this out and uh calculate it. We'll also in the code I'm also going to introduce a little bit of 3D graphing. Python can generate this shape and you can really get a sense in three dimensions. Hey, what would this look like? And we can use that you know for other more complicated graphs. So let's take a look at this. So we get the the logic of this you know equation to define the edge of the curve because then that y value becomes my radius. So then summing you know make these cylinders sum them up um and look we'll look at the code we'll look at how we can just you know show this what what this would look like in three dimensions and then set it up that all we have to do is change the equation and the upper and lower bounds we can get the volume here. So let's take a look at how to do this in the code before we even get into the calculus. Let's talk about how to visualize this in three dimensions. And I'm going to show you how to set this up that we can use this for any other 3D, you know, uh, function revolved around an axis and all the rest of this can stay stable and we can just change the function. So we're going to import numpy as np import mplot library as plt things we were using more in the algebra stuff sometimes in the pre-calc stuff and here we've been doing a lot of simpl plots but this can do more including the 3D I like the way that this works for 3D so that's why we're going to use this library then I'm going to import these toolkits so that I can see the stuff in 3D I'm going to set my range here for X lower bound upper bound and then that way I can use these V variables and I don't have to change this everywhere. And all this stuff we're going to keep the same here. All the all this to set up the graph. I'm only going to plot one and I'm going to put it in row one, column one. So there we go. But then here we throw in this this projection 3D. I have my points. you know that difference whatever I pick as my lower bound lower minus upper upper minus lower times 10 that should give me enough XYZ coordinate points to plot so this should work out nicely so you know here's you know define it once here's the points and then I'm going to set this up um U and then V so lower minus upper points and then V u is really what we're going to use here but Remember, I'm revolving this around. So, this lens space, if you notice the trig in here from 0 to 2 pi and then 60 points that again, that should be enough to give us a nice smooth curve going around in a circle. I'm going to set this up. Notice the capital letters here for the mesh grid from U and V. And this is where it connects that capital X equals U. So what I'm going to do here is I'm going to define my function and this is where you're going to change this like this going forward for other functions. So really this is going to be y= x^2 is my function. So that's going to be u^2 capital u ^2 that's my function and can't spell function without fun. So then I'm going to have and I have these as one y1 and z1 because I'm building this in there's we can build upon this to plot more than one. Uh we'll we'll get to that later but we can plot more than one. So my function here and now notice this function is going to be you square. There's my function and then in 3D. So then the y value is that function times np.c cosine of v and then the zval is np do sine of v. So remember with our parametric equations s and cosine you know as my x and y values that'll give me a complete circle. So I'm going to take this and this in three dimensions is what plots it going around in a circle revolving around that axis. So now I have that equation. See, I can define it here and then just use this here. So I don't have to ever change these two. And that will plot this going around that axis. And then here I'm going to say that I'm going to plot the surface. And what am I plotting? I'm plotting my x value y1 z1. And I'm going to pick this color blue. And then this alpha 0.04 refers to like how dark are the shading of the blue. So I like to keep that in there. If I didn't, it's just a dark blue and that's okay. But I decided to adjust this here. So this is what we get. So we see we've made it 3D and going along the x axis. There we go. You know, we can see the edge of this curve is that y= x^2 and then it revolves around. This is what this shape would look like in three dimensions. Now, we can picture this. Here's another we'll just modify this a little bit here. Supposing instead of just x^2, it was x^2 + 1 revolved around. So, we see it doesn't go down to 0 0, it revolves around like this. And again, we get this like, you know, horn or megaphone, you know, looking shape. But now we can picture what that looks like. That, you know, this, you know, we imagine this is a solid. And we're looking at getting the volume of that. And I just need to change my lower and upper bounds. What do I want it to be? And I can change what what is the function that I want to plot. And with all that, the rest of this will plot. And we can use this to visualize a lot of 3D plots and that's going to be really helpful. This is what we would add. And actually it ends up not working very well on its own. I do need to include this in the other. But when I graph a second function, but we'll look at this in a little bit. I'm not going to worry about this right now. So now I want to calculate the volume. So for that I'm going back to simp and my symbols and this is my function here which I'm just going to call it radius and it's its own function. So if I had y= x^2 then you know I could actually probably connect it with what was up there already but I'm not going to worry about that. You know, you can copy and copy it and paste it here or whatever you'd like to do. But there's my function. This is what you would change. And remember, you know, if I'm revolving it around the x-axis, the y value is the function. So this would be y= x^2. And then remember pi r 2. So simp. So that's already built in there as the area. So really that's our function that we're taking the integral and here I'm just defining the lower bound and upper bound si.inttegrate that area and then as x goes from lower to upper we you could link this all together use these same lower and upper the difference would be the radius here I definitely want to use x and I'm using those simples and then the other one I'm using u. So that's one way if you wanted to. But anyway, this is the exact area and it's going to be, you know, this exact number because I'm using simp. If I needed a numerical answer, I could then add si.n the numerical approximation of this. So you see, I could get this as a numerical approximation if I wanted to. And there we go. So anytime we want to revolve something around an axis, we can use this. And then you see, you know, there we go. U^2 + one. I could, you know, copy or something like that. Um, lower and upper. Um, there we go. I could represent it in three dimensions because that's what I want to see. And then I can go down here and calculate the volume. I'm not worried about graphing this because the graph of this, the numerical answer is what I want. But the graph of this doesn't look at all like what I wanted. But notice the graph of this in two dimensions. The numerical answer to that over that same range is the volume of that other shape. So there we go. So we can get this and then we can get the numerical answer if we want. I I like to include both because sometimes we want one, sometimes we want the other. And then next we will look at the hollow shape. Like supposing I want something like this but maybe part of the inside is missing. What's the volume of that? And that's what we'll look at next. So what happens if I have a more complicated shape that maybe I I it's like a hollow shape like if I have this revol you know this would be let's say y= x^2 + 2 let's say I have that revolved around the x axis around here that that would be like the bottom part. All right. So, I get that. But let's say it's a hollow shape and in the middle I have maybe something like, you know, y equals 1, just a cylinder down the middle here. What happens if I want to get the volume of this shape, but not the volume of that cylinder? Because, you know, I want to know, I don't know, maybe I'm making this out of plastic. I want to know how much how much plastic I need. So, you know, for maybe making like, you know, like end of a a horn or clarinet or something like that. So, what do I want to do? Well, if I have this, you know, odd shape, I can do the volume of the outer minus the inner. I can do that. And if if I was factoring this, I would set these up as, you know, two integrals. But then if it's, you know, integrals over the same range, I could combine them um as one integral and still do the subtraction. But what we're going to do is we're going to look at just in the code how I can just once I identify the outer and the inner. And again, each of these defined by an equation, then I'll just set this up and do the complete integral of the outer minus the integral of the inner. And what's left is, you know, the volume of that shape. This is a lot of things that you can create in 3D um modeling. And then maybe you want to print this out, you know, with your 3D printer. So in doing that model, one of the things you might want to do is say, okay, when I print this out, how much plastic am I going to need? So you we want to do this. We want to do the math. We want to see the sketch of this and be able to do the calculations. How much plastic do I need before I even make it? So, we're just going to do the volume of the outer minus the inner. And we'll also look again at a 3D model. So, you can see what that would look like and how to set this up and calculate it. When we're doing the volume of a hollow shape, which is really going to end up being the volume of some outer shape minus the volume of some inner shape. This is where the non-code calculus doing all the factoring could get really involved, but the code's going to help us here. And we have this code that we set up when we were doing the volume of one sheet. All the same imports to see what this looks like in 3D. And I'm going to have my range and all these things that we still have to set up the graph. None of that's changing. I can change the range if I want, but I don't need to change any of this. So now I'll define the functions here. And the first one I'll make this, you know, u ^2 + 2. So it's still going to have that curve, but you see we'll have a shift here. And then that'll be the outer function. So let's say we have like some we can leave this. But let's say we have just even like y equals x. So that's going to be u capital u um here. And I'll define this as function two. So all the rest of this is still the same. I just have, you know, fun for my first function and y1 z1 and then fun 2 y 2 z2. And remember each of these, you know, we set up that u is my variable here for this mesh grid. And then this multiplying by s and cosine like parametric equations gives us that that 3D shape revolved around the axis. So if I just have this or I could even have it as a constant. Let's just say I have this as one. So let's take a look at this. So I have this square uh u ^2 + 2 and this is just one. It's going to be this constant. So now when I plot both of these, you see I'm going to plot this surface x and then y1. You know, still that's those same x values. So notice that's the same for both these. Y1Z1 and my color is going to be red. Alpha equals 003. That gives it the level of like how dark it is. I'll do the second one. Same X Y2Z2 alpha. 02 slightly lighter. And then the color blue just to show the contrast here. And you see we have this the volume. I'm going to get the outer minus the inner. But this is what I see what this looks like. And then we could even have let's say supposing I have some like interesting you know y equals x and this one would be I could have like that inner shape maybe you know hollowed out for some reason but then comes to a point you know all kinds of different things. So this is what it would look like and I'll just define these. Let's just say these are the outer minus the inner. So, I want the volume of this, but I don't want that hollow part. Like, let's say it's like how much wood or foam or plastic or whatever I'm making. How much would it require to make this? So, it's that outer shape minus the inner that I'm not going to use. Now, we see it. But then calculating the volume, I'm going back to simp import all this and same lower and upper bounds. So if the outer radius I'll I'll use this one. So it's going to be u ^2 + 2. And remember that's the radius. So x^2 + 2. And all this I'm just setting up for the outer. Remember if the y value is the radius. So simpi.outer radius squared pi r squ. And then I'm going to take the integral and that's going to be my outer volume. And I'll do the same thing with the inner volume. Remember I just made that the actual function was y equals x or this equals u. So equals x that's my inner radius and pi r 2. And then the integral for that one. Um, and notice the outer volume. I want it to be outer area. So that should be what I'm taking the integral of. And then this one is inner area. So then that's the one I'm taking the integral of. So I get outer volume minus inner volume. And then there we go. So I get this. There's my difference. And that's the exact answer. Maybe I want that. But remember, I also could make it simp. N spi.n is the numerical approximation. So I really I I like to get both of these. So I want this to calculate the exact volume and I want to outport this and that needs to be its own. I can't do this in the same block of code because this is the last line. So then it gives it the nice display and then I can do the inner volume. There we go. Or the actual volume. So I could do this for all kinds of different things. you know, maybe I had this, you know, plus two and maybe I make this, you know, to, I don't know, some sort of like thing that comes to a point. That'd be really thin at the top. But let, you know, again, let's just say that that that is the case. Then now I see what that looks like, how things line up. And then I calculate this. There we go. you know, what's that volume? There we go. And then, you know, slightly different than before. So, these are the things that we could do. All that's set up to display this complicated volume and then to calculate it. And then we really only have a few things that we need to change. What I want is my upper bound and lower bounds and what functions I'm using as the outer and inner functions. Now what happens if I have a shape that I revolve around the y ais instead? If it was written as y equals, I just need to reate write it as x equals. And why am I going to rewrite it as x equals? Because if it's revolved around the y ais, so if I'm doing this, that's what that would look like. and where's my radius now? So, we're still using that same logic of, you know, cylinders here, but notice I'm going to draw the cylinder this way this time because here now is my radius. And so, my radius is my x value. So, I really would rather write it like this as my and then so my radius is my x value. And then if I'm slicing it, I'm slicing it this way. So then this is dy here. So the math of it is kind of the same but the picturing it and what variables we use and where that's we really need to just be able to picture anytime if I'm revolving it where is that axis and then what's my radius. So then once I identify the radius and then perpendicular to that is always like how am I slicing it? So identify the radius here in this case it's the x value. So I want it written like this. Then since I'm slicing it this way it's dy. So then the volume is going to be pi * in this case my x value y^2 d y I get that and then the sum of that. So it just becomes the integral here of pi * y^2 because that's specifically for this time d y and maybe maybe my y value still goes from 0 to 3 if I want you know let's say you know if I wanted to cut it off here at three. So you know zero to three that that would be the setup. So in the code then when we look at this it's really going to be the same setup. It's before we get to the code that you want to be able to picture, you know, what you're doing and make sure you set up the correct um the correct function for this to get the radius. So that's really it. As with a lot of code, you do um and certainly with math and code, you're you're doing more of the thinking before you're writing the code so that when you write the code, you're like, oh, okay, it's like this that I just that I did in a different situation. But then you want to make sure that you're putting it the correct way. So, as we look at this in the code, it's actually going to be almost identical to what we're doing when we're revolving it around the x-axis, um, really I'm just going to swap out the variables. So if I'm developing something in some like 3D software with the intent on printing this out then hey if you're doing this make that center you know whichever way you want to do it like X or Y whichever works for you make that your center the axis your center and then you know define an equation for that that's just going to help. So, let's take a look at how to do this in the code and how similar it is to revolving it around the x-axis and then just what you need to do differently if you decide to revolve it around this way. So, let's take a look at the code to help picture revolving around the y ais instead of the x- axis just like we did this code here to revolve something around the x axis. And you know, you remember this code, X equals U, and we work from here. We're just going to change a few of these things. X, and then you'll see we change Y and Z here. If I'm revolving it around the X-axis, lower and upper still works as variables for my range. And all of this stays the same, but here's what I'm going to say. Z equals U. And I'm going to say Z instead of Y because of the way this Python graph displays it. I like that output better. Um, so I'm going to say Z equals U. I can still define the function the same way, same variable, but here's what I'm going to do. I have X and Y as my variables here. So that's the shift Z and then X and Y here compared to here X and then Y and Z. The reason why I don't switch it from X to Y is like I said it it you'll see it it it looks nice here and it shows vertically. And that's what this whole part is. This is really just about trying to picture this because you're doing the math of of this. And there we go. So we can picture this now vertically that that's what we want to do. We want to be able to just picture that. Um but you're you're doing the the math you know solving making it x equals instead of y equals. So this is you know just to help you and we'll come back to the second function calculating the volume. Then if we look at this now again this is just comparing if it was you know y= x^2 we see the radius x2 um or if I put x^2 + 1 and here um the hollow volume. So if if I wanted to calculate the volume, here's what I'm going to change, then I I still define both of these symbols, but I I'll just call it notice the math of this is very similar y^2. And once I just define that in as a function of y instead, all the rest of it falls into place the way I have this defined lower, upper. There we go. And it's just here that I'm going to change this variable from X to Y. So you see the math of it and I expect the same answer. Um the math of this and the code is the same. It's really you're just changing that variable from X to Y. So really the advantage is just picturing it differently. But once you solve it and realize that the radius is my x value, then you know we change this here. There we go. And then oh now that's my y value. There we go. And you know all this I don't have to change anything here because I just calculated that. And then there we go. And same with the hollow volume shape. If I look at this, you know, there's my hollow volume and I left it as this. Maybe I'll just change this to plus one just because you know again this is the the hollow volume revolved around the x axis and we can picture it that way. So then around the y ais. So these are the changes that we're going to make. My y range instead of my x range. All this stays the same. I still want that z equals u. Um remember I change it from x to z instead of from x to y. still define this and then here remember that's going to end up being Z. So X and Y are are these functions here. So for the second one I'll define this one and again X and Y. So then we see how we plot the surface X1 Y1 Z X2 Y2 Z. That's really what we want. And then again, we can picture it vertically. So that outer and then the inner shape. And like before, calculating volume is really going to be the same either way as long as um I just change these. So if I just want my outer radius, I'll make this instead of X, I'm making this Y. I've already defined the symbol. So that's it. And this doesn't change. And then this just becomes Y. So really, we're really just switching X and Y for this. Picturing that, that's what helps. But the the math of it ends up being the same. Inner radius 2. That's it. And so again I just change X to Y outer volume inner volume and it should be the same. And again you know simply that we're just changing X to Y. But it's all about you being able to picture if it's a function of Y then making it this. And then these are the things that help. Can I picture what that looks like? Just like I wanted to picture the other way. Um but but that's really it. It's just you know where is my radius and then that's what you know where is my radius and then therefore how am I slicing it and then what uh value then am I going through? If in this case x is my radius and then the y value is the direction I'm slicing it. So all these work out really nicely to switch it from revolving around the x- axis to revolving around the y- axis. Picturing that with Python graph and solving it. So if I wanted to find the center of mass on let's let's start simple. Let's say it's just on one dimension, you know, and I have some, you know, where where is the center? If I have, you know, a bunch of things on each side and, you know, maybe I have each each of these represents some some mass that I have, you know, the mass of this object and then the distance from the center and I can, you know, uh, calculate these distances times the mass and, you know, where is that center so that this would all balance out. So we could do that if it was just in one dimension and and that's discrete not continuous. I just have certain things that I can measure for each one and then that's the moment of the moment of mass. Uh notice that connects with like the moment of inertia. How like I have to move you know I have to move this you know if I if it was the moment of inertia but the moment of mass is that you know distance times the mass and then you know I have all these moments and I need to balance them. So if I have this in two dimensions I would be doing something very similar. I would have like all of my uh you know I would calculate all the masses you know as it relates to x values you know horizontal and then I would calculate all the masses as it relates to y values vertical again uh doing that multiplying adding them all up finding that center um you know a averaging them out that type of thing. So all these are nice if it's nice and discreet. Um not really I guess you don't really need calculus summation overlap you could almost call calculus but what happens if I have something continuous. So now not just on one dimension not just on two dimensions but we'll we'll do it with two dimensions um with a common thickness. So really you know it is in three dimensions but we we'll say that there's a common thickness that if I have something here that um if I'm graphing XY and you know for whatever this graph might be I'm not even going to worry about an equation for this then you know maybe I'll bound it by like the X axis y ais and there you know something like that. Let's say I have some shape like this that I would definitely have an equation to define that that that actually that that really makes a difference that that really helps um because realistically if I'm if I don't have an equation to define it and it if it is a bunch of points then I am just doing summation um you know for a lot of different discrete values and then it goes back to the non-calculus. So let's talk about this. What's the total mass of this? Well, I can get the area and then that times a common thickness. So, that would be the total mass. Um, we'll call it the common density. So, the total mass of this is the integral of this. Um, whatever my y value is, like let's say I don't know, I'm not going to worry about like what exact equation this is. the integral of that y value times um times dx and then all that times a common density which I'll just do as a capital d for density. Um so that's going to be my total mass from you know from a to b from wherever um the range is. So now I have this this is good. Now how can I calculate those moments? Let's take a look at each of these. Supposing I have um and and now as as we think about calculus just like we were thinking about before the uh rectangles like let's say I have some particular rectangle here. There we go. And so adding these up let's talk about each moment then. Um so if this rectangle now has this density here. So that would be um you know the y value time the the density times the y value times you know dx. So you know that would be you know like the density of that rectangle. So if I wanted to then get the you know the center there um as it relates to um the x axis you know if I have some center then what's that center is that y value and then half of that. So you know that y you know that y value and then half. So if I wanted to get um you know this this density as related to the x axis here what I'm going to do is I'm going to have th this moment for x and it's going to be the density times y squar well because it's y that's going to be times y over two. So it ends up being um I'll even write write it like this y * y / 2 um dx. So this becomes this moment of you know how how much mass is you know at each of these each of these uh rectangles this one in relating to the x- axis here. So if I have you know from here to here so that that density times the y value squared. So that would be like that density um you know this distance from the y- ais and taking that to calculus we would have the integral of that density and you can bring out the d time y^2 / 2 dx and that becomes this moment relating to the x- axis So we can get that that density there. Now let's talk about it relating to the y ais. So if I still have that there's my rectangle still and then now if if it's that's the density of this rectangle but now it's that as the distance from the y ais. So what is that distance over the Y? Well, that's my X value. So then it becomes X times so the uh times the density I'll just put the d here times Y * DX. So that becomes um my value here. And since all this I'll I'll define this as a function of X. It's going to be then the inter then that m y relating to the y ais is going to be I'll bring out the d for density. So it's x * y * dx and again all of these would be the integral given the entire range because we're doing this same type of thing with every rectangle. So then that's calculus. We're taking the the integral there. And how do I get the overall uh let's let's put this all together here. So how do I get this overall um center of mass is that I want all right so if that's my and then remember mx was the integral density time the integral of y^2 / 2 dx. So we get that and then remember the total mass of all of it was just the integral you know of y whatever that y value is defining it dx. So when I have these, so then the center of mass for the x value because remember this one it was related to you know we called it my distance from the y ais but that I a lot of times I've seen this in in the textbooks it always seems weird at first but this is my because it's the moment from the y ais but the x the mean for the x value. And this gives an like an a number which is the xcoordinate of this center of mass would be that my as you calculated it divided by the overall mass. And then the y mean would be because remember when we did this it was like the moment from the x axis. So then that would be mx as we calculate it there over the total mass. And so this is what we have here as you see like taking it from being these discrete amounts to calculus this continuous amount with this common thickness. We can look at this and we can get you know that moment. So I you know each of these moments but then that's not enough. I need that as it relates to the overall mass of the whole thing. And so I can figure out doing this where is the xycoordinate of that center of mass. And I I think that's pretty cool that we're able to do that. So that if you know if if it ends up being something discreet then you know we're going back to summation and we're doing that. But if it's something and that I can at least get a pretty good approximation with that I can define by a function some y-value then that use that function and then I can get you know the center of mass um on some continuous area. Now given all this you know I want you to see how like hopefully in in in looking at this explanation it makes sense because now we're going to see just how we can do this with code once I just define that y value you know what is that function and then what what's the range that from where to where you know above the x-axis is is this example here so once I define that then yeah integral to get and all All these integrals would be within that range from A to B from A to B. So once I define that function then I can figure out um you know there would be that common mass you know all these with a common density here. Um and so what we would do is if I have this define this calculate the mass you know write the code to do that you know and then you're going to have that mass then you already have that y value stored y^2 over two take the integral of that calculate this moment y * x and simpi you can just incorporate that in into another equation get this moment moment and then with all those functions then we're going to calculate the exact point xy so we get that point of where that center of mass is and like a lot of things we're looking at you can set this up so that um in the future if you have a function just having that one function to define it and then you can boom you know all the other steps cascade down and changing that function changing the range and you can figure out where is that center of mass for some different things. So let's take a look at how to do this in the code. Now we're going to translate all those equations into code using simp again to find the center of mass. The usual imports and we're going to have this that I have a a common thickness. If this was, you know, something in three dimensions, there would be a thickness to this. But as long as it's the same thickness everywhere, you know, then this works. And I'm gonna, you know, as x goes from here to here. So I I picked this range because this is the equation. Um, and we're looking at this from this down to the x axis. You know, where is the center of this? Um, so I pick sine of x. And then we go to these formulas that, you know, I I want to get the mass. So I made that my variable. I'm going to take the integral of this and you know keep using this from lower to upper. So once I get the integral of that remember that's total area but then for our purposes here that would be the total mass of whatever shape I have here. And then I'm going to use this I want equation two here. I I I made it it's separate. I like to split things out here. So that's going to be that original function squared / two. And remember that's the one I want to use. So then I have that equation two. So that when I solve for mx, I'm going to integrate this as x goes from here to here. So there we go. So I have equation two is a separate. So that way I can just put equation two here. I just like doing that a little better. Equation three. And then you know that's mx equation three. Remember it's x times that first equation. So I'll do that. And now for my take the integral there. So now that I have mx and my um remember to get that x center my divided by mass and remember I kept the conventional notation you know this is mx um my divided by the mass is actually the x center because as x it's like you know distance from the y ais type of thing and y center is mx divided by mass. So now I have these two. I'm going to print these out and simp if it involves pi or something like that, it's going to give me, you know, something like that. So I I want that exact answer, but I'm also going to print out the point. And for that, I'm going to do si.n. I want to get convert that to, you know, a decimal number, however many decimal places. SPI.n here. And there we go. So I want I want to see these I want to see the point as an XY coordinate in decimal form and then I'm going to plot it and plot you know that original from over upper and then I'm going to include these markers so that I can plot that point in there and when we do this so given that the X center Y center and then here's the point and we see it graphed. There we go. And zero to pi and the point in the middle. So there we go. Sometimes it gets weird. Simpai sometimes will plot things where this x axis the the numbers on the graph are correct and it shows but the x-axis shows up in the middle of the graph and that just gets kind of weird. So, you know, I could find um you know, may maybe I have that, but maybe um you know, I don't know if I have like sine of x plus one, just that little shift. And I supposing I still want to do the integral from here to here. So when I have this, you see it really does the X-axis goes down here, but it doesn't go all the way down. It's, you know, it's you can uh force this in here and do like X limb um Y limb. I put this other one uh you you know instead of just X with this upper and lower you could um put in you know X limb equals and then give it a range Y limb equals but um just to show you sometimes it it gets weird and the for the limited aspect here the graph is there and the numbers are correct and this point is correct it just looks weird. So you you you could change the dimensions and you see center of mass like this is the simp answer but then you know I'm gonna go like that's that's the point here. So the x value would be the same but notice the y-value changes because we shifted it and yeah so these are some of the things it will also if you do this let's say I make this sine of x but I go from si.py pi to um so from there all the way around to 2 pi. So now we have the negative value. So it will still do this and give this value. And you see here the yaxis now showed up in the middle. Senpai just likes to do that. put the axis somewhere. There's there's an algorithm for how it feels like some line of symmetry or something. But you see the x values are here. Like this is not the y- axis, but simply just put it in the middle here as we see this graph go from pi to 2 pi and then down below the x- axis. So there's the X, there's the Y, you know, and as decimal numbers, it all looks correct, but the axis, the way it displays it, it just gets weird. So remember, the these are designed to to help to give you a visualization, but sometimes the output um sometimes the output just gets gets a little weird, but that's okay. Let's do one more here. And supposing my equation isn't triging it's uh [Music] simp. There we go. And I'll make this from zero to nine. How about that? We'll make it, you know, some nice even. And that notice this is all I need to change. You know what equation do I want? And then here because all the rest of this is then built in and I get my simp values as well as the point here. And we get a nice visual of where this would be. This one the axis looks nice and that would be that's where the center is and we have the xy coordinate for it. really good being able to get the center of mass you know in two dimensions here and simp let's talk about the normal curve and what do we mean by normal we're looking at really a frequency distribution of you know the y value would be you know what's the probability of a score being right around here and the x value would the standard deviations. So little bit of statistics background. If I if I were to measure, you know, anything and I and I get this like range of scores, if I have enough scores, the idea is that most we would find some mean score and most other scores would be around that mean, you know, within one standard deviation. And so you know that that's let's start with that theory that whatever I can measure that I have you know I once I find the mean most of the scores that I measure will be around that mean pretty interesting thing to think about. So if I have average height of of people wherever I go if I really measure like enough people I'll get some range of heights and somewhere there I can find you know calculate the average height and especially if I take it to several decimal places maybe I don't find anybody who actually is that exact average height you know right down to so many decimal places but most people would be within a certain range and then that would one standard deviation above or below or you know what whatever I measure the normal curve theory really kicks in when I have a lot of scores I mean millions ideally so that you know that's what we're looking at that if I measure if I measure something that I can calculate the mean then most of the most of the scores are going to be within one standard deviation and what's what's standard deviation well once I have the mean, pick any other score. The difference between that and the mean is deviation. It's really not average deviation uh because the formula is a little bit different. But a large standard deviation means that there was a lot of differences and so therefore the scores get more spread out. A very tiny standard deviation means that the scores are all really close to one another. So what this measures then is you know if I talk about standard deviations um I'll just say one two and three because the normal curve theory that most scores are right around you know one or you know one standard deviation above or below and then you know I could find this range not drawn to scale. Um, we might see that we, you know, might look at this and see, yes, maybe that looks like a snake eating an elephant. Um, definitely not a hat. Some people call it the bell curve, but really it's all about that frequency distribution that, you know, most scores and when we say most in terms of frequency, like it really kind of caps out here at like 0.4 as a frequency. So, you know, I changed the scale of this a little bit because otherwise it gets like really steep. I don't know. It it works out nicely just to see it a little bit more spread out. So, within one standard deviation, we can say that most scores are there. And this is where the calculus comes in because if I look at all these scores like how many of those scores would be within that we find that if we get the area under the curve here then we can figure out a percentage that would be here and actually that's where the calculus comes in. Once I establish this normal curve, and this is the equation for it, which we'll talk about in a second, but once I establish this normal curve, the area under the curve is exactly one. So that's what's kind of interesting. The area is exactly one. And then when I get this area here, it's about uh 68%. So it' be like 34 and 34. But then that's what we're saying like you know given something if I have enough data points then I calculate the mean I expect about 68% are going to be right around the mean and you know we can look at you know what percentages are beyond this. So if I'm measuring something something beyond one standard deviation now that's news. anything within one standard deviation, I'll say, "Yeah, that that that I'll lump that in with like that's average." Might be, you know, maybe close, maybe a little bit above average, but I might not even call it significant. Once I get beyond one standard deviation, and in some statistical tests, maybe you talk about around two standard deviations. Hey, now we're talking about something significant. Hey, this is news. Wow, that's that's noticeably different. But the calculus of this is all about looking at this and this happens to be the equation for this. I often I put it here, but I often see the e exponent written as a negative exponent in the numerator or even beside it. And when we get into the code, we'll look at how if I put this in one way, how Python will then display it um you know in a slightly different way. And that's fine mathematically as long as like the factoring works that yes this is the same equation. So you know this is the equation and if we notice then whatever x is this is never going to be zero. And that's what happens. We get this it drops off beyond you know it right around uh two standard deviations it drops off kind of steeply and then a little bit beyond three. If you look at the graph and we'll look at that in the code, you know, it's very close to the x- axis. So, you know, four, five, hardly any scores would be four or five standard deviations away, but it's still not officially zero. And a lot of times um if you you know set it as like you know an upper range of like 10 standard deviations the official would be it goes up to infinity but even when you get up to like 10 standard deviations that's that's enough for it to like demonstrate a lot of the theories um you know how many standard deviations you know beyond the mean are we? So the calculus of this is calculating the area under the curve. So we're going to look at integrals and then we're going to look at um ranges here uh you know given a certain standard deviation. Now this the the general simplest form of this would be a mean of zero and a standard deviation of one. So essentially we are exactly counting standard deviations. So one standard deviation two three there we go. Um, I could actually have and we'll we'll look at this in the code too, but you could have this shift wherever and have a and have the mean be whatever actual number that would be and then the standard deviation be a number of like you know what would that be? Um, let's say I was giving a test that I I gave to millions of people and you know there is a definite average test score. that could be my my mean. And then the standard deviation, you know, what would that be if if I gave it out to millions of people? What would the standard deviation be? Then, you know, we could graph it that way. And then this would show up. The x-axis would be different, but it would show up. The mean would be somewhere on that x- axis, and then the bell curve would develop at that point. Um, and we could see how spread out it was or narrow. So, you know, these are the things that we can do. um that works out really well to work this in the code because we're doing a lot of this. We're going to focus not so much on the statistics but on the calculus of it calculating the area because maybe I want to find out how many standard deviations away till I get like the extreme scores. And a lot of times we talk about like the top 5% or the top 2% or the top 1%. So how many standard deviations away does that need to be? We can use calculus to figure that out because I can use you know negative infinity or as I said standard deviations I could even do -10 that's actually extreme enough to demonstrate from there up to some point um if I want that area so if I want the top 5% I would find some point so that from from the lowest point all the way up to some point here that the area would be 0.95 and then I know given that then the top would be you know that's my cut off so beyond that would be the top 5%. So we'll look at the calculus of how we find these critical values, but really it kind of all gets woven into this notion of if I have enough scores things will and distribute to this. If I if I'm doing any sort of measurement any scores I'm not necessarily going to get a normal curve at all. Um you know whatever I happen to be measuring and especially not so many scores. Yeah, I get scatter, you know, points wherever. But the idea is if I have a lot, like a lot, like millions, it it will even out to this, which is kind of an interesting theory. So, we use this a lot in statistics to say, all right, if I did this, if I have all these scores, then you know, what's my cutoff score to say that something is this is news? This is extreme enough for me to take notice and do something. So let's take a look at how this would play out in the code here. Now we're going to see how calculus, statistics, and Python all interact. Looking at the normal curve. So mathwise, this is the equation of the normal curve. And you notice there's a few ways you could write this. This is the most common way I usually see it. So I put it this way. 1 over the<unk> of 2 pi times e to the x^2 over 2 exponent. And this is the simpler version because uh the mean is zero, standard deviation is one, and with that there's a bunch of other things that then cancel out. That that's nice. This works. And really, this is the z graph. The zcore is really counting standard deviations. So that's why the mean is zero and then we're really talking about how many standard deviations away from the mean we are. This will then illustrate all the theory with the normal curve and area under the curve and probabilities that works out really well. And then we're going to look at the more complicated version so that I can have the mean be whatever and the standard deviation be whatever and how that adjusts the curve and everything. So let's take a look at this. So um we also notice that there's other ways I could write this. I could put the e in the numerator. I could this is a negative exponent. I could put all this in the denominator. But this is the usual way. And then here's how I would do this in Python code. You know, I'll do that one over the square root of 2 pi and then all that times and remember simp.exp is e to this exponent. So when I run this and I said this is the normal way I see it. There's a couple other ways you might see it. This is a rare way because what simp is in the denominator it factored out the two but then to make that work out here it had you had to multiply by the square<unk> of two over the<unk> of two to get that also in the denominator in the numerator. very weird. I felt like it was unnecessary. So, I wanted to check something here because simp do simplifying and such. So, I took this equation and I wanted to multiply it by the<unk> of two over the<unk> of two, seeing if I could cancel that out. And no, it didn't. So, there we go. There's your normal curve. And we have it stored really as EQ. So, when I plot it now, I really don't need to add this X limb. Um, and I'm going to show you if I didn't have that, I could just plot it. And that's fine. That's nice because this is a default built-in. Um, Senpai works out nicely like that, you know. There we go. And I have center at 0 0 and there's my highest probability right there point4 and you see right around you know two two and a half standard deviations it really drops off. The reason I had this in there is because I just like that proportion of setting out this limit. it it just makes it a little bit more stretched out. And really it's so the the remember this is the frequency distribution. So the line that this defines here is the probability the the the the frequency the likelihood that uh this shows up. So, you know, we're saying that right around the mean, you know, we would expect about 40% of the scores of whatever I'm measuring. And then, you know, the y value here is that frequency. So, how frequently do any of these scores, you know, one standard deviation above the mean, two standard deviations above the mean, you know, how often does that show up? Um, and then we see, you know, what's the frequency beyond three standard deviations? It's really really low. So that's why, you know, it it just gets so low. Uh, beyond that, it it I I feel like it's nice to just cap off the graph here. But that's all that's just, you know, a preference for the the way this displays. And this is the bell curve because, as you can see, it looks kind of like a bell. So there's your normal distribution. And again, the y-value is the frequency at any given point. And this is the theory. If I get enough scores of something that I get normally distributed, then that's we get most scores right around the mean of whatever I'm measuring. And the further away I get above or below, it really drops off in terms of like, you know, frequency of how often that those scores show up. So now let's bring in the calculus area under the curve and we're going to take the integral si integrate and I'm going to integrate that equation as x goes from negative infinity to infinity and remember that's si lowercase z o. So the area under the curve is exactly one. And that's the beauty of this and why it relates to probability because that cumulative probability what's the probability you know the area under this whole curve that's one that's every every score should fit under this if it's normal normally distributed but then if I have certain values here what's the probability that if I go you know from some low value -4 whatever all the way up to something you know what's the probability that I have scores that are within that area that that range it's the integral and then we just you know identify our low and lower and upper V values. So that's what we're doing here. I can you know going back to that I can say all right well now lower is negative one and upper is one. I'm going to take you know the integral lower and upper and I'm going to do two things here because I want to show you I'm going to print out si.n. So remember, I'm going to, you know, I'm going to get that area. When I do the integral, it gets the area. That's going to be um you you'll see it uh the way si displays it, but I want it to be an actual number. So that's where I do the sai.n to get that number and I'll print that out. Then I also will show you what that exact area is because then you know display that too. So when I do the integral now this is from negative 1 to one so within one standard deviation above or below and as we were talking about before with the theory you know about 68% is within that range. Now what's this look like in SPI? It's the error function. That's why they they call this is the error function of this like that's the actual you know if uh we'll take a look at do doing this. So the error function of roo<unk>2 over2 um you know that's the behind the scenes the exact math answer. So you know if I take uh the integral now notice I'm just doing this again here taking the integral um because this remember I gave it up here a definite value in take the integral as x goes from here to here in this case negative 1 to one so it gave me a definite value but if I just take that go back to that equation equation and if I just take the integral the in you know what the integral involving x you know what is that formula this is what it's going to give me the error function of and then this roo<unk>2 over 2 * x and then all that divided by two now if I just plot this this is what the error function looks like shaded you know I just took the integral there's my error function and now if I plot this and again I I decide to cap it off at five to five because it you know beyond that it just continues like this. Now remember that doesn't exactly flatten out. It gets so close to zero that slope but that slope is never going to be zero on either end. And remember if this is the integral the slope of this the derivative is our uh is our normal curve. So we see this slope it's actually officially positive so close to zero but officially positive and then it gets to be a greater slope and then still positive but again the slope still positive but less and less. So it's like close to zero but still positive. So we see and going to show one more thing. So right in here would be the number one. So what's the slope here? If I go up 04 over one, you see? So the slope right around here is exactly 04. So you see that slope positive but close to zero gets steeper up to about 0 4 and then less and less of a slope still positive but zero and that's exactly what we're seeing here. The y value of this is the slope of that. You see positive but close to zero. these y-v values increase up to 04 decrease down to zero or positive but close to zero. So we see you know we see see the connection here of of these and then what we want what we do is if I look at this so back to that error function now if I have that remember I'm I take that and I'm going to substitute in for x simpi you know I cut the graph off here but supposing I plug in negative infinity for x what do we get - one2 half. You see? So, I could probably I probably don't even need to go down to negative infinity. Sometimes like maybe even like -10 or something, not even close to infinity works because it just continues and the the values beyond that are so tiny. But notice then remember I plugged that in. So when I did that integral here to get the area. So when I plug in you know when I take that integral and plug these in si infinity the value that's 1/2 minus a negative one/2 and then that's where we get the one. So that's it you know the y val you know see it goes beyond 04. So we have like one half as that value minus a negative one2 and then so there we go from when I plug that in plug in negative infinity the answer is negative 1/2. If I plugged in positive infinity the answer is 12. So then um that's where we take you know again just coming at it from another angle taking the integral um I'm going to go back to plugging in as negative infinity because I'm going to use this for something else here. So how can I find that critical value? So remember, I'm going to take that integral and then plug in. So that shaded is the integral. Remember, we take the integral um plug in the upper value, plug in the lower value, and you subtract them. So here's the integral with x um still as a variable. and then the integral taking the integral and then plugging in that lower value in this case negative infinity. So that's how I would find that area from lower to upper. So that and I'm going to set that equal to if I want if I wanted to be 0 n5. So that's what I'm going to set this equation to. So that's it. you know, just going back to those definite integrals. Um, integral, plug in the upper, lower, the upper value I don't have yet. So, that's going to be shaded that integral with x still as a variable minus the integral plugging in that specific number negative infinity. So, there we go. that when is that what x value makes that equal to 0.95 and then solve it and you see we get these zcores that this is a critical value and plugging in 0.95 or maybe I have like 0.99 you see you know what zcore do I need to get that level of certainty And there we go. So maybe if you've looked at different charts of the zcores, um how do I get the get all these numbers? This is how they get the numbers. You know, given that area under the curve, what critical value do I need for that area to be 0.99? And so um and let me go back to 0 n5 because that might just be easier to see on the graph. 1.64. 64. So what this means is that then we have you know right about here when the zcore is 1.64 that means if all that area under the curve up to here that's 95% of the area and then this top part is the extra 5%. and a lot of things social science research you know top 5% we're going to say that's extreme enough that's news and I can reject the null hypothesis so there we go how can I set this up so that I can you know find my critical values you know all this and then the area and everything we're doing is like leading up to that so this theory all carries over this works out well you know the zcores We can carry all this over to the more complex version. All right. And also notice it's still this first fraction here. Um now this is where remember that Greek letter sigma is the standard deviation. So you see now that that works its way into our equation. And remember the earlier example the standard deviation was one. So I didn't even need to write that there. And then I get that times e to this exponent. Now if we take a look at this. So if the mean was zero, you see x minus 0 wouldn't I don't need to write that in the simpler version. And the standard deviation being one, I didn't need to write that. So then it goes back to that x uh x2 over two. Still a negative exponent here. So in this more complex version then what it gives us is the ability to then put in whatever mean or standard deviation we want and then we can see that. So I'm going to start I I imported these symbols and I I know I could have done this all in one line. I just kept it this way. Um I I'm going to start by just showing the comparison. supposing the mean and standard deviation are one or the mean zero standard deviation one. Um but this is how um because of this more complex thing just to make sure I get it right I like to break a lot of these formulas down into parts. So I just made the denominator of that first one standard deviation times the square<unk> of 2 pi and then that first exponent you see x minus the mean divided by the standard deviation and then all that squared and then exponent two I'm just doing simpx so it's e to this exponent.5 times that other exponent I just like to break that up. If I put this all in one line, um it just becomes a a a mess of parentheses and that's, you know, you want to reduce the likelihood of error. So there we go. And then this one I'm just going to call bell for the bell curve. And for our examples here, I'm keep I made it a different uh a different variable so that I could keep the other things for other if I want to reuse that. So now what do I have? One over that denominator times that exponent because that's what I did. I made that whole denominator something and then this. So one over that denominator times that exponent and we get to this. So first of all, really funny that it still does the same thing about factoring out that square root of two and putting this in the numerator. weird but also just the way it happens to do it. It makes this 0.5 instead of x^2 over two you know times 1/2. Okay so we just notice the subtle differences but this is where you know in doing writing the code knowing the math you also want to be aware of when hey this is mathematically equivalent it just happens to be how they're setting this out here. All right. So now when I plot this, there we go. Good. Um, and then I do the integral. Now I plot this and because I made the mean zero zero, standard deviation one, that that is exactly the same plot. We're going to go back in a second and adjust this with different mean and standard deviation, but just want to show you that if you go to that those values, it's the same plot. Now, when I do the error function, even though I made it that the mean was zero and standard deviation one, the error function, notice that looks very different than when I did the error function up here. You see very weird. Um actually it's this this is the integral of the error function here. So it should be the same. And again, this is why where we want to like take notice of where it's mathematically equivalent because if if I take this error function. Now this if you do enough trig you you remember that this decimal here 707 and it goes on and on is exactly the square root of 2 over2. But this part is actually just kind of funny because Simpai decides to put it like this. This weird decimal times a square root of two. So I wanted to show you this. If I do uh square<unk> of two times that decimal and then I'm going to convert it to an actual just decimal number. It's exactly 0.5. So, Simpai output this whole mess instead of outputting 0.5. Go figure. But notice then if this is 0.5 and this in the parentheses is the square<unk> of 2 over two * x then we do see that it does match up with that. you see <unk>2 over2 * x and then all that divided by two or times.5. So these are some of the things and a little bit later on in the course I will go through some some other examples too of a few things where simp outputs something and then I'll show the factoring to see hey these two things are actually mathematically equivalent just simp has its quirks for how it outputs things. So that's fine because this is now stored as the error function and we can use it. This works. This is just a check. So now when I do the area between values here, you see I'm just going to set this up. Lower is error function. Um, in this case I'm going to say negative infinity and the upper well I'm not going to uh we could we could check this that it matches up with the other um yeah we could do that if uh if I say uh like 0 68 2. So we see what am I doing? I'm saying from negative infinity up to here I expect the pro I you know all these I I really just want to make them all numbers. So si n you know what's the lower what's the upper and I expect the probability to to be about 0.95 if I do this. You see from negative infinity to here because I still have the oh 75. Oh do you know why? because I put zero instead of one. There we go. So, yeah, from negative infinity up to here. Yep, we expect that's about the that's about the right probability. I'm just rounding here still works. The area between curves, but this is where this more complex version helps us. If we go back to this and now if I change the mean and standard deviation I can make them anything I want. So I really can I don't have to convert everything to zcores right away. I can just say like like let's say I talk about a test that I give um it would have to be a test to a lot like millions of people for that distribution to normalize. But let's just say there's some test that's like the mean is 72 and the standard deviation is eight. So you know there there we go. So um one standard deviation above is 80 then 88 then 96. So you see somebody getting a 100 is going to be more than three standard deviations away. You know let's let's say that works. Now, first of all, let's take a look at how that affects this. And you see it's not just x minus the mean. Remember the mean is 72. And uh in here, this works. X - 72. Um, so Senpai just wanted to like write it as X over 8 minus 9. But that works mathematically. That that ends up being the same thing. Um, because that standard deviation was eight, you see? So they just split it up X over 8 and then 72 over 8 is nine. So again, the usefulness of being aware of the math u that you know why did it show up like this? Oh, senpai split it up into two fractions x over 8 and 72 over8 which then 72 over 8 is exactly 9. Another just weird. Um so square root of 2 pi it brought that out and remember the formula was eight out of here because it was the standard deviation. So when you brought out we brought out that and it was a two then times 8 is 16. So we we just see that like it looks weird. It's a little bit different um than I would have factored it but mathematically it's correct. And then we get that now this is that curve. Now I want to show you one thing here. This is the old one. So if I plot this here, this is the interesting thing. So I plot this. Now it gives me because what I set here I just wanted to give the x limit of where I graph it. So what I and I put this with variables that way I don't have to keep changing the numbers every time. But the mean m you know that lower x limit the mean minus four standard deviations the mean plus four standard deviations and that's what it did. You know 72 right in here and you know just over 100 is four standard deviations around 40 four standard deviations. That's great. I defined my x limit for what the graph should display, but it's not there. This is a subtle difference. And we'll also look at like even more uh uh simp knowledge in a little while. But if I wanted to find the x values, I have to go this way in so graph that bell and then in parentheses x comma and I don't want x limit I want I want it to look like this and then make sure I close that outer parenthesis. So we see this is the this is the way I want to do it. So just to be aware if you do it the other way the the way you would limit just what the graph displays that still might not change the x values that it displays. And so then you you show you get this blank graph and you're like what happened? Um it displayed what I wanted it to but here I'm saying plot it and I just want my x value to go from here to here and then the graph will actually display accordingly. So it'll be fine. So you see here also slight you know the maximum here ended up being um 05 but that's okay. Um but we see it still put that not put that uh vertical axis line in the middle but right at where that mean is. you know, there's that mean 72 and then we see the standard deviations. So, this is really what I would want. I would want to see, okay, mean, standard deviation, what's that bell curve look like? Great. And then, so this is what we have. I I keep I can keep this change the mean and standard deviation. See what that equation looks like. See what the graph looks like. And if I do the integral of that now definitely it's, you know, a huge mess. And I'm not even going to try to decode all these. Um it may or may not help anything. Um so this um we just did that check before I don't even need to do that again. But now now that I have that now let's talk about the area between values. So now if I have from negative infinity up to so let's say um two standard deviations so from 72 to 80 is one from 88 let's just type in 88 here and see you know what's the probabil you know if 88's the score. Now I can plug in that exact value and we see the probability is 0.97. All these upper and lower they're like my in between values as a way of checking um you know because this is really like the y value of that error function. Um and of course the lower you see is negative.5. But then the probability is 97%. So this is where we can figure out um you know oh percentiles. Oh you scored an 88 on this test you're in the 97th percentile or almost 98th really. Um and then it should be that if you scored an 80 that should be exactly one standard deviation. you should be about the 60 uh the 84th percentile because remember it's 50th and then one standard deviation it's another 34%. remember remember it still goes back to that normal curve uh logic here of you know 34% um between the you know one standard deviation in the mean 34%. So, yep. So, then, oh, you scored an 80. You're in the 84th percentile. And then we could then say, you know, two, uh, you know, that little bit more than two, 88. Was it 2.3 uh 90 should be in like the 90th percentile. Now, I'm just, you know, showing you that you can test a few different values here. Look at that 98th percentile. almost 99th. But you don't need to do this trial and error. I I just want to show you that you can f you know plug in whatever values and that doesn't even have to start at negative infinity. You can say you know whatever values um you know whatever lower and upper amount because it should be that from uh 72. So one standard deviation below. Remember the standard dev deviation was eight. So it' be 64 up to 80. So 64 up to 80. And we would expect that to be like 68% score within that because then that's one standard deviation above and below. And again, you don't have to do the trial and error because we're going to get to that in a second. But you can if you have a question about how many scored within this range, you know, this is where you would plug that in. And a lot of situations come up where you want to do that. How many score exactly within this range? Now I have that whatever my mean and standard deviation, I can plug in whatever score ranges I want, lower and upper. And you know again just outputting these out of curiosity but this is really the probability that we're looking for. So how do I find that critical value? So just like we were doing with that simpler version but now I have this here that error function um substitute the uh you know from negative infinity and if I want to find out where is that cut off from 95th percentile I'm going to do that same thing that error function um is the integral because we already did that that integral minus This this one is the integral plugged in that lower value. Set that equal to 0.95 and then solve it. So 85%. So you know for whatever I'd want to do if there's like a cut off. Um there we go. 85%. So, you know, we could do this, you know, 99. There you go. 90.6%. So, now I have that for whatever score the way, however I score this test. And, you know, it can be, you know, uh, scores with, it doesn't even have to be bounded by a 100. It can be however I decide that I'm scoring this. Once I get the mean and the standard deviation, I can set up that and you know area between two different values or probability to fall within that range. What is the critical value given you know the probability that I want? There we go. And then notice I can check this. So once I got this, I have lower and then um notice I didn't even uh call it uh negative infinity. I just called it -10. That's actually probably going to be close enough. And then step two, this is the answer here. So I'll just make that the upper because that's really what we just solved for. And then if I take the integral 70 oh 70 <unk>2 uh let's try this here and let's print [Music] simp. So we see, yeah, that's great. But and what this shows us then is I I I went up here and said, what's the cut off for it to be in the, you know, in the 99th percentile? Oh, a score of 90 or 90.61 actually. So then I'm going to just take that 90.61 61 as my upper and then look at that it says that yes if I go from there to there that is the you know the area under that curve is exactly 99%. And like I said you can still do it to negative infinity but it it levels off so much there that even -10 you're beyond you know uh significant changes there. And then there we go. Um So there we go. I just had it here of like area and then probability to do it again. Yep. Pretty cool. So there we go. We see how we have this formula that defines this normal curve and we can plug in any mean any standard deviation define the normal curve plot it get the error function which is the integral and I can use that to figure out you know probability from that it any score falls within a range or if I want to know what probability then I can solve it. You know what? What's my cutoff score for that probability? And there we go. Calculus explaining statistics and being able to write it all with Python code. So, you can set this up that if you're setting this up for yourself, you can keep this and all you have to do is change the mean and standard deviation. And this you don't have to change anything. So you can see the plot and each of these you just might run this one. You don't need to. That was just a check. And you can keep this. And then you can keep this for the critical value. Can keep this other check if you wanted to. And there we go. All these fundamental questions in statistics you can answer. You set it all up and now you know what this means and you can answer it. change a couple values and um you know decide whether to reject the null hypothesis. We've been looking at a lot of different default simp plots. Nice, easy. You could just simply plot the equation. And that's great in its simplicity, but what if you want to add something to the graph, change it, modify it, you know, uh change, you know, really focus on particular x values or y values or add a point to the graph and all these other things. So, we're going to look at all these things that are already just set by default that you don't have to use. But what if you do want to use them? And we're going to look at all these different attributes that you can use and see, you know, some ways you can really make use of these for certain graphs that it's it's going to really benefit you that you can say, "Oh, I I I I want to focus on this part of the graph or I want to add this to it or I want to change this." And we're going to look at how you can do that in the Simpai plot. So, let's take a look at the code. So let's look at all the other options that are available when you do simp.plot and you know we we were been talking about plotting just you can just plot the equation and call it good but all these things this is from the documentation when you when you plot these are all the arguments that are available and let's go through these here. So you know these these global options you can put a title and so notice the setup of this args comma title equals x label equals you know you can go through and have as many or as few of these as poss as you would like. Um so the title string pretty straightforward you can put a title to your graph X label string or symbol um because if you have X and Y as your symbols then you can just make that the label as well. So that's you know that's why or string you can put a letter or word in there for your X label and Y label. Z label um in the near future we're looking at our two dimensional graphs. So just to note that that's possible the legend and boolean uh true or false and for one line you might not need the legend you might not want it but multiple lines that might be useful especi you know different color lines and then it really spells out which color line is which function for the x scale the y scale the default's linear that's just my go-to you can make it a logarithmic scale and maybe there's some situations where that's going to help you. But just to know that you can change that scale and do anything. Axis is a boolean. You can turn the axis off. Maybe that uh suits your graph a little bit better. You just want to see the shape. um the axis center and you know it normally would be auto but you could um make this a tuple and you can put the center of where you want the axis or the graph to be move it around and again it enables you to focus in on different parts of the graph so um it's one I don't use that often but it's useful to focus on different parts of the graph um these the x limb and y limb um especially the x. That's a big one. And we'll demonstrate this because I might want to have a graph that, you know, maybe a lot happens like right around x equals, you know, plus or minus some low number, but then the some part of the graph causes it to be zoomed out and maybe I just want to limit that. So that just, you know, limits the x or you might want to limit the y. again different reasons why you might want to focus on one part of the graph and we'll look at how we can make that happen. Um, same with aspect ratio. Again, I could change this. Um, one of the things that I like with the aspect ratio, uh, and we'll look at this is our gra Python tends to elongate the graph and make it a little bit more of a rectangle and not a square. But if I want to graph something that's a circle, I might want that as aspect ratio. We we'll look at that autoscale. Uh now if you change that to uh not autoscale it then you also have to include another argument for what you want that scale to be. I I don't all you have enough other things you can change that I like to have autoscale just you know the default margins again the default's nice but maybe you want to print this or save it you know that graph as an image in which case you might want more of a margin the back end is going to be mattplot library so um I really I this is next level I don't feel like you need to tinker with this size of the graph also um again maybe the size of the graph the margins b you know depending on if you want to do something else with it annotations this one um I don't use very much as far as annotations for each of the points that that tends to be too crowded um markers I do use and again you'll see the default would be none but this is everything except for well you would keep these braces notice square brackets curly braces within that and then I'd keep that args um colon and then here's what what I'd put in again uh square brackets within that some x value, some yvalue, and then in these single quotes, um, a color and some indicator of what the the dot would be. So, this is good because it's going to plot a point on that graph. Maybe I have this graph and maybe I want to highlight here's a particular point on the graph. I'd have the xy value of that point. Now, in this case, this is a red triangle. Um, you could do the star and it be a red star. Uh, RGB are the go-to red, green, blue. Um, star, triangle. If I just put an O, lowercase O, then that would be a circle. So, some good markers. We'll we'll look at some examples of these. Um, rectangles kind of an experimental feature. We're not going to worry about that. Fill. This is pretty cool. And we're going to see a good example of this. You know, your usual defaults none, but you can fill the graph. Uh and more than just changing this, we'll look at some other code that you'd want to include before you do this. Um back in default. There we go. So, let's see some examples of this. This is our normal, you know, I'll just import all these simp. And then here's my equation. There you go. X^2 minus 3. Okay. So, one of the good things you can just keep all the defaults and just plot that. There's my equation. Plot it. Boom. There we go. Nice. All right. And you see where it's centered? It looks nice. Proportional. There's a lot of things. Simpai knows where the center of the graph is and a lot of that. That's going to be your go-to, you know. Let's just see what this looks like. All right. Now, here's what the plot would look like with all the options. But notice I kept all the defaults here. So this, you know, what am I plotting? I'm plotting that equation. That's that first argument. We do need that. All the rest are optional. But this is just keeping all of them in there. And you know, there we go with all the default options. And when I run this, that's going to be exactly the same graph. So we can do this without. We can just simply plot EQ without all the rest of this and it's the same. Now, let's start modifying this. Um, like the title, I could put the title in here or I could make it it another variable and then put it up here. You know, a lot of times it's it's future convenient for se separating out this variable and putting it here. So, there we go. Let's say I had this title. There we go. some graph and X labels. Now, just because I want them to be X and Y, um I showed you here's another way. I can just put it right in here, you know, X label equals X, Y equals Y, that's fine. And this works with one quote, single quotes or double quotes. Zel, I'm not worried about it. Access as aspect ratio, we're keeping the same X limb. We'll come back and change this, but I want to show you that for right now, we're going to leave that. um leave most of these. I just want to show a few at a time here. And then here's the markers. Now, it you could plot any point you want, but it helps if you pick one that's on the on the line. So, I just picked, you know, there's markers and then, you know, here's the brackets and everything you need args. So, for our equation, if x is three, y is six, that'll be on the line, and it's going to be a red circle for the point. And all the rest of these I kept the defaults. So let's just mark a few things. So we see the title shows up at the top. X and Y. There we go. And then here's our one point on the line. Um I also made the legend true instead of false. So this is the legend here. You see it shows x x^2 minus 3 and that's that's our blue line. So that's nice. And then I can go and let's change some things here. Um, so the X limb, let's just make it um, so square brackets and I'm going to make it from like negative 4 to 4. Now, I don't need to change the Y limb. Um, I c I can, but I I don't need to. Um, watch what happens when I do that, though. You see, it looks really stretched out because I didn't change the yim. And since I defined this point based on, you know, that's still on the graph because I didn't change the the y limit. So if I did, you know, cuz look at this this point here, it really, you know, it it's not very high up. So this is kind of wasted. So if I do then decide to change the Y limb and maybe I'll make that like negative one. I want to see a little bit below the axis instead of negative 1g5. Remember this is like your like ongoing um workbook here. So you know you're solving some things. You want to decide how you want to graph this, how you want to display it maybe to save it for something else. And you know you say okay I change this great. Oh but then I change that axis and then now you know this is all nice and zoomed in where we see like that demonstrates this nicely enough but now we can make some things you know I want to zoom in on this part where you know maybe I want to see a better look at where this crosses the x- axis here um or some other things. So, you know, that's really where x limb and y limb help you. Um, you know, change I could turn the axis off and just see the graph. Uh, for a lot of things that I like to do, that doesn't help as much. Um, annotations, the markers. Good. So, all these different things that we can do. I want to get into the one more thing that we can do. And this one, this was the shading. And up here that goes into uh fill which right here we have none. Where am I going to very often want to fill is if I have might recognize this equation that's my normal distribution. So let's say I have this is my equation and I just called this F because I used EQ up there. We'll just keep that. So there's my normal curve. And you can have you can connect all this with other code you already wrote. I just picked a value here just to put this here. But remember, you could have whatever else you did to find that critical value, you know, whether it be the integral or other things. And this is like I said that what other things you need to do if you're going to shade or fill under the graph. Um I needed to incorporate some numpy stuff because I need this array. There we go. So um I just knowing this um I picked the lower value of -4. um knowing that anything negative beyond that it you're not even going to notice it. So nplin space there we go I'm going to set up this array from -4 up to that critical x value and I want a th00and points here. So there you go. There's this array, this x array. And then this other twist. Um, if f is my function, my f array, I've been dealing with simp. But now I need to lambdify this so that I can get these y values here. So spi.lambdify for the variable x in function f across this x array. So that's kind of the thing, you know, here we go. So we have uh sixar array and then and then I'm going to plot um that function. There we go. My normal curve and then x remember not x limb. I really want those x values. Um and actually from uh -4 let's do that up to now that's you know that's nice. It actually si.pi works out just as just as well. But let's go up to four here. So I'm plotting this you know f function as across these x values. Now this is simply plotting it. But now when I do the fill equals this is now all that I set up here. Now this is what I have fill for my x value. I'm gonna use x array for my y value. I'm gonna use this f array. So it's filling in all those values. And then on color, I'm going to say blue just because I like blue. So all that goes in the curly braces for the fill. And then when I fill it now, we really get that there's my normal curve. Here's my critical value and I've shaded everything up to that value. Good times. So, these are so many of the different uh modifications you can do to a simp. And as you see all the different parameters you could you could add to it. you know, tinker with some things, but I wanted to highlight some of the ones that I feel like are the most useful going forward, especially relating to some of the stuff we've been doing in calculus. Let's talk about arc length, which is the length along this curve here. So, you know, not really the y-value, not the total yvalue, it's the length along the curve. So I, you know, I could have an even parabola here. And if I just talk about y-v values, maybe I end up at the, you know, the same y-value. That's no good. I want the the length if I were to actually draw along here along this curve. There we go. Um, if I if my curve was a full semicircle, then I could even do geometry to figure out that. But what happens if I have some curve, any curve, how do I get this length here? And let's think about this and kind of the distance formula um on a really small scale. This would be magnified on a really small scale here. So in whatever these xy values are, then what's my length here? It would be dx. And then what's my length here? That would be dy. And we'll do like a^2 + b²= c^2. You know, we'll call this c. Um then you know what do I have here? Uh so just like I have you know a^2 + b^2= c^2. Then what do I have? I have dx^2 + dy^ 2 = c^2. And if I take the square root, there we go. Now, that works. That's nice, especially if I have definite values here. But let's talk about the notation and link this up with uh calculus notation. And notice we were talking about, you know, slope as dy over dx derivative. We were talking about the rectangles dy * dx for the area integrals. And now we have dy dx pythagorean theorem. Are we going to use derivatives or integrals? We're going to end up using both. So let's start to talk about a little bit of notation here. Then um if I have so this this is my distance here. So if if I take this and underneath this square root if I multiply everything by dx^2 I'll move this over here by so multiply everything under this square root by dx^2 over dx^2. There we go. So the distribute this now times one. I didn't change the problem. But if I do this, so now I have this would be um I'll leave this out here. So that's going to be dx 2 times dx that denominator distributed becomes 1. dx^2 over dx2 is 1. And then over here it becomes dydx squared. So we see the distance here. And now if I leave that out here under the square root that comes out. And so what I'm going to write it as is um the square t of 1 + dydx squared all that times dx on the outside. And remember we're adding these up. So it becomes the integral of this from whatever points here you know from a to b. So then I get I get this you know integral and derivative. So the dydx is exactly the derivative. So when I whatever equation I get to define this line then you know I'll square it. I'll take the derivative of that and then square it and then then add one then square root and I'm doing the integral. So what's interesting is if it wasn't for this plus one then I would have the square square root would cancel derivative integral would cancel but it's it you know it would it's not going to be that easy. It's plus one in here. But you know, we're not just like putting the plus one to like make your life more difficult. The plus one comes from the fact that it is Pythagorean theorem and then this little factoring trick here to get this like that's really where the plus one comes from here. But you know that's where we get you know this is this is nice because we bring integrals derivatives theorem all good. Now, the good news, since we're going to be talking about doing this in code, if you were doing this like the the factoring way, um, it ends up being, uh, this plus one really throws it off and you, you know, you're like, "Oh, I I could have factored it so close. I can't It gets frustrating sometimes." Some things work out nicely, some things don't. writing it in the code. Hey, we could set this up and you know it it'll it'll work out because Simpai will do that uh solving for us. So that you know that that's really the nice advantage of doing everything in the code. A lot of things that would take a long time with factoring you know is one line of code and it'll happen right away. So there we go. That's what we get for the for the you know that that's You know that's how we get the integral and the and the derivative. It is the that whatever that original function is then the derivative squared + one square root of that and then the integral and that's and then we get the arc length the the length along that line. So let's take a look at how to do this with code. So with arclength as with some of the other formulas once we get know the formula we can just put it in code and set this up for future use. So all my usual imports and I'm going to use this range here because I'm going to use this equation simp<unk> 9 - x^ 2 and I'm using this because I'm picturing this as a part of a circle you know 1/4 of a circle there um in the first quadrant as x goes from 0 to three and that way in the back of my mind I can think about the geometry way to get this arc length and see if this works out, you know. So there we go. There's my equation. So what do I want to do? Remember that arclength formula. I first of all want the derivative. And I'm not plugging anything in yet. So I'm just going to put that there. The derivative deer. And I want the square root of the derivative squar + one. And I I like to put this in, you know, two steps here. So it it it it just works out that. So um there we go. So the derivative and remember it's the square of the derivative squar + one. I'll just call that equation two now because now for arc length and I'll just make that my variable. It's good to have variables that you know make sense. Simpi integrate. I want to integrate equation two from here to here. And like a lot of things I do with simp. So, I'm going to have it print out arclength here. And I also have this that I want it to be simp arc length. One of the other things that you can do is you can put this n first and then you can just have this as the last line. arc length because if it's the last line not not even within a print statement or anything it it it'll display nicely here. So if we run this then there we go and you see just to compare the different outputs here. All right this is nice and that tells me what I want to know. This is the decimal answer that I wanted. And also this is the simp answer that I wanted the exact answer 3 pi over 2 formatted nicely. So a lot of these remember you know you're using these as your calculator to give you information that you might need for something else. So hey have it output all the different versions of the answer you know which one do you want to use. So there we go. So there's arc length and then I can have the arc length you know now all I want to do is supposing I have the arc length of you know any other formula um you know x squared and here maybe I'll make it you know from like -3 to three so all along that parabola now here we might not see you know very interesting um how it prints out here but this is it. So what this is is this is the arc hyperbolic sign of six but again you see the exact answer formatted and the decimal answer which may often be what you want. If I want to calculate the surface area of a shape that I'm revolving around the x axis. So I drew this as if it were like y= x^2. That way it has that curve to it constantly, you know, complex enough that we can um illustrate the point here. So if I'm revolving this around and remember when we were working with volume, we imagine slicing this. So what am I doing with these here? If I want surface area, well, we have to think about the surface area of a cylinder. So supposing I had a cylinder and all of these I'm going to picture cylinders on the side. So if I have a cylinder, then how do I get the surface area of this? I can imagine this is like a can with a label that if I were to un roll it, that label is a rectangle. So, and yes, this is right from the horse's mouth, I'm telling you. So, how do I get the area of that rectangle? All right. Well, then it was going around in the circle. So, then I have the circumference of that is the length of my rectangle. And then the height is still the height of my rectangle. So, there we go. So if I have these rectangles here or these um yeah so it was rectangles when we were doing area and then we're slicing to make discs when we're doing volume and now it's surface area it's picturing the label of a can as um unrolling this here. So you know all these on the side unroll it to make a a can. So what's the setup here? I'm slicing it this way. And so if I have the surface area, so it's 2 pi r. So that's my circumference 2 pi r. and then times. Now, since it's on an angle here, it's kind of like if we were doing the surface area of a cone or something like that. Um, you know, I I can't just say the height because that would be misleading. You know, my height is going to get me dx, but I want I need that angle here. And in geometry times we call it L with like a little curse of L as that slant height because that's what we have here. Then 2 pi R is my circumference. But then um then I want that slant height. Well in calculus what's that slant height then? So if I have now if my I have I have my surface area here. So it's 2 pi and what's the radius is my it's my y value at any given point. And then what's this slant height here? Arc length. So then the ark length would be remember the square<unk> of 1 + the derivative squared. So it's the integral of this dx. So that's where we get this and it's the integral of all this. So we would have to we would put the y value here. I would put the integral symbol here. I could have put it right at the beginning, but actually that works out mathematically. Any constant I can bring out to the other side of the integral. So we get to this that that's where we get our slant height or you know our slant length here is our arc length. So then I have 2 pi times that y value at any given point. So that becomes the radius and then times the arc length here. And remember it's that formula here. Remember kind of like Pythagorean theorem. Um so then if I'm doing this in the code, which we're going to look at in a second, then once I define my y value, I'll set up a couple other steps. I'll take the derivative. I'll get the arc length and the arclength formula and then I'll I'll take it from there. But we see like the the the setup of this. Where does this come from? It's, you know, if I want this and I'm revolving this around the the x axis there, you know, 2 pi times the radius, which is my y value, and then that slant length. Because if I just called it dx and eliminated this, that's going to be not the exact answer. It's too much of an approximation because it's tilted. It's not just flat. no matter how small I I chop up these uh slices here. So I have to do arc length. So 2 pi times the y value for the radius times the arc length for this. And then when I take that integral from you know my x value from where to where then I can get the surface area. So now now that we can picture this, get a handle on on it, let's take a look at how to do all this in the code. Now looking at the code for this with surface area. Remember that, you know, now we have this set up. What do we want? We want that arc length times the circumference. So I'm going to just set this up so we can use this, you know, my lower bound and upper bound. I just decided to pick these and arc length times the circumference. So, we need to get that equation and I just put this here. We'll use that x squ. Um, and remember, yeah, the y value is the radius. We're revolving this around the x- axis. So, now I want that arc length equation. Call back to this. There we go. I'm going to take the derivative of this. Um, and then I'll just save that as the variable deer because then I want the square root of remember the arclength equation the square root of the derivative squar + one and I'll just call that equation two because we're you know we're building on this and then so take the derivative use that square root of the derivative squar + one and then now surface area we're going going to take the integral of that. And remember the surface area is uh just like the surface area of any cylinder. And remember we're doing all these cylinders. So 2 pi times the radius. So that's the circumference u times the arc length. And remember because it's not like a direct dx um as if I were doing slices. It's the arc length. It's the surface around it. So 2 pi * the radius times the arc length. So we did this uh and we're going to take the integral of that. So 2 pi times the radius which is equation one. That equation defines the radius times the arc length which we've built here as equation two. So now that's our uh full thing and we're going to take the integral of that as x goes from that lower bound to upper bound. And there we go. So now you know once you picture it, you know, we worked that out uh on the marker board. Now we know that formula we're just going to put that in. There we go. Um you know this is what it is. And then we match up everything that we have here revolving it around the x- axis. So I'm going to do a couple things here. This is the main answer that I want. I want that numerical answer. So we got the surface area and then I'm going to just going to convert it to a numerical answer. But I'm going to put these other two things in here. This is if I just use the print statement and then just do surface area. We'll see how that uh how Senpai displays that. And then after all that and this would have to be the last line of code. This is just the exact answer as Senpai interprets it. Yes. So I just wanted to show you these as comparison. You know this and the way this displays it and this which actually I guess would count as the exact answer. because we're not rounding anything. We're leaving square roots and arc sign and everything. But this is going to be the most practical answer that we want. There you go. So you have this set up that anytime you use surface area really if you say the lower bound and upper bound and then what's that equation that defines that you know revolved around the x- axis y value is the radius. get that equation that defines that boundary lower bound and upper bound and calculate surface area. Now, we're going to look at how we match up some of the simp outputs to the trig integrals on the table, just in case you say, "Wait, they don't match up. What can I do?" So, let's take a look at how we can do some factoring to match these up. So, simple for you and the factoring. So, you're not going to have to go through and memorize these. You know, a lot of calc textbooks will have a table of, I don't know, maybe even a hundred basic integral formulas. Simpai will do that for you, and that's what's great. Um, but some of the output from simples, yeah, it's going to give you what you expect. Power rule, your simp output, you're going to recognize that, no problem. du over u. So we have u could be x or could be some other inner function. Integral is the natural log things like that. E to the e to the x= e to the x e to the u. You know some of these you're going to be okay. Integral of s and cosine. That's great. And then you get down to some of these. But some of the trig stuff, it's like a simple factoring substitution. But what I'm going to look at is a few of these where you might remember, oh, a derivative of tangent is seeant squar. So when I do the integral, it goes back integral of seeant squar is tangent. And then we go you know integral of cosecant squar is negative integral of seeant tangent. So these few are just the direct you know, opposite of the derivative formulas that we remembered. But the way si will will do that especially because of the inverse functions or even tangent, we'll look at how the integral is going to be it'll it'll say s over cosine instead of tangent or co-angent will be cosine over s. See it will say one over cosine. And then we need to do those trig substitutions. So we'll look at a few of those you know some of these then integral of tangent integral of cootangent it becomes something totally different and we might need to do a couple more steps of substitution and then that's what we'll look at now the integral of tangent is what we're doing. So we didn't do a derivative formula that matches up with the integral of tangent in this case. Now log remember this is base e not the common log base 10. So negative log of the cosine of x. Now our table of integrals says that the integral of tangent should be the natural log of the absolute value of the secant. So let's see how those two will match up. So when the table says it should be the natural log of the secant absolute value of the seeant and si says negative log of the cosine are these the same? Yes. So let's do one thing. Let's move the negative here. Remember our logarithm rules. The negative here becomes an exponent here. So what's cosine to the -1 exponent? So that's the reciprocal. So that becomes the log of 1 over cosine which is secant. Remember natural log or log Simpai uses log meaning natural log. So that is the same si saying log is the natural log. And how do we account then for the absolute value? That's kind of a formality because it can't if if it's logarithm it can't be negative anyway. So you know writing the absolute value just kind of like highlights that. But there we go. moving the exponent and then rewriting one over cosine as seeant. We see how they match up and we get down to some of these. The integrals of these would be you know arc tangent arcant integral of sin squar we have secant squar and the integral of that should be tangent but it's output is s over cosine and we're going to say oh s over cosine is tangent and we see how that matches up and I put the negative here so the integral of negative coseant squar is again cosine over s trig substitution so we're going to say that matches up seeant tangent one over cosine well that's seeant derivative of seeant is seeant tangent. I also put in here that you know we have uh you know that we can take the derivative if we want. Um you see I can check this. Oh I output the integral here. What is the derivative of that? Again the way simp wants to put it there. S over cosine squar s over cosine is tangent and times one over cosine is secant. So that would be tangent. You know it gets back to it. So here's where we get to tangent. Senpai says the integral of co-angent should be the log of the s of x. SI says the integral of the secant of x should be this mess. And the table says the integral of secant is the natural log of seccant plus tangent. So, how does the table say natural log of secant plus tangent yet simp? And how could we say that these are the same? Well, the only way to show it is to dive into the factoring. Let's remember our exponent rules for logarithms that two and that negative there. So I'll just bring this here as -2 - one2 times the log of the sin of x -1. And then this one that two denominator becomes 12 times the log of the sin of x + one. And then that expon that multiplier becomes the exponent. So without rewriting all this, I'm going just going to move that that goes to the - one2. And then this one goes to the 12. And other logarithm rules, if I'm adding, what am I doing with these logarithms? I'm multiplying them. So this becomes one logarithm and since the 1/2 is the square root and negative would put this in the denominator. So when I multiply them it becomes the square root of this sin of x + one over this which is the sin of xus one. And it looks like we're not necessarily getting any closer, but we will. So, can I multiply this times something to factor this square root differently? Yes, I can. So, I'm just going to put a little side project up here that the sine of x -1 times the sin of x + 1 equ= sin^ 2 x - 1. And we remember that that sin squared is cosine squar sin^ square minus one is cosine squar. So that could become cosine squared which under the square root gets factored out as cosine. Um we'll come back to that in a second. So what about this? Let me move this up. So if I have this, let's within the square root multiply by that sine of x + one over itself. So I didn't change any of the values. I multiplied by one. It's just this over this. Anything over itself. But then in that denominator I have this sin minus one sin + one becomes sin^ squ minus one and remember pythagorean identity that that becomes cosine^ squar. Now that's underneath the square root. So then it becomes the log of it's still in the denominator though but that's what cosine in the denominator then becomes secant not in the denominator. So now that I factored that out, I have this which becomes sin of x + 1 squared. And you see since it's the same thing squared square root. So then I can simplify this. And if we distribute this secant * sin + 1 it becomes the log of so seeant maybe I will write this back as one over cosine because that's going to when I distribute that becomes s over cosine which is tangent and then plus one I think I will write that one back as seeant And remember si writes log but they mean natural log and then the because it's log the absolute value is implied because you couldn't get a negative number from that. So we really can factor all this and get it down to the natural log of seeant plus tangent. Simpai says the integral of the cosecant of x should be this mess and the table says the integral of coseant is the natural log of cosecant minusent. So after doing something like this with seccant cosecant you know you've got this. So the table says that the integral should be cose natural log of cose x minus cent x yet simp outputs this uh wacky logarithm. So we're going to go through some more drill that all this over two is a 1/2 multiplier. So it's 12 times uh the natural times the log which also remember simply calls it log but it's natural log the log of cosine of x - one minus and then we'll bring that up there 12 times the log of cossine x + 1. So then I'll make this our exponent again. That 1/2 becomes 1/2 as an exponent. And then I'll leave this minus here in the formula. And then I'll just put the 1/2. So when I'm subtracting logarithms, I divide. So then that just reminds me that that goes in the denominator. So it's the log of the square root of this cossine x - one over this cosine of x + 1 and we remember our side project here cosine of x + one if I did this cosine of x + 1 times cossine of x - one. So if I multiplied that whole denominator by this, then that would end up being cossine^ 2 - 1. And then remember our Pythagorean identity, that's really sin^ squar. So then sin squared, but it's in the denominator. I'll bring that out. Let's move all this up. So we'll keep this step to show then that under the square root I just multiplied by the same thing over itself. So it's like times one and then down here in the denominator then that's going to cancel out the sign squared which will factor out. And then up here then that's the same thing squared. So then I get the log of sin^ squar in the denominator. So I'll write it like this 1 over sin^ squar and then this is this same thing cosine of x - 1 squared because now it shows up twice but then square root. So then that cancels out that squar square root is cosine of x - 1 and when I distribute this oh this was sign don't mind me because it was sin squar under the square root. So now when I distribute this it becomes the log of cosine over s which is coangent and then minus 1 / s which is coseant and that should work minus coseant because if I if if I subtract them in the different order. I'll get the same number, but it'll be negative. But I know that only the absolute value is going to work. And that is the same. I just checked. So given the absolute value. So we see a few of these that we can factor and there's a few that then uh even built into the code uh collab or simp library will factor some of the some of the other ones and you can see that yes uh simp is not going to steer you wrong. It'll just give you a little bit different version sometimes that we can factor in and get the