College Calculus – Full Course with Python Code

freeCodeCamp.org · Intermediate ·🔢 Mathematical Foundations ·1y ago

Key Takeaways

This video course covers college Calculus with Python code, including limits, derivatives, and graphing functions, with a focus on practical implementation using Python libraries like sympy and matplotlib.

Full Transcript

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 i

Original Description

Learn college Calculus from an experienced university mathematics professor. You will also learn how to implement all the Calculus concepts using the Python programming language. ✏️ Ed Pratowski developed this course for freeCodeCamp.org. ⭐️ Code Links ⭐️ 1 - Limits and Slope https://colab.research.google.com/drive/1hKKLXzWUtLj_5BjsQpnceKtLAxzZeIkn?usp=sharing 2 - Derivatives https://colab.research.google.com/drive/12n9UZphbfC9tVhoy8QWkeSOhkDjmlh1f?usp=sharing 3 - Graphing Derivatives https://colab.research.google.com/drive/1zdyoccDiQmYnHMfChB8EWY6NqPE10O2J?usp=sharing 4 - Related Rates https://colab.research.google.com/drive/19q4nRpqpBzwZ2Qs7heHSCXHHIPyDpm6R?usp=sharing 5 - Integrals https://colab.research.google.com/drive/15KmbeugtGsxMUiGTg3Wn-fQDgIOK2KAZ?usp=sharing ❤️ Support for this channel comes from our friends at Scrimba – the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp ⭐️ Contents ⭐️ ⌨️ (0:00:00) Intro: Calculus with Python ⌨️ (0:01:51) Limits: Hole in the Graph ⌨️ (0:06:51) Limits: Asymptotes ⌨️ (0:14:08) Limits: Graphing ⌨️ (0:32:36) Limits and Slope ⌨️ (0:44:41) Slope and the Derivative ⌨️ (0:54:46) Derivatives and Calculus ⌨️ (1:04:53) Chain Rule ⌨️ (1:12:27) Product Rule ⌨️ (1:29:42) Implicit Differentiation ⌨️ (1:45:07) Multiple Derivative Steps ⌨️ (1:57:43) Derivative Example ⌨️ (2:06:10) Financial Applications ⌨️ (2:23:06) Projectile Motion ⌨️ (2:31:52) Derivatives and Differentials ⌨️ (2:36:53) Tangent Lines ⌨️ (2:43:03) Parametric Equations ⌨️ (2:54:20) Related Rates: Ladder Sliding ⌨️ (3:12:01) Related Rates: Balloon Volume ⌨️ (3:23:13) Mean Value Theorem ⌨️ (3:29:03) Rolles Theorem ⌨️ (3:33:19) Riemann Sums: Area Under a Curve ⌨️ (3:46:06) Summation and the Integral ⌨️ (3:55:14) Fundamental Theorem of Calculus ⌨️ (4:02:08) Area Above and Below the Axis ⌨️ (4:10:19) Area Between Curves ⌨️ (4:19:05) Volume Revolved Around X ⌨️ (4:33:56) Volume of a Hollow Shape ⌨️ (4:42:57) Volume Revolved Aro
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60

