# Lecture 22: Gradient and Divergence

Flash and JavaScript are required for this feature.

Instructor: Prof. Gilbert Strang

The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To make a donation, or to view additional materials from hundreds of MIT courses, visit MIT OpenCourseWare at ocw.mit.edu.

PROFESSOR STRANG: OK. So this is lecture 22, gradient and divergence, headed for Laplace's equation. So the gradient will be our operator A; the divergence, or minus the divergence, will be A transpose, and then A transpose A will be the Laplacian. We get to Laplace's equation Wednesday. Today I wanted to take them separately. To understand the meaning of gradient, the meaning of divergence, the connection between them. I mentioned at the end of last time that one is the transpose of the other, or minus the transpose. I'll try to keep gradient on this side, and if I could only transpose the blackboard, I could do divergence-- I'll do divergence on that side. And I guess if I could get a rotating blackboard, right in the middle I could do curl. That would be perfect. [LAUGHTER]

OK. So some of this will not be new to you. But maybe some of the insights or the ways of looking at it could be new. This is the background of vector calculus. So we have things like vector fields. That means I have a vector [v_1, v_2] at each point (x, y). So I could draw a little arrow at every point to show the direction and magnitude of that vector. I have a field of vectors.

OK, so. From last time, our basic setup is, the gradient is this first operator, A. The one we see at the beginning. One change. Instead of calling the result e, let me connect to velocity. I'll be thinking of u as a potential, I'll use the word potential for u, and I'll use v instead of e for the velocity component. So that's the A.

And then on the other side, I start with a w. Again, it's a vector field, it's actually a momentum. Very often the step between here and here, well most often, the step between here and here will be the identity. That's what gives Laplace's equation. So you'll have to watch, I'm sometimes confusing the v's with the w's. Because when I go to Laplace's equation and c is the identity, they're the same. But I would like, today, to try to keep this left side separate, the gradient, from the right side, the divergence. And understand what they mean, and how to work with them. And of course, a big connection is the divergence theorem, or the Gauss-Green connection, identity. We'll get to that.

OK, gradient first. First, what does it mean? If I have a function u, what does its gradient tell me? And then the second is kind of a backwards question. Suppose I have the v. Is it the gradient of some u? So one direction from u to v, and the second direction will be from v back to u when possible.

OK, meaning of the gradient. So the gradient is just the obvious thing, the two derivatives in the x and y direction. So of course the gradient gives you the rate of change, the partial derivatives of u. But how do you see that in a picture? Let me draw an important curve. I'll start with a very simple u. u is x squared plus y squared. So that's my example. Example one. And what I've drawn is an equipotential curve. Or isopotential might be a more appropriate word these days, but we still say equipotential. It means that, along this curve, u is a constant. And for this particular potential, this simple one to work with, x squared plus y squared, the curve is a circle. So the curve would be a circle.

Now how do I understand that? How do I see that? I think of this-- Let me try to draw the whole surface, u equal x plus y squared. What would that whole surface look like? Let me try to put that picture-- This is a 2-D picture right now. It's sort of a cross-section. Let me put this 2-D picture into a 3-D picture to see. So the 3-D picture is a picture of the whole surface. u going up, x and y going around. And what kind of a curve, what kind of a surface do I have for that function, x squared plus y squared? Well, it grows, right? It's sort of, I use the word bowl. And we've seen it before. It sort of goes up, right, and it goes up faster and faster as I go out, because of the squares.

And now what I want to do is, suppose I take the cross-section, suppose I take this bowl. Do you see a beautiful bowl there? And now, I cut through it, horizontal cross-section, at the height c. What do I get if I take this surface in 3-D, and I cut through it by a plane, the plane at that height, c. So if the plane went through there, that distance was c. It cuts out a cross-section out of the bowl. And what's the cross-section? It's the circle. It's this one. So out of the bowl, this plane is cutting this cross-section. This is height u equal constant. So I'm really intersecting one surface, the plane u equal constant, with the bowl. And the intersection of those two is the equipotential, the curve u=c. Right? You see the two pictures?

