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
Improving Life with Exoskeleton Technologies | Breakthrough
Exoskeleton Technologies is a program where we’re working on developing exoskeletons for different applications. National Geographic contacted us about participating in their breakthrough series on a show called “More Than Human.” They asked us to bring F…
Q&A with YC at the Female Founders Conference 2016
Okay, so the first question that someone asked was: How does a company determine if they’re right for YC? If they should apply? Well, every startup’s right for Y Combinator. I mean, I think a lot of people say, like, “Oh, I’m too early” or “I’m too far al…
Interpreting definite integral as net change | AP Calculus AB | Khan Academy
In a previous video, we start to get an intuition for rate curves and what the area under a rate curve represents. For example, this rate curve might represent the speed of a car and how the speed of a car is changing with respect to time. This shows us t…
Angela Duckworth talks about helping children develop grit and resiliance | Homeroom with Sal
Hi everyone! Welcome to the daily homeroom live stream style here from Khan Academy. For those of you all who are new to this, this is a live stream that we’ve been doing every day since we’ve had these global school closures, just as a way to stay connec…
From the Frontlines to the Shorelines | Podcast | Overheard at National Geographic
[Music] Now for the marine forecast for Waters within five nautical miles from shore on Western Lake Superior, from Fort Wayne to Bayfield to Saxon Harbor, Wisconsin, and the outer Apostle Islands. It’s summer 2021, time of this radio broadcast. National…
Is This the End of Cathie Wood? | ARKK Fund Collapsing
One of the new stars in the investment world over the past few years has been Kathy Wood. She has had a successful and established career on Wall Street but really became a household name relatively recently with the company she founded, Arkhanvest, and i…