yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Gradient and contour maps


5m read
·Nov 11, 2024

So here I want to talk about the gradient in the context of a contour map.

Let's say we have a multivariable function, a two-variable function, f of x, y, and this one is just going to equal x * y. So we can visualize this with a contour map just on the XY plane. What I'm going to do is I'm going to go over here, I'm going to draw my y-axis and my x-axis, right? So this right here represents X values, this represents y values, and this is entirely the input space. I have a video on contour maps if you are unfamiliar with them or are feeling uncomfortable.

The contour map for x * y looks something like this, and each one of these lines represents a constant value. So you might be thinking that you have, you know, let's say you want the constant value for f of x * y is equal to 2; that would be one of these lines. That would be what one of these lines represents. A way you could think about that for this specific function is you're saying, "Hey, when is x * y = 2?" and that's kind of like the graph y = 2/x, and that's where you would see something like this.

So all of these lines, they're representing constant values for the function. Now I want to take a look at the gradient field. The gradient, if you remember, is just a vector full of the partial derivatives of f. Let's just actually write it out: the gradient of f with our little del symbol is a function of x and y, and it's a vector-valued function whose first coordinate is the partial derivative of f with respect to x, and the second component is the partial derivative with respect to y.

So when we actually do this for our function, we take the partial derivative with respect to x. It takes a look; x looks like a variable, y looks like a constant. The derivative of this whole thing is just equal to that constant y, and then kind of the reverse for when you take the partial derivative with respect to y: y looks like a variable, x looks like a constant, and the derivative is just that constant x.

This can be visualized as a vector field in the XY plane as well. You know, at every given point x, y, so you kind of go like (2, 1), let's say. So that would be (2, 1). You would plug in the vector and see what should be output. At this point, the point is (2, 1), the desired output kind of shows that we’re looking somehow to draw the vector (1, 2). So you would expect to see the vector that has an x-component of 1 and a y-component of 2, something like that.

But it's probably going to be scaled down because of the way we usually draw vector fields. The entire field looks like this, so I'll go ahead and erase what I had going on since this is a little bit clearer. Remember, we scale down all the vectors, but color represents length. So red here is super long, blue is going to be kind of short.

One thing worth noticing: if you take a look at all of the given points around, if the vector is crossing a contour line, it's perpendicular to that contour line. Wherever you go, you know, you go down here, this vector is perpendicular to the contour line. Over here, perpendicular to the contour line, and this happens everywhere, and it's for a very good reason, and it's also super useful.

So let's just think about what that reason should be. Um, let's zoom in on a point. I'm going to clear up our function here, clear up all of the information about it, and just zoom in on one of those points. So let's say, like, uh, right here. I'll take that guy and kind of imagine zooming in and saying what's going on in that region.

So you've got some kind of contour line, and it's swooping down like this, and that represents some kind of value. Let's say that represents the value f = 2, and you know it might not be a perfect straight line, but the more you zoom in, the more it looks like a straight line.

When you want to interpret the gradient vector, if you remember in the video about how to interpret the gradient in the context of a graph, I said it points in the direction of steepest ascent. So if you imagine all the possible vectors kind of pointing away from this point, the question is which direction should you move to increase the value of f the fastest? There are two ways of thinking about that: one is to look at all of these different directions and say which one increases x the most.

But another way of doing it would be to get rid of them all and just take a look at another contour line that represents a slight increase, right? So let's say you're taking a look at a contour line, another contour line, something like this, and maybe that represents something that's right next to it, like 2.1. That represents, you know, another value that's very close.

If I were a better artist and this was more representative, this would be, it would look like a line that's parallel to the original one because if you change the output by just a little bit, the set of input points that look like it is pretty much the same but just shifted over a bit.

So another way we can think about the gradient here is to say of all of the vectors that move from this output of 2 up to the value of 2.1, you know, you're looking at all of the possible different vectors that do that. You know, which one does it the fastest? This time, instead of thinking the fastest as constant length vectors, what increases it the most will be thinking about constant increase in the output: which one does it with the shortest distance?

If you think of them as being roughly parallel lines, it shouldn't be hard to convince yourself that the shortest distance isn't going to be any of those; it's going to be the one that connects them pretty much perpendicular to the original line. Because if you think about these as lines, and the more you use them, the more they pretty much look like parallel lines, the path that connects one to the other is going to be perpendicular to both of them.

So because of this interpretation of the gradient as the direction of steepest ascent, it's a natural consequence that every time it's on a contour line, wherever you're looking, it's actually perpendicular to that line. Because you can think about it as getting to the next contour line as fast as it can, increasing the function as fast as it can.

This is actually a very useful interpretation of the gradient in different contexts, so it's a good one to keep in the back of your mind: the gradient is always perpendicular to contour lines. Great! See you in the next video.

More Articles

View All
Lecture 1 - How to Start a Startup (Sam Altman, Dustin Moskovitz)
Welcome. Um, can they turn this on? Maybe all right. Uh, people here in the back, can you guys hear me? Is the mic on? No? Uh, maybe you can ask them to turn it on. Maybe we can get a bigger—ah, there we go. All right. Maybe we can get a bigger auditorium…
iPhone 4 is for LOSERS? -- Wackygamer
Today was Thursday. We’re recording this Thursday, June 2nd. Exciting part: iPhone 4 launch! That’s not exciting. I went 7:00 a.m. this morning. You’re pathetic! No, I’m not. What the point of getting iPhone 4? Ione, what’s what’s what’s different with …
Mozart Helps Me Think | Genius
[music playing] [glass hitting metal] [light scraping sound] [violin playing] [LIGHT CRASH OF GLASS INTO METAL] [footsteps] [side conversation] ALBERT: Miss Maric. MILEVA: Are you under the impression you’re the only one in the building? ALBERT: …
Entering a Salmon Graveyard | The Great Human Race
Getting deeper, huh? 5,000 years ago in the Pacific Northwest, the seasonal salmon runs sustained huge populations of early humans. Oh, is that a dead fish? But this bounty was only available for a short window of time each year. Look, there’s even skin e…
Biohacking our way to health with robot cells | Michael Levin
The modern assumption of regenerative medicine is that it’s a clockwork. So if you have a gene regulatory network or a pathway, the only way you’re gonna get it to do something different is through bottom-up rewiring. But if you’re willing to entertain th…
Auckland Clip 5: On Starting a Political Career
What would you say to a young man looking to get into politics? Well, the first thing I would say is it’s a mistake to be fatally cynical about politics because, for all the reasons there are to be fatally cynical about politics, there’s much more reason…