And now, what does the gradient tell me in that picture? What does the gradient of u tell me in that picture? Over here, it pointed outwards, but now we're going to kind of see why. So, it pointed outwards for this nice function u. I could have made a more general function u, but let me just stay with this simple one.

So suppose I'm climbing. This is like, I'm climbing out of a volcano or something. Usually, I would say a mountain, but the way I've drawn the thing, it's not much of a mountain. So OK, volcano. Climbing out of it. And I got up to this point, which was this point. Now, what does the gradient tell me? That when I'm climbing away, I reach -- duh duh duh duh -- I get up to here. What does the gradient tell me? It tells me which way to go. It tells me the steepest direction upwards, and it tells me how steep. Right. So it tells me those two things, direction and magnitude.

Direction is, well for this special function you know the direction is, like, straight outwards, straight upwards. And how steep is it? What's the steepness? It's the size of, it's the magnitude of this vector, grad u. Which is the-- This is not-- How big is this vector? This is my vector grad u. This is another shorthand notation for gradient. The size of that vector is the square root of what? The length of a vector is the square root of the sum of the squares. So I have the length of v is the square root of 4 x squared plus 4 y squared. OK.

Now, it gets steeper and steeper, of course. The cross-sections here would be all circles for this simple function. And the gradients would keep pointing out, and we'd have a function that comes from a surface like that.

OK, this is what I wanted to say about question one, the meaning of the gradient. The thing to remember, if you remember the two pictures, you've really got the idea of what the gradient is telling you. For a function of one variable, the derivative is telling you the slope. Well, we've got slopes in the x direction, slopes in the y direction, and the gradient direction tells us the steepest slope. And it tells us, yeah, tells us what that slope is. OK, so much for the meaning of the gradient.

Now, backwards. Suppose I have v. Because these gradient fields are extremely important. I mean, they're wonderful fields. If you've got a v_1 and a v_2 that is the gradient of some u, you want to know that. I mean, that's good news. Most fields will not. So let's figure out, when is-- Can I lift that up now, and do the other gradient thing now? Or maybe I'll put the final gradient blackboard here, and then make way for divergence.

OK, so I'm asking now question two. I'm given v_1 and v_2, and I want this to be du/dx for some u, and I want this to be du/dy. So I've got two equations. If I'm looking for u. Remember, I'm now starting with these, looking for the u. OK. So, am I going to find Au? Am I going to find a function u, whose x derivative is my v_1 that was given, and the y derivative is my v_2? Well, chances are not good. Right? I've got two equations here, but only one unknown. So generally, it's like a rectangular system. Usually there's no solution, but sometimes there is. So we have to find out, what's the test for consistency for these to have a solution?

And this brings us right away to the key identity from partial derivatives. OK. Can you see some condition that has to-- How can I connect these two equations, and therefore connect v_1 and v_2? That'll be the test for weather-- You could say, in matrix language, I'm asking whether [v_1, v_2] is in the column space of the gradient. Is it something I can get from this tall thin matrix?

OK, they key idea is take the y derivative of that equation, d second u/dydx. And take the x derivative of this equation. dv_2 dy-- dx, is the second derivative of u, with respect to-- the x derivative of the y derivative. OK. So it's like I'm operating on the equations. I'm doing what I'm allowed to do, I'm doing the same thing to both sides of each equation. Now I'm going to eliminate. And what am I going to find? What's the key point here?

The key, key point about second derivatives is that that equals that. Right? Take any function. Do we want to practice with a function, just to see it be true? Take, let u be x cubed y. Just for the heck of it. OK, I don't know what-- Is that going to produce anything interesting? Then maybe I'd better make it y squared. I don't know why I'm doing this, it just is like an example, to make it believable. OK, so du/dx, u_x, is -- and I'll often use u sub x as a sort of shorthand -- will be what? 3x squared y squared. And u_y, taking the y derivative is just, x is constant, 2x cubed y. And now let me do u_xy, the y derivative of this. Which is what? It's been a long weekend, but hey, we can do these. The y derivative of that is going to be 6x squared y. And the x derivative of this is going to be, so I should say y-- I've got the y derivative and now I should take the x derivative of that. And what do I get? The x derivative of that is 6x squared y. And look! They're the same. Hooray. OK. So if the function is smooth and has these derivatives, they'll come out the same.

