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
Behind the Scenes: Documenting the Elusive Florida Panther | National Geographic
Foreign and that’s how you test. I don’t think I had any idea what I was getting into at the beginning of this project. I’ve only seen a Florida panther twice with my own eyes. The animals that we’re trying to film and photograph are super elusive. There’…
Homeroom with Sal & Mala Sharma - Wednesday, May 5
Hi everyone! Sal Khan here from Khan Academy. Welcome to the homeroom live stream. A very exciting conversation today! We have Mala Sharma, who is the VP and GM of Creative Cloud at Adobe. But before we jump into that conversation, I will give some of our…
Loneliness
Everybody feels lonely from time to time. When we have no one to sit next to at lunch, when we move to a new city, or when nobody has time for us at the weekend. But over the last few decades, this occasional feeling has become chronic for millions. In th…
Ray Dalio on his Principles for Success
Can you think about, like, what are some of your first principles? Life is a journey. It’s an adventure, an adventurous journey in which you come into it with having a certain nature. That nature, um, you’re in a journey to find the path to match up with …
Flamingo Breeding | Flamingo Dads Adopt an Egg | Magic of Disney's Animal Kingdom
Down by the tree of life lives a haunting flock of pure blankness. I’m coming to check on our lesser flamingos. These guys are from Africa. Hi, guys. Good morning. How are you doing? Hi, everybody. It’s egg-laying season for the lesser flamingos. And the…
Solving quadratics by taking square roots examples | High School Math | Khan Academy
So pause the video and see if you can solve for x here. Figure out which x values will satisfy this equation. All right, let’s work through this, and the way I’m going to do this is I’m going to isolate the (x + 3) squared on one side. The best way to do …