[music]. Let's pretend that you're a shepherd and you want to build a fence for your sheep. Specifically, let's say that you've got 52 meters of fencing. And you're committed to building a rectangular pen for your sheep next to your barn. So how big of a sheep pen can you build with that much fencing? Process is a standard one, it's got five steps. The first thing I'm going to do is to draw a picture of the situation. With a picture in hand and everything labelled with variables. I can write down the thing I'm tying to optimize, the goal. With that, I can also write down the constraints, right? Not every combination of variables maybe make sense. Maybe some domain issues to consider as well. Then in step four, I'm going to solve my goal for a single variable. And once I get a function of a single variable, I can apply calculus. Right? This last step really means to say differentiate the function of a single variable, find the critical points, figure out where the largest and smallest values are. That's my five-step process. First, I draw a picture, so let's rearrange this in a slightly nicer picture. I'll build my little sheep pen here. Here's my rectangular pen for my sheep that I built next to my barn. And I'd better label the side lengths here. The top and bottom will be x meters long and this side here will be y meters long. Now, what is it that I'm trying to maximize? Well, I want the fenced in area here to be as big as possible. So the thing I'm trying to maximize is the area of this pen and that area is x times y. Now at this point, you probably think, well I know how to maximize that function. I'll just pick x and y to be absolutely enormous. And if an enormous number is multiplied by an enormous number, if I multiply x times y, the output will be enormous. Right, I'll build a sheep pen the size of the Milky Way. But there's a constraint. Yeah, I mean, I can't simply build this fence as big as I like, because I've only got 52 meters of yellow fence to begin with. So the constraint is that the length of this fence, which is 2x plus y is 52. I'm saying equal 52, because I might as well use all of the fence that I have to build this sheep pen. Another way to say that, is that this is what I'm trying to do. I'm trying to maximize this quantity, the area of the pen, subject to the constraint that 2x plus y, the length of fence in my pane is 52 meters and I should say that x and y are nonnegative quantities. It doesn't make any sense to make one side of my sheet pane negative. Now, I use to constraint to solve the thing I'm trying to optimize for a single variable. Okay. So I'm going to solve this for a single variable. I've got that 2x plus y is 52, so I could solve for y, y is 52 minus 2x. The quantity that I'm trying to maximize is x times y, but if y is 52 minus 2x, then, I'm really trying to maximize x times 52 minus 2x, because this quantity is y. So this Is the function of a single variable that I'm trying to maximize. Okay, fantastic. Now, I've got a function of one variable so I can apply calculus. I can differentiate this thing and search for the critical points. So let's call this a function f. So f of x is x times 52 minus 2 x, that dot is multiplication. I could distribute this and write f is 52 times x minus 2 x squared. Now, I can calculate the derivative, the derivative of f is the derivative of 52x minus the derivative of 2x squared. The derivative of 52x is just 52. The derivative of 2x squared is 4x. So, the derivative of f is 52 minus 4x. The critical points are places where the function's derivative vanishes or the function is not differentiable. This function is polynomial, so it's differentiable everywhere. The only thing I have to worry about then, for critical points, is when this derivative is equal to zero. So for what values of x is that equal to zero? Well, the only place where this derivative is equal to zero is when x equals 13. I should also remember that I'm maximizing this function subject to this constraint and there were also some domain issues. I don't want x to be negative. And, in order to ensure that y is nonnegative, if 2x plus y is 52, then x can't be negative, but x also can't be bigger than 26. So, what's the situation here? Well, I can summarize it. My critical points. Well, there's really only one critical point and the critical point is when x is equal to 13. Then I've also got some endpoints, right? The endpoints to consider are when x is equal to 0 or when x is equal to 26. So these are the three points to check. Now, I just have to figure out which of these values results in the best fence. All right. So these are the three points that I should check, so I'll make a little table here. The x values that I'll check are 0, 26, and 13. And I'll figure out what the corresponding y value is and then I'll figure out x times y, which will be the area of the fenced in region if I use these as my side lengths for my sheep pen. So, let's see. If x is equal to 0 and I satisfy this constraint that 2x plus y is 52. If x is equal to zero, then y must be 52. And if x is 26, then 2 times 26 plus what gives me 52. Well, then y is equal to 0. And if x is 13, then 2 times 13 is 26, plus what gives me 52? Well, then y must be 26. Now, in each of these three cases, I can figure out what the area of the resulting sheep pen would be. 0 times 52 is just 0 and 26 times 0 is also 0. 13 times 26 is 338. So here, this is the best choice for my sheep pen. This isn't just a great calculation. I hope that, intuitively, this method really makes sense. I mean, look, if x is equal to 0, that means I'm building my sheep pen just entirely up against the side of the barn. And if y equals 0, well, that means I'm building my sheep pen like this. I'm just building fence that goes over and then fence that comes right back. Again, there's no area that this so-called pen is enclosing. That x equals 13 and y equals 26 is the best choice for our sheep is clear if you think about how wiggling x would affect the area of the sheep pen. So here is the best solution, right, to have x be 13 and y be 26. What if I were to push this fence in just a little bit, all right? I'd make the x values a little bit smaller and then make the y value a little bit bigger to compensate, right? Maybe x is 12 and y is 28. Why is this worse? Well, think about what happens when I change from x equals 13 to x equals 12. I give up this region here and that's 26 square meters of fenced in region. I gain this region up here and this region up here, but this region up here is 12 square meters and this region here is 12 square meters. So I'm giving up 26 square meters to gain 24 square meters. That's a bad deal. This is really the whole crux of the matter. Right? When you found the best thing, small changes to the best thing, don't make it better. But if you hadn't found the best thing, then you could make a small change that would make it better. That's the whole idea, right? That understanding how functions change can be used to understand the best output for a function.