And therefore, so what do I conclude? What's the test on v_1 and v_2 that it must pass to be a gradient field? For there to be a function u, that solves these equations. These are solvable only when what? Well, if these are the same, these have to be the same. So it's solvable only when-- I need dv_2/dx - dv_1/dy to be what? Zero. OK. That's the conclusion. Those have to be the same. dv_2/dx - dv_1/dy has to be zero. OK. Because those are the same.

I guess I came to this early because it's the key identity of vector calculus. Well, the key identity behind vector calculus is this fact about the derivatives.

Can I just throw out a question that you might think about? We already have seen, so often now, the second derivative, like v_xx. Or u_xx, suppose. So here's a little question to think about. So think. OK. I just want to bring finite differences in for a moment. u_xx we've got a handle on. We know that that's like u at x+h -- and if there was a y, put in the y -- minus 2u at x -- and I can put in a y there -- plus u at x-h, y. We've seen that. That'll be the x derivative, second x derivative-- sorry, second x difference. Since we're taking x derivatives and x differences, it's x that moves, and y doesn't move. Just the central idea of partial derivatives. u_yy will be similar with y moving. And my question to you is -- and we could have asked it way way back -- what's a finite difference approximation to u_xy, to the cross derivative? That equals what? I want to go to finite differences. OK, what finite differences? Which finite differences? Maybe that's a question to think about. If I can remember, I'll include it just as a small homework question for the homework on this material.

So that's looking ahead, really, today. We're not making things discrete. We're in continuous x and y. We have vector fields. And we now know the test for a gradient field. I'm tempted to use the word curl here. I'm tempted to use the word curl. I want to connect that test -- may I use the word curl without -- and I'll say why I'm not going to do everything properly with curl right away. I would describe this as curl. The test is, the curl of v has to be zero. So for me, that's the curl of v.

You're going to say, wait a minute. I learned about curl, and that doesn't look like the curl to me. So I'll say wait another minute. It's not that far off. OK, what's your objection? Your objection is that the curl is in three-dimensional space. Right? When you saw curl, and of course it comes in this section of the book, we had functions of x, y, z. And the curl had three components. And those three components-- Do you remember curl? I mean, if you remember curl, you're a good person. Because it's got this-- You sort of remember that it has things like this. Right? Sort of, differences of derivatives, and the indices follow a certain pattern. I'm saying that this is the natural-- this is the curl in a plane.

What do I mean by the curl in a plane? So in 3-D, v has components of v_1, v_2, and v_3. Right? That depend on x, y, z. In the plane, in 3-D, I have a vector field v, which has components v_1, v_2, v_3. All depending on x, y, z. That's the general 3-D picture, where you usually see the curl. Now, in the plane, what's happening? In the plane, we have two components. So what's happening? Think of this. So in 3-D, our velocity field is like, the flow is going all over the place. Right? It's a three-dimensional flow. But now suppose my flow stays in the plane. So in 2-D-- so now I have to put 2-D up above here. So in 2-D, a plane field is what I'm working with today. My v is some v_1(x,y). No z, no dependence on z. v_2(x,y), the y direction of the velocity doesn't depend on z, because this is a plane field, same on every plane. And the third component of this plane field, the velocity perpendicular in the z direction, is zero. OK. Zero.

So what I want to say is that if I look, if I specialize to plane fields, to fields like these, then the only component of the curl that survives is this one. See, the other components of the curl, which I'm not even writing down-- the other components of a curl have derivatives of v_3, but v_3 is zero. And they also have derivatives of these guys with respect to z. But they don't depend on z. So that's why all the other pieces of the curl, like, are automatically zero for a plane field. So that the only component that's significant-- The test curl v equals zero boils down to a test not on three things, but just on one. And that's the one. So because I want to stay mostly with plane fields and two-dimensional problems, I just had to comment that, if the curl was to get in here, it would fit fine. And if I restrict the curl to the fields I'm working with, plane fields, then there's only one component I'll have to think about, it has to be zero to have a gradient field.

