Tutorials › Calculus III › Computing Gradients in Two and Three Variables

Directional Derivatives and Gradients · Tutorial 379 of 999

Computing Gradients in Two and Three Variables

Find the gradient of a scalar function by differentiating with respect to each variable in turn and assembling the evaluated partial derivatives into a vector.

Intermediate 9 min read

What You'll Learn

  • Define the gradient of a differentiable scalar function of two or three variables.
  • Find each first partial derivative while holding the other variables fixed.
  • Evaluate all gradient components at a specified point.
  • Compute gradients for polynomial, trigonometric, exponential, and mixed functions.
  • Check partial derivative calculations and avoid common variable-holding errors.

Introduction

A scalar function of several variables assigns one number to each point in its domain. For example, a function \(f(x,y)\) might describe a temperature depending on position, while \(f(x,y,z)\) might describe a concentration throughout a three-dimensional region.

The gradient gathers the function’s first partial derivatives into one vector. Computing it is a systematic task: differentiate with respect to each variable, keeping the other variables fixed, and then evaluate each derivative at the point of interest. This tutorial develops that process for functions of two and three variables.

Definition of the Gradient

For a function \(f(x,y)\), the partial derivative \(f_x\) measures the rate of change with respect to \(x\) while \(y\) is held fixed. Likewise, \(f_y\) measures the rate of change with respect to \(y\) while \(x\) is held fixed. These first partial derivatives are assembled in the order of the variables.

Definition: If \(f\) is differentiable at \((a,b)\), its gradient vector there is \(\nabla f(a,b)=(f_x(a,b),f_y(a,b))\). For a function \(f(x,y,z)\) differentiable at \((a,b,c)\), its gradient is \(\nabla f(a,b,c)=(f_x(a,b,c),f_y(a,b,c),f_z(a,b,c))\).

We will write the gradient vector as \(\mathbf{g}\) when we want to emphasize that it is a vector. Thus, for two variables, \(\mathbf{g}(x,y)=\nabla f(x,y)\). In three variables, \(\mathbf{g}(x,y,z)=\nabla f(x,y,z)\). The gradient may be considered at a particular point, or as a vector-valued function defined at every point where the required partial derivatives exist and \(f\) is differentiable.

In practice, the computation has two stages: first find symbolic formulas for all the first partial derivatives; then substitute the coordinates of the point into every formula. Keeping these stages separate makes it easier to check both the differentiation and the arithmetic.

Why the Partial Derivatives Are the Components

The gradient’s components are not chosen arbitrarily. They are precisely the coefficients that describe the first-order change in a differentiable function. Here is a direct justification for two variables.

If \(f\) is differentiable at \((a,b)\), its change under a small displacement \((h,k)\) is given by a linear part plus an error that becomes negligible compared with the size of the displacement. The linear part has the form \(L(h,k)=Ah+Bk\), where \(A\) and \(B\) are constants. Set \(k=0\). The change is then in the \(x\)-direction alone, so the coefficient \(A\) must equal \(f_x(a,b)\). Setting \(h=0\) gives \(B=f_y(a,b)\). Therefore the linear part is \(f_x(a,b)h+f_y(a,b)k\), and its coefficients form the gradient.

$$ f(a+h,b+k)-f(a,b) = f_x(a,b)h+f_y(a,b)k + \text{an error negligible compared with }\sqrt{h^2+k^2}. $$

The same reasoning applies in three variables: displacements along each coordinate axis identify the three coefficients as \(f_x\), \(f_y\), and \(f_z\). This explains why the gradient contains one first partial derivative for each input variable, in the same order as those variables.

A Reliable Computation Process

For an explicit function, use the following sequence. When differentiating with respect to one variable, treat every other variable as a constant. After all the derivative formulas are found, evaluate each one at the same point, and preserve the coordinate order in the resulting vector.

1
List the variables.
For \(f(x,y)\), find two first partial derivatives; for \(f(x,y,z)\), find three.
2
Differentiate one variable at a time.
Hold all the other variables fixed during each partial differentiation.
3
Assemble the gradient.
Put the partial derivatives in the same order as the function’s variables.
4
Evaluate at the requested point.
Substitute every coordinate into every component and simplify.

Worked Examples

Worked Example: A Polynomial in Two Variables

Find the gradient of \(f(x,y)=3x^2y-2xy^2+4y\) at \((1,-2)\).

First differentiate with respect to \(x\), treating \(y\) as constant. The derivative of \(3x^2y\) is \(6xy\), the derivative of \(-2xy^2\) is \(-2y^2\), and \(4y\) is constant with respect to \(x\). Thus, \(f_x=6xy-2y^2\).

Next differentiate with respect to \(y\), treating \(x\) as constant. The three terms give \(3x^2\), \(-4xy\), and \(4\), respectively. Thus, \(f_y=3x^2-4xy+4\).

$$ \nabla f(x,y)=(6xy-2y^2,\;3x^2-4xy+4). $$

At \((1,-2)\), the first component is \(6(1)(-2)-2(-2)^2=-12-8=-20\). The second component is \(3(1)^2-4(1)(-2)+4=3+8+4=15\). Therefore, \(\mathbf{g}(1,-2)=\nabla f(1,-2)=(-20,15)\).

Check the component evaluations against the derivative formulas: substituting \(x=1,y=-2\) into \(6xy-2y^2\) gives \(-20\), and substituting them into \(3x^2-4xy+4\) gives \(15\). Both entries are in the order \(x,y\), as required.

Worked Example: Exponential and Trigonometric Terms

Let \(f(x,y)=e^{xy}+\sin(x-2y)\). Find the gradient at \((0,\pi/2)\).