← Previous Next →
1 React: Production Server Setup Part 2 - Live Coding with Jesse
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
2 cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
3 Browser history tutorial - Beau teaches JavaScript
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
4 Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
5 React: Parameterized Routing with Next.js - Live Coding with Jesse
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
6 React: Dealing with jQuery Issues - Live Coding with Jesse
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
7 setInterval and setTimeout: timing events - Beau teaches JavaScript
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
8 Browser and Device Testing - Live Coding with Jesse
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
9 Last Minute Updates - Live Coding with Jesse
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
10 Post Launch Updates - Live Coding with Jesse
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
11 React: Setting Up Google Analytics - Live Coding with Jesse
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
12 React: Masonry Layout - Live Coding with Jesse
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
13 Load Balancing Digital Ocean Droplets - Live Coding with Jesse
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
14 try, catch, finally, throw - error handling in JavaScript
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
15 Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
16 Graphs: breadth-first search - Beau teaches JavaScript
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
17 React: Masonry Layout Part 2 - Live Coding with Jesse
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
18 React: WordPress API Live Search - Live Coding with Jesse
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
19 Creating WordPress Custom Post Types - Live Coding With Jesse
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
20 Dates - Beau teaches JavaScript
Dates - Beau teaches JavaScript
freeCodeCamp.org
21 Miscellaneous Front End Updates - Live Coding with Jesse
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
22 Merging a Pull Request from GitHub - Live Coding with Jesse
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
23 React + Prettier + Standard JS - Live Coding with Jesse
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
24 React: Sortable Responsive Table - Live Coding with Jesse
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
25 Geolocation Sorting by Distance - Live Coding with Jesse
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
26 Tradeoff Matrix - Agile Software Development
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
27 The Definition of Ready - Agile Software Development
The Definition of Ready - Agile Software Development
freeCodeCamp.org
28 Getting first React job without experience - Ask Preethi
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
29 React: Google Analytics Click Tracking - Live Coding with Jesse
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
30 Submitting a PR to an Open Source Project - Live Coding with Jesse
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
31 Should I go back to school to get CS degree? - Ask Preethi
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
32 Hero Section CSS Changes - Live Coding with Jesse
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
33 Working Agreement - Agile Software Development
Working Agreement - Agile Software Development
freeCodeCamp.org
34 A day at Pennybox with Co-Founder Reji Eapen
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
35 React: Sorting and Filtering Data - Live Coding with Jesse
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
36 React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
37 React: Building a New UI - Live Coding with Jesse
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
38 Definition of Done - Agile Software Development
Definition of Done - Agile Software Development
freeCodeCamp.org
39 Getting started with jQuery (tutorial) - Beau teaches JavaScript
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
40 Making a React Blog with WordPress Content - Live Coding with Jesse
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
41 React, NextJS, CSS - Live Coding with Jesse
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
42 jQuery events - Beau teaches JavaScript
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
43 React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
44 React: Working with API Data - Live Coding with Jesse
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
45 React: Refactoring Components - Live Streaming with Jesse
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
46 jQuery effects - Beau teaches JavaScript
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
47 More React Refactoring - Live Coding with Jesse
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
48 animate in jQuery - Beau teaches JavaScript
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
49 "Finishing" My React Site - Live Coding with Jesse
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
50 Starting a New React Project (P2D1) - Live Coding with Jesse
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
51 React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
52 The Agile Manifesto - Agile Software Development
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
53 jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
54 React Project 2 Day 3 - Live Coding with Jesse
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
55 The INVEST approach to product backlog items
The INVEST approach to product backlog items
freeCodeCamp.org
56 React Project 2 Day 4 - Live Coding with Jesse
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
57 Chickens and Pigs - Agile Software Development
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
58 React Project 2 Day 5 - Live Coding with Jesse
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
59 jQuery: add and remove DOM elements - Beau teaches JavaScript
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
60 React Project 2 Day 6 - Live Coding with Jesse
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org

This video course teaches college Calculus with Python code, covering topics like limits, derivatives, and graphing functions, with a focus on practical implementation using Python libraries.

Key Takeaways
  1. Import necessary Python libraries like sympy and matplotlib
  2. Define functions and calculate limits and derivatives
  3. Graph functions using matplotlib
  4. Use Google Collab to run and visualize code
  5. Apply the power rule and sum rule for differentiation
💡 The power rule and sum rule can be used to find the derivative of a polynomial, and Python libraries like sympy and matplotlib can be used to implement and visualize calculus concepts.

Related Reads

Up next
Marks Weightage | Quantitative Aptitude CA Foundation September 2026 | ABC Analysis | Nithin
ArivuPro Academy
Watch →