OK, now I guess I should just do an example or two. Can I give you a v_1 and v_2, and you tell me, is it a gradient field or is it not a gradient field. Let me give you a different, let me just change these guys. Suppose I change that to y and x. So there is a v, a different v. That's a vector field. At every point x, y, I've got a little vector. I could try, even, to draw them. And I'm going to ask you, is it the gradient of any u. And if it is, what's that u?

So let me show you what I mean by a vector field. I mean, at a typical point like x=1, y=0, the vector-- Let's see, if x is one and y is zero, then what's the gradient at that point? [0, 2], am I right? I won't draw it too big, or you won't be able to see a darn thing. OK, what about at the point (1, 1)? Which way is my vector field going? [2, 2]. So what's that look like? Plotting the vector field v at a bunch of points. So you get like a map of little arrows. So here it would go that way. Is that right? Huh. I wasn't expecting that, to tell the truth.

Let's see, so can I get in some more points? Let's see. What if I have there. What's-- At (1/2, 0). Then, v is [0, 1]. Where is this flow going? See, if the point is along this line, where y is equal to x, then the flow was going out along this line. Can you give me some other point here, just so we get some handle on this? There's the point (2, 1), let's say. Let me put it over a little bit. How about the point (2, 1)? What's the vector if I just want to draw -- I'm just drawing here. And of course, code would do it much better than I'm doing. (2, 1), that gives me [2, 4], right? So [2, 4] is over two and up, it's like this. I think -- but I don't swear to it -- that if I connect all this-- See, now you have to take a big leap of faith. Imagine, like, at every point we've got these little arrows, and I want to connect them up.

Let me do something. I'll do that, but let me come back to the question I should have asked you first. Is this a gradient field? Does it satisfy the curl zero condition that we put in a box here? Does that satisfy dv-- There is v_2 and there's v_1. Is dv_2/dx, whatever that test was, minus dv_1/dy equal zero? Yes, no? Yes, right? dv_2/dx is two, and dv_1/dy is two. So what's the conclusion then? It satisfied my little test, so this must be the gradient of some u. Right? That's the question we have. Which vector fields-- And we found that this is one of them, it passes the test. It's the gradient of some u. What's the u? What's the function u, which is supposed to exist, whose gradient is that. 2xy. Did everybody spot that one? 2xy. Because the x derivative has to be 2y. So I just integrate with respect to x. This is the x derivative. It's 2y. Take the integral with respect to x, and I get 2xy. And there could be some term that depended only on y. Anyway, this works.

I think that this, maybe gives me somehow-- Oh, yeah. What's the equipotential curve now? Oh, yeah, this picture's going to come together. What is the equipotential curve for this potential? It was a circle for the first guy, but circles are out now. I changed v. I've got a new potential function. And now I want to draw, in this graph, the equipotentials. Suppose u is one. Suppose I draw the curve 2xy=1 in that picture. What kind of a curve is it? Do you recognize this? The Greeks would. Recognize 2xy=1. Or you could say y=1/(2x). That gives you a quick handle on the curve. It comes down like that. Right? And what's the Greek name for that curve? Oh, come on. It's a hyperbola. It's a hyperbola.

Hyperbolas-- You remember the Greeks, they had these conic sections. They had ellipses, they had parabolas, the marginal case, and then they had hyperbolas. And they all come from second degree things. If I have a x squared and 2bxy and c y squared equal one, that's one of those curves. And if it was x squared plus y squared equal one, it was a circle. If it was x squared plus 7y squared equal one, it would be an ellipse. The positive definite-- It all comes down to linear algebra, of course. If that little matrix is positive definite, so that means a and c are positive, and ac is bigger than b squared, you know the test for positive definite. What kind of curve do the Greeks have? What kind of equipotential-- What kind of a curve have we got here? An ellipse. If this curve, if that little matrix is indefinite, as, for example, here. So with this one, what would be the matrix, what's the matrix that goes with 2xy, if I match this with this. That's the matrix. There's no a squareds, there's no x squareds, there's no y squareds. And there are 2xy's, I think the matrix is that. So it's this nice symmetric matrix. Is that a positive definite matrix? Certainly not. It's indefinite. It's eigenvalues are plus one and minus one, adding to the trace zero.

