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
How to Focus to Change Your Brain
Welcome to the Huberman Lab Podcast where we discuss science and science-based tools for everyday life. [upbeat music] My name is Andrew Huberman and I’m a professor of Neurobiology and Ophthalmology at Stanford school of medicine. This podcast is sepa…
The Problem With Elon Musk
Uh, I mean, my mind is a storm. I don’t think most people would want to be me. They may think they would want to be me, but they don’t. They don’t know—is your storm a happy storm? No. I’ve grown tired of hearing the name Elon Musk and not really understa…
Photos: When Food Prices Go Up, What Happens? | Nat Geo Live
We are now 7.3 billion fellow human beings, on the only place we can live, and in the next twenty-five years, we’re going to be 9 billion fellow human beings with no other place to go. I went to Egypt. Right before the landscape of the Great Pyramids of …
Return from the Okavango in 360 - Ep. 4 | The Okavango Experience
This is a place filled with magic. Everything has to have a reason: the movement of the grass, the core of an animal, the call of a bird. Everything, we’re connected to all of it, and that is the most alive we can ever be. This is a sanctuary for these in…
HOW TO CONTROL YOUR ANGER - SENECA | STOICISM INSIGHTS
Have you ever experienced a surge of anger so intense it felt like a volcanic eruption within you? Picture this anger burning with the fury of a thousand suns. But what if I told you there’s a way to extinguish it? Not tomorrow or next week, but today. We…
Properties perserved after rigid transformations
What we’re going to do in this video is think about what properties of a shape are preserved or not preserved as they undergo a transformation. In particular, we’re going to think about rotations and reflections. In this video, both of those are rigid tra…