1 00:00:00,000 --> 00:00:00,710 2 00:00:00,710 --> 00:00:04,269 In the last video we had a three-dimensional surface, 3 00:00:04,269 --> 00:00:08,169 where the height z was a function of x and y. 4 00:00:08,169 --> 00:00:10,759 And it gave us surface in three-dimensional space. 5 00:00:10,759 --> 00:00:14,699 Now let's try to get our heads around what the gradient 6 00:00:14,699 --> 00:00:19,339 of a function of three variables looks like. 7 00:00:19,339 --> 00:00:22,920 So the easiest one for me to imagine is a scalar field. 8 00:00:22,920 --> 00:00:24,440 So what's a scalar field? 9 00:00:24,440 --> 00:00:27,920 One that I find fairly intuitive is temperature in 10 00:00:27,920 --> 00:00:29,140 a three-dimensional room. 11 00:00:29,140 --> 00:00:34,140 So let's say the temperature in a room is a function of 12 00:00:34,140 --> 00:00:35,700 where I am in the room. 13 00:00:35,700 --> 00:00:42,670 So let's say it's a function of my x, y, and z coordinates. 14 00:00:42,670 --> 00:00:45,340 And I don't know, I have never actually modeled temperature. 15 00:00:45,340 --> 00:00:50,330 But let's say I have, I don't know, a 20 kelvin-- actually, 16 00:00:50,329 --> 00:00:52,289 let me make it so that our vector field works out right. 17 00:00:52,289 --> 00:00:54,460 Let's say we have a 10 kelvin heat force in 18 00:00:54,460 --> 00:00:57,950 the center of our room. 19 00:00:57,950 --> 00:01:00,650 I can imagine as you go further and further away from that 20 00:01:00,649 --> 00:01:02,600 heat source it's going to get colder and colder. 21 00:01:02,600 --> 00:01:04,939 So let's say that the temperature function. 22 00:01:04,939 --> 00:01:07,810 And let's say that center of the room is at the coordinates 23 00:01:07,810 --> 00:01:09,240 x, y, and z is equal to 0. 24 00:01:09,239 --> 00:01:11,170 So let's say our temperature function-- I'm just making this 25 00:01:11,170 --> 00:01:23,879 up, I don't know if this is an accurate model of temperature-- 26 00:01:23,879 --> 00:01:31,699 it's equal to 10 times e to the minus r squared. 27 00:01:31,700 --> 00:01:32,939 Now why did I say r? 28 00:01:32,939 --> 00:01:34,259 I said it's a function of x, y, and z. 29 00:01:34,260 --> 00:01:39,350 Well I'm just saying that it exponentially decays as you 30 00:01:39,349 --> 00:01:41,559 get further and further away from that source. 31 00:01:41,560 --> 00:01:44,200 Kind of radially further and further away from that source. 32 00:01:44,200 --> 00:01:46,439 So what's the radial distance away? 33 00:01:46,439 --> 00:01:48,310 And this actually isn't that relevant to learning gradients, 34 00:01:48,310 --> 00:01:50,320 but let's get a little intuition about what that 35 00:01:50,319 --> 00:01:54,229 actual temperature function-- how it actually changes as 36 00:01:54,230 --> 00:01:56,210 you go through the room. 37 00:01:56,209 --> 00:02:01,030 So the radius away from the center, that's just going to be 38 00:02:01,030 --> 00:02:06,030 r squared is just x squared plus y squared plus z squared. 39 00:02:06,030 --> 00:02:09,069 That's just the Pythagorean theorem in three dimensions. 40 00:02:09,069 --> 00:02:10,549 So let's write our temperature function. 41 00:02:10,550 --> 00:02:18,840 So let's write temperature as a function of x, y, and z is 42 00:02:18,840 --> 00:02:29,990 equal to 10 e to the minus x squared plus y squared plus z 43 00:02:29,990 --> 00:02:33,370 squared-- which is exactly what I wrote up here. 44 00:02:33,370 --> 00:02:35,789 Instead of x squared plus y squared plus z squared, I wrote 45 00:02:35,789 --> 00:02:38,129 r squared, just to kind of give you the intuition that this 46 00:02:38,129 --> 00:02:41,219 expression is just saying the square of the distance as we 47 00:02:41,219 --> 00:02:45,009 get away from the center of our room, or from the 48 00:02:45,009 --> 00:02:46,989 coordinate 0, 0, 0. 49 00:02:46,990 --> 00:02:48,310 But that's not what we're learning here. 50 00:02:48,310 --> 00:02:50,879 But I want you to understand, at least conceptualize this, 51 00:02:50,879 --> 00:02:53,349 it's hard to draw a scalar field. 52 00:02:53,349 --> 00:02:57,460 All a scalar field means is that in any point in this 53 00:02:57,460 --> 00:02:59,969 base-- and in this case we're dealing with three-dimensional 54 00:02:59,969 --> 00:03:05,039 space-- at any point in that space we can associate a value. 55 00:03:05,039 --> 00:03:05,840 And that makes sense. 56 00:03:05,840 --> 00:03:08,400 If you were to take a thermometer and measure any 57 00:03:08,400 --> 00:03:11,180 point in space in the room that you're in right now, 58 00:03:11,180 --> 00:03:12,540 you would get a temperature. 59 00:03:12,539 --> 00:03:14,590 You wouldn't get a temperature and a direction, so it's 60 00:03:14,590 --> 00:03:16,250 not a vector field. 61 00:03:16,250 --> 00:03:17,530 You would just get a temperature. 62 00:03:17,530 --> 00:03:19,509 And that's why it's called a scalar field. 63 00:03:19,509 --> 00:03:21,090 Associated with every coordinate is just 64 00:03:21,090 --> 00:03:22,610 a temperature. 65 00:03:22,610 --> 00:03:27,890 So how would we view the gradient of this function? 66 00:03:27,889 --> 00:03:30,719 Well the gradient of this function is going to tell us in 67 00:03:30,719 --> 00:03:33,289 which direction-- and actually, the gradient of this function 68 00:03:33,289 --> 00:03:36,209 is going to generate a vector field, because it's going to 69 00:03:36,210 --> 00:03:39,990 tell us in which direction do we have the largest 70 00:03:39,990 --> 00:03:41,580 increase in temperature. 71 00:03:41,580 --> 00:03:44,660 And also, the magnitude of those vectors in that vector 72 00:03:44,659 --> 00:03:47,229 field will tell us how large of an increase in temperature 73 00:03:47,229 --> 00:03:48,319 we are looking at. 74 00:03:48,319 --> 00:03:52,909 Or you can kind of view it as almost a 75 00:03:52,909 --> 00:03:55,219 three-dimensional slope. 76 00:03:55,219 --> 00:03:56,189 Hope that doesn't confuse you. 77 00:03:56,189 --> 00:03:59,259 So let's compute the gradient, and then I'll show you a 78 00:03:59,259 --> 00:04:02,629 diagram that might make things a little bit more intuitive. 79 00:04:02,629 --> 00:04:07,210 Let me erase this thing down here. 80 00:04:07,210 --> 00:04:09,480 And I'm going to switch from this blue color, because 81 00:04:09,479 --> 00:04:14,590 it's a little nauseating. 82 00:04:14,590 --> 00:04:22,540 So the gradient of T is going to be equal to the partial 83 00:04:22,540 --> 00:04:28,490 derivative T with respect to x times the unit vector in the x 84 00:04:28,490 --> 00:04:33,550 direction, plus the partial derivative of the temperature 85 00:04:33,550 --> 00:04:38,939 function with respect to y times the unit vector in the y 86 00:04:38,939 --> 00:04:43,740 direction, plus the partial derivative of the temperature 87 00:04:43,740 --> 00:04:48,829 function with respect to z times the unit vector 88 00:04:48,829 --> 00:04:50,079 in the z direction. 89 00:04:50,079 --> 00:04:52,180 And now we just plug and chug and figure out the 90 00:04:52,180 --> 00:04:54,129 partial derivatives. 91 00:04:54,129 --> 00:05:00,469 So the gradient of T is equal to-- now you might be daunted. 92 00:05:00,470 --> 00:05:05,250 Oh, I have an e to this three variable function, how do I 93 00:05:05,250 --> 00:05:06,050 take the partial derivative? 94 00:05:06,050 --> 00:05:08,500 Remember, if you're taking the partial derivative with respect 95 00:05:08,500 --> 00:05:12,139 to x you just pretend like the y's and the z's are constants. 96 00:05:12,139 --> 00:05:14,360 So let's do that. 97 00:05:14,360 --> 00:05:19,500 So let's take the derivative of the inside function. 98 00:05:19,500 --> 00:05:20,089 That's the way I view it. 99 00:05:20,089 --> 00:05:22,869 So minus x squared plus y squared plus z squared, 100 00:05:22,870 --> 00:05:24,439 with respect to x. 101 00:05:24,439 --> 00:05:26,810 So you could distribute this minus if you like. 102 00:05:26,810 --> 00:05:28,980 So it'd be minus x squared minus y squared 103 00:05:28,980 --> 00:05:30,850 minus z squared. 104 00:05:30,850 --> 00:05:33,890 So the derivative of that with respect to x is just going to 105 00:05:33,889 --> 00:05:36,714 be-- these are just constants, so the derivative with 106 00:05:36,714 --> 00:05:38,179 respect to x is just 0. 107 00:05:38,180 --> 00:05:40,590 So the derivative is minus 2x. 108 00:05:40,589 --> 00:05:41,859 Right? 109 00:05:41,860 --> 00:05:45,670 Minus 2x is the derivative of minus x squared. 110 00:05:45,670 --> 00:05:50,330 Minus 2x times the derivative of the outside. 111 00:05:50,329 --> 00:05:52,509 Well, what's the derivative of e to the x? 112 00:05:52,509 --> 00:05:55,099 The derivative of e to the x is e to the x. 113 00:05:55,100 --> 00:05:57,990 That's why e is such an amazing number. 114 00:05:57,990 --> 00:06:00,810 And this 10 here, this is just a constant that when you take 115 00:06:00,810 --> 00:06:05,319 the derivative of a constant times something the 116 00:06:05,319 --> 00:06:06,699 constant carries over. 117 00:06:06,699 --> 00:06:11,370 So the derivative of the outside expression, the way I 118 00:06:11,370 --> 00:06:17,810 imagine it, is equal to 10 e to the minus x squared plus 119 00:06:17,810 --> 00:06:21,860 y squared plus z squared. 120 00:06:21,860 --> 00:06:27,280 And then all of that times the unit vector in the i direction. 121 00:06:27,279 --> 00:06:29,599 Right? 122 00:06:29,600 --> 00:06:34,040 And now we can do the same thing for the y direction. 123 00:06:34,040 --> 00:06:35,790 So plus-- what's the partial derivative of 124 00:06:35,790 --> 00:06:36,760 this with respect to y? 125 00:06:36,759 --> 00:06:37,819 Well it's going to look very similar. 126 00:06:37,819 --> 00:06:39,802 The partial derivative of this inner function with respect 127 00:06:39,802 --> 00:06:42,500 to y, it's minus y squared. 128 00:06:42,500 --> 00:06:43,250 So it's minus 2y. 129 00:06:43,250 --> 00:06:46,670 130 00:06:46,670 --> 00:06:48,319 And then the derivative of the whole thing is 131 00:06:48,319 --> 00:06:50,680 just itself again. 132 00:06:50,680 --> 00:06:55,680 So times 10 e to the minus x squared plus y 133 00:06:55,680 --> 00:06:58,180 squared plus z squared. 134 00:06:58,180 --> 00:07:01,660 And then all of that times the unit vector in the 135 00:07:01,660 --> 00:07:04,920 y direction times j. 136 00:07:04,920 --> 00:07:10,370 And then finally, the partial derivative of the temperature 137 00:07:10,370 --> 00:07:12,139 function with respect to z. 138 00:07:12,139 --> 00:07:23,349 And that's just minus 2z times 10 e to the minus x squared 139 00:07:23,350 --> 00:07:25,500 plus y squared plus z squared. 140 00:07:25,500 --> 00:07:26,620 This is just the chain rule. 141 00:07:26,620 --> 00:07:28,689 And I'm treating the other two variables that I'm not taking 142 00:07:28,689 --> 00:07:31,870 the partial derivative with respect to, as constants. 143 00:07:31,870 --> 00:07:37,430 And then all of that times the unit vector in the k direction. 144 00:07:37,430 --> 00:07:39,800 And we could simplify this a little bit. 145 00:07:39,800 --> 00:07:42,040 You could have minus 2x times 10. 146 00:07:42,040 --> 00:07:43,910 That's minus 20x. 147 00:07:43,910 --> 00:07:44,750 Let me write it up here. 148 00:07:44,750 --> 00:07:49,740 So the gradient of the temperature function is equal 149 00:07:49,740 --> 00:07:58,120 to minus 20 e to the minus x squared plus y squared-- you 150 00:07:58,120 --> 00:08:07,970 probably can't read this-- plus z squared, times i minus 20y. 151 00:08:07,970 --> 00:08:09,860 And actually, I'm not going to go into that, because I realize 152 00:08:09,860 --> 00:08:10,819 I'm running out of time. 153 00:08:10,819 --> 00:08:14,879 I think you can simplify this algebraically. 154 00:08:14,879 --> 00:08:18,370 But anyway, the more important thing is I always find with 155 00:08:18,370 --> 00:08:19,980 gradients it's easy to calculate them, but the 156 00:08:19,980 --> 00:08:20,800 intuition-- oh sorry. 157 00:08:20,800 --> 00:08:21,680 This is also included. 158 00:08:21,680 --> 00:08:23,180 This is a k right here. 159 00:08:23,180 --> 00:08:25,519 The harder part is the intuition. 160 00:08:25,519 --> 00:08:27,919 So let's get an intuition of what this gradient function 161 00:08:27,920 --> 00:08:29,129 will actually look like. 162 00:08:29,129 --> 00:08:29,879 So what would happen. 163 00:08:29,879 --> 00:08:33,220 If you wanted to know the gradient at any point in space, 164 00:08:33,220 --> 00:08:35,170 you would substitute an x, y, and z in here. 165 00:08:35,169 --> 00:08:40,559 So you could write it as the gradient function is a 166 00:08:40,559 --> 00:08:44,159 function of x, y, and z. 167 00:08:44,159 --> 00:08:48,169 Remember, T, the temperature at any point, was a scalar field. 168 00:08:48,169 --> 00:08:49,819 At any point in three dimensions it just 169 00:08:49,820 --> 00:08:50,890 gave you a number. 170 00:08:50,889 --> 00:08:53,039 Now when you have the gradient, at any point in three 171 00:08:53,039 --> 00:08:55,099 dimensions it gives you a vector. 172 00:08:55,100 --> 00:08:55,379 Right? 173 00:08:55,379 --> 00:08:57,950 Because it has i, j, and k components. 174 00:08:57,950 --> 00:09:00,330 Where the magnitude are the partial derivatives, and 175 00:09:00,330 --> 00:09:02,850 then the direction is given by i, j, and k. 176 00:09:02,850 --> 00:09:06,990 So we've gone from having a scalar field to a vector field. 177 00:09:06,990 --> 00:09:08,070 And let's see what it looks like. 178 00:09:08,070 --> 00:09:11,570 179 00:09:11,570 --> 00:09:14,120 And let me make it bigger so we can explore it a little bit. 180 00:09:14,120 --> 00:09:17,370 181 00:09:17,370 --> 00:09:19,480 I think that's pretty good. 182 00:09:19,480 --> 00:09:22,600 So this is the vector field. 183 00:09:22,600 --> 00:09:26,220 This is actually the gradient of the function that 184 00:09:26,220 --> 00:09:29,220 we just solved for. 185 00:09:29,220 --> 00:09:34,170 And as you can see, at any point-- and when this graphing 186 00:09:34,169 --> 00:09:36,779 program that did it, it just picked different points and it 187 00:09:36,779 --> 00:09:38,620 calculated the gradients at that point, and then it 188 00:09:38,620 --> 00:09:40,230 graphed them as vectors. 189 00:09:40,230 --> 00:09:44,550 So the length of the vectors are just the magnitudes of 190 00:09:44,549 --> 00:09:46,169 the x, y, and z components. 191 00:09:46,169 --> 00:09:50,259 And then you add them together like you would add any vectors. 192 00:09:50,259 --> 00:09:53,879 And then the direction is given by the relative weighting of 193 00:09:53,879 --> 00:09:55,879 the i, j, and k components. 194 00:09:55,879 --> 00:09:58,439 And as you can see, the intuition is pretty 195 00:09:58,440 --> 00:09:59,950 interesting. 196 00:09:59,950 --> 00:10:03,850 As you get closer and closer to our heat source, the rate at 197 00:10:03,850 --> 00:10:07,440 which the temperature increases, increases! 198 00:10:07,440 --> 00:10:07,720 Right? 199 00:10:07,720 --> 00:10:10,680 The vectors as you get closer, get bigger and bigger. 200 00:10:10,679 --> 00:10:11,389 And let me zoom in. 201 00:10:11,389 --> 00:10:14,875 Let's actually fly in to the vector field. 202 00:10:14,875 --> 00:10:18,740 203 00:10:18,740 --> 00:10:20,620 So we're now within the vector field. 204 00:10:20,620 --> 00:10:23,840 And you can see as we get closer and closer to the center 205 00:10:23,840 --> 00:10:27,980 of our heat source, the vectors, the rate at which the 206 00:10:27,980 --> 00:10:31,860 temperature increases, gets bigger and bigger and bigger. 207 00:10:31,860 --> 00:10:34,450 Anyway, I hope I didn't confuse you. 208 00:10:34,450 --> 00:10:36,950 When I first learned gradients, I think the computation is 209 00:10:36,950 --> 00:10:37,840 relatively straightforward. 210 00:10:37,840 --> 00:10:39,129 It's just partial derivatives. 211 00:10:39,129 --> 00:10:41,659 But the intuition is always the interesting thing. 212 00:10:41,659 --> 00:10:44,439 And hopefully this temperature analogy-- and not even 213 00:10:44,440 --> 00:10:48,515 analogy-- this temperature model will make a 214 00:10:48,514 --> 00:10:49,019 little sense to you. 215 00:10:49,019 --> 00:10:51,360 But it applies to pretty much any scalar field. 216 00:10:51,360 --> 00:10:54,100 Anyway, I'll see you in the next video.