So indefinite matrices correspond to hyperbolas. And later on, definite matrices will correspond to elliptic partial differential equations. And indefinite matrices-- Like Laplace. And indefinite matrices will correspond to hyperbolic partial differential equations, like the wave equation. What's the-- Now we're here. I didn't expect to get here. What's the marginal case? What's the marginal case between positive definite and indefinite is...? Semidefinite. Great. And what kind of a curve do you think comes when this little matrix is semidefinite. It's the one in between ellipses and hyperbolas. The marginal guy is a parabola. Right. So semidefinite would correspond to a parabola. Right. OK. Good.

Anyway, all I was going to say is, this u=2xy, that's our potential. If I draw equipotential curves, they're hyperbolas. And now, what's the point about these little arrows that I got started on. What was the very first point about the answer to the meaning of the gradient was what? These are the gradients of u, so those arrows point where? Perpendicular to the hyperbolas. Perpendicular to the hyperbola. We're trying to see the geometry -- its beautiful geometry -- behind the gradient. So if v is a gradient, then it comes from some u. I can plot the u equal constant, the equipotential, and then the gradients will be perpendicular. So they really are a little-- OK, good.

OK, those are pieces of information that you have, but always need saying again. And to get the picture in your mind-- I suppose, finally, I should choose a v which is not a gradient. Just to finish. How shall I adjust that v? This v was a gradient. Can you just change it a little bit -- practically anything you do will screw it up -- to make it not a gradient? So I just changed this two, what shall I change the two to? To three. That would totally foul it up. So that vector field, which I could draw little pictures of, but there would be no u that it's coming from. There would be no u. These little arrows would not line up perpendicular to some beautiful curves. I don't get a u from that. Because the y derivative of that is three, and it doesn't equal the x derivative of that. So that's a no-good one. Let's go back to the good one.

OK. OK, good. Is that OK for gradients? We got the meaning of gradients. They point perpendicular to equipotentials. They tell how steeply those-- They tell the separation between the equipotentials, right. It's like, if you're a mountain climber, you're looking at your map, your contour map, and that's all I'm drawing here. I'm drawing a contour map that every guy who goes climbing in New Hampshire is going to have. And it shows little circles, those are level heights, right? Those are level contours. And if you want to climb as fast as possible, you go perpendicular to those contours. And the distance between contours tells you how steep it is. So it's all nice geometry.

OK. I've got to get to divergence here. Divergence. I should have said though-- Damn. There's more to say about gradients. That question of whether [v_1, v_2], the vector field, is this question. The question of is it the gradient of some u. So we now have a test. We now have a test. This is our test, right? That's our test. But I have to connect it with Kirchhoff's voltage law. Do you remember, we haven't talked so much about Kirchhoff's voltage law, but I'm connecting it with the discrete case, to add in a little more insight. What did Kirchhoff's voltage law say? In that case, A was a difference matrix. It was the incidence matrix for our graph. And the question was-- I have to take two moments to think about that.

So Kirchhoff's voltage law, for a graph. A is an incidence matrix. You know, the minus one, one guys, one for every edge? And let me call it v again, or e. I called it e at that time. Let's just look at an x. Right. A is this long thin matrix, times-- sorry, u's. u's. Let me say it all at once. Which vectors have the form Au? Which vectors are combinations of the columns of A? The test is, Kirchhoff's voltage law, that if I go around any loop in the graph -- so if I have a u_1 here, u_2 here, u_5 here, and u_7 here -- then Au will produce u_1-u_7 on that edge. It'll produce a u_2-u_1 on that edge. It'll produce a u_5-u_2 on that edge, if the edges are all going that way. And it'll produce a u_7-u_5 on that edge. So I've got four components of Au, four differences. And what does Kirchhoff's voltage law tell me about those four differences? Which I can certainly see directly. Those four differences, u_1-u_7, u_2-u_1, u_5-u_2 and u_7-u_5. What's the obvious fact about those four guys? They add to zero. The total drop around a loop is zero. You see, if I cancel those, if I add them, the u_1's cancel, the u_2's cancel, the u_5's cancel, the u_7's cancel. We know this. OK. So that's Kirchhoff's voltage law. It's got to have a continuous form. This tells me, this is the test on v at a point. What's the test on v around a loop? I just want to connect that-- I have to connect that to a second test. I'll just mention it, and you'll find it in the book. That's the pointwise test. That was the easy test. We applied it to this and we got the answer yes. If it was 3y, 2x, we got the answer no.