For \(f_x\), the chain rule gives \(y e^{xy}\) from the exponential term and \(\cos(x-2y)\) from the sine term. Therefore, \(f_x=y e^{xy}+\cos(x-2y)\).

For \(f_y\), differentiating \(xy\) with respect to \(y\) gives \(x\), while differentiating \(x-2y\) gives \(-2\). Hence \(f_y=x e^{xy}-2\cos(x-2y)\).

$$ \nabla f(x,y)= \left(y e^{xy}+\cos(x-2y),\;x e^{xy}-2\cos(x-2y)\right). $$

At the point, \(xy=0\) and \(x-2y=0-\pi=-\pi\). Since \(e^0=1\) and \(\cos(-\pi)=-1\), the first component is \((\pi/2)(1)+(-1)=\pi/2-1\). The second component is \(0(1)-2(-1)=2\). Thus, \(\mathbf{g}(0,\pi/2)=\nabla f(0,\pi/2)=(\pi/2-1,2)\).

For a check, evaluating the displayed formulas at the point gives exactly \(\pi/2-1\) and \(2\), respectively. The factor \(-2\) in the second component comes from differentiating the inner expression \(x-2y\) with respect to \(y\).

Worked Example: A Mixed Function in Three Variables

Let \(f(x,y,z)=x^2z+yz^2+\sin(xy)+e^z\). Find its gradient at \((1,-1,0)\).

Differentiate with respect to \(x\). The first term gives \(2xz\), the second term is constant with respect to \(x\), the sine term gives \(y\cos(xy)\), and \(e^z\) is also constant with respect to \(x\). Thus, \(f_x=2xz+y\cos(xy)\).

With respect to \(y\), the terms give \(0\), \(z^2\), \(x\cos(xy)\), and \(0\). Therefore, \(f_y=z^2+x\cos(xy)\).

With respect to \(z\), the terms give \(x^2\), \(2yz\), \(0\), and \(e^z\). Therefore, \(f_z=x^2+2yz+e^z\).

$$ \nabla f(x,y,z)= \left(2xz+y\cos(xy),\;z^2+x\cos(xy),\;x^2+2yz+e^z\right). $$

At \((1,-1,0)\), the inner product \(xy=-1\), and \(xz=yz=0\). The first component is \(2(1)(0)+(-1)\cos(-1)=-\cos(1)\), using the evenness of cosine. The second is \(0^2+(1)\cos(-1)=\cos(1)\). The third is \((1)^2+2(-1)(0)+e^0=1+0+1=2\). Consequently, \(\mathbf{g}(1,-1,0)=\nabla f(1,-1,0)=(-\cos(1),\cos(1),2)\).

Checking each entry in the three derivative formulas at the point gives \(-\cos(1)\), \(\cos(1)\), and \(2\). In particular, the exponential contributes \(e^0=1\) to the \(z\)-partial derivative, not to either of the other two partial derivatives.

Conditions and Scope

The gradient is defined at a point where the function is differentiable there. Having all first partial derivatives at a point, by itself, does not guarantee differentiability at that point. A useful sufficient condition is that the first partial derivatives exist in a neighborhood of the point and are continuous there; under that condition the function is differentiable at the point.

The explicit functions in the examples have continuous first partial derivatives everywhere, so their gradients are defined at the points used. If a function is given by a formula involving a denominator, logarithm, or another restricted operation, first ensure the point lies in the function’s domain and that the derivatives involved are defined there.

Common Mistake: Forgetting Which Variable Is Fixed

When finding a partial derivative, the variables not being differentiated are held fixed. For example, let \(q(x,y)=x^2y+\sin y\). A common incorrect calculation for \(q_x\) is \(2xy+\cos y\), as if the term \(\sin y\) changed when \(x\) changed.

But \(y\) is fixed while differentiating with respect to \(x\), so \(\sin y\) is constant and its \(x\)-derivative is zero. The correct partial derivatives are \(q_x=2xy\) and \(q_y=x^2+\cos y\). At \((2,\pi/3)\), \(q_x=2(2)(\pi/3)=4\pi/3\), while \(q_y=2^2+\cos(\pi/3)=4+1/2=9/2\). Therefore, the gradient is \(\mathbf{g}(2,\pi/3)=(4\pi/3,9/2)\), not the vector obtained by including \(\cos y\) in the \(x\)-partial.

A second practical error is to substitute the point before differentiating and thereby lose dependence on variables. The safe order is to find the partial derivative formulas first, then evaluate them. Also check the order: the first component corresponds to \(x\), the second to \(y\), and the third to \(z\), when present.

Why Gradient Computation Matters

The gradient packages the local first-order information about a scalar function into a single vector. Once its components are known, later methods can use that vector to describe changes in chosen directions or to analyze local behavior. At this stage, the essential skill is accurate component-wise computation: use the correct variable as the differentiation variable, apply differentiation rules carefully, and evaluate every component at the same point.

Key takeaway: For a differentiable scalar function, compute one first partial derivative per input variable, hold the other variables fixed in each calculation, and assemble the results in the variables’ original order.

Check Your Understanding

For each question, find the requested partial derivatives before substituting any point coordinates.

  1. For \(f(x,y)=x^3y+2y^2\), find \(\nabla f(1,2)\).
  2. For \(g(x,y)=\cos(x+y)+e^x\), find both first partial derivative formulas.
  3. For \(h(x,y,z)=xyz+z^2\), find \(\nabla h(1,2,-1)\).
  4. When computing \(f_y\), which variables are held fixed?
  5. Why does the existence of first partial derivatives at a point not, by itself, guarantee differentiability there?