Now let me give you a test that looks like Kirchhoff's voltage law. So I'm going to integrate around a closed loop. What am I going to integrate? I think I integrate v-- Oh boy, I'd better look. It's easy to get these wrong. Yeah. So I would call this the vorticity. And then I would say the vorticity is zero for a gradient field. Now my integral guy is going to be the circulation. Oh yeah. Because I'm following the path. So it's just v_1*dx+v_2*dy should be zero. Around every closed loop -- that's idea of this thing, that it tells me the integral goes around a closed loop -- if I follow the velocity field, the total circulation is zero. I put this up here as a fact in vector calculus that's connected to that. These, one is zero when the other is zero. There's a Stokes' theorem that tells me that this integral is found from a double integral of this. So if one is zero, the other is zero. I'm just saying, here is the natural analog of Kirchhoff's voltage law.

OK. I had to say something about voltage law, because for the divergence, which I'm now going to get to-- Whatever. Let me ask about divergence of w equal zero. What does that mean? That's going to be the equivalent of whose law? Please tell me. Which law is going to be the equivalent of-- Divergence of w equal zero is going to mean there's no source. Whatever goes in, comes out. Whose law is that? That's Kirchhoff again. Well, yeah, other people in physics. Right. But in our little world, it's the other Kirchhoff law. It's Kirchhoff's current law. It's the one, it's the A transpose, right? This is what we're thinking of as A transpose w equal zero. Kirchhoff's current law, in equals out.

How will I translate that in equal out for functions? Now I don't have-- On a graph, I just had the total flow, the net flow at every node. Notice the divergence is at every node. The circulation was around every loop. OK. So in equals out was just the sum of four things. OK, here I'm going to have in equal out-- How am I going to express in equal out? Divergence of w equals 0. Yeah, what I need is the divergence theorem. Let's just face it, we've got to have that.

So I have a region here. I have a w everywhere, w, [w_1, w_2]. Then the divergence theorem. This is the great identity, which of course has a discrete form. OK. The divergence theorem says that if I integrate over the region, over this region R, the divergence, that's (dw_1/dx+dw_2/dy), dxdy. So that's like telling me the source, I'm integrating over the source at every point. At every point here, this measures in minus out. But now, when I put the whole thing together by integrating, what's the right-hand side of this equation? Do you know the divergence theorem? And let's remember it and see why it's so.

What I'm doing is in equals out for the whole region at once. Right? When I-- This is like in equal out at a point. But now I'm putting all the points together. So the only way out will be out through the boundary. And so I'll need to say how much flows out. This is the total source, the total in equal out inside. The only way to get out is through the boundary. So this is the integral around the boundary of-- So what's the flow out? It's, yeah, it's somehow-- Think now, what should go there?

This is flux I'm talking about. Flux is short word for the total flow out. OK. So now I've got to get this right. In vector notation, it would be-- w tells me the flow. But flow outwards, see, suppose w points that way. Then the actual flow out is not all that. Because a lot of that is just going sideways. It's this part. It's the flow perpendicular to the boundary. So it's w dot n, the normal component of flow. And I integrate that around the boundary.

There you have a key, key theorem. In 2-D. And it's an equation for the flux. It's like the fundamental theorem of calculus, but now we're in two dimensions. And this is what it looks like. OK, so I'm obviously not going to finish with the divergence theorem today. So what's the conclusion? If the divergence is zero, then what? If the divergence is zero, if this is zero at every point, then this is zero across every loop. Can I call this thing a loop? That closed loop. That's the conclusion that we want to reach. So this is the divergence theorem. The text gives a proof, not to repeat in class, but it's a crucial formula to know. That the integral of the divergence is the flux.

OK. Let's come back to that Wednesday, and I'll have lots of homework for you. Thanks for turning in these today.