Multivariable calculus

Gradient of x cos(y) + y sin(x), Term by Term

For f(x, y) = x cos(y) + y sin(x) the gradient is (cos y + y cos x, sin x - x sin y). Differentiate one term at a time: in the x partial, cos y is a constant multiplier on x and y is a constant multiplier on sin x. The y partial follows the same rule with the roles reversed.

f(x,y)=xcos⁡y+ysin⁡xf(x,y) = x\cos y + y\sin x
∇f=⟨cos⁡y+ycos⁡x,  sin⁡x−xsin⁡y⟩\nabla f = \left\langle \cos y + y\cos x,\; \sin x - x\sin y \right\rangle
∂f∂x=cos⁡y+ycos⁡x\frac{\partial f}{\partial x} = \cos y + y\cos x
∂f∂y=sin⁡x−xsin⁡y\frac{\partial f}{\partial y} = \sin x - x\sin y

Two terms, four small derivatives

Split the function and handle each term on its own. For ∂f∂x\frac{\partial f}{\partial x}, the term xcos⁡yx\cos y has cos⁡y\cos y as a frozen multiplier, and the term ysin⁡xy\sin x has yy as a frozen multiplier.

∂∂x(xcos⁡y)=cos⁡y,∂∂x(ysin⁡x)=ycos⁡x\frac{\partial}{\partial x}\left(x\cos y\right) = \cos y, \qquad \frac{\partial}{\partial x}\left(y\sin x\right) = y\cos x
∂f∂x=cos⁡y+ycos⁡x\frac{\partial f}{\partial x} = \cos y + y\cos x

Now hold xx fixed. The term xcos⁡yx\cos y has xx frozen and cos⁡y\cos y live, which produces the minus sign, and the term ysin⁡xy\sin x has sin⁡x\sin x frozen.

∂∂y(xcos⁡y)=−xsin⁡y,∂∂y(ysin⁡x)=sin⁡x\frac{\partial}{\partial y}\left(x\cos y\right) = -x\sin y, \qquad \frac{\partial}{\partial y}\left(y\sin x\right) = \sin x
∂f∂y=sin⁡x−xsin⁡y\frac{\partial f}{\partial y} = \sin x - x\sin y

Every term of ff is a product of a bare variable and a trig function of the other variable, so no product rule is ever needed. Whichever variable you differentiate, one factor is a constant.

Checking the answer at a point

The origin is the cheapest test point, because sines vanish and cosines are 11.

∇f(0,0)=⟨cos⁡0+0⋅cos⁡0,  sin⁡0−0⋅sin⁡0⟩=⟨1,  0⟩\nabla f(0,0) = \left\langle \cos 0 + 0\cdot\cos 0,\; \sin 0 - 0\cdot\sin 0 \right\rangle = \left\langle 1,\; 0 \right\rangle

That is right: near the origin f≈xf \approx x, since xcos⁡y≈xx\cos y \approx x and ysin⁡x≈xyy\sin x \approx xy is second order. A function that behaves like xx has gradient ⟨1,0⟩\left\langle 1, 0\right\rangle.

Try a point where all four pieces are active, say (π2,π)\left(\frac{\pi}{2}, \pi\right).

∇f(π2,π)=⟨cos⁡π+πcos⁡π2,  sin⁡π2−π2sin⁡π⟩=⟨−1,  1⟩\nabla f\left(\tfrac{\pi}{2}, \pi\right) = \left\langle \cos\pi + \pi\cos\tfrac{\pi}{2},\; \sin\tfrac{\pi}{2} - \tfrac{\pi}{2}\sin\pi \right\rangle = \left\langle -1,\; 1 \right\rangle

Both components collapse neatly there because cos⁡π2\cos\frac{\pi}{2} and sin⁡π\sin\pi are zero. Points like that are worth hunting for when you want a quick sanity check with no decimals.

The mistake: leaking a term into the wrong component

The wrong answers here are almost always cross contamination: a piece that belongs to ∂f∂y\frac{\partial f}{\partial y} appears in ∂f∂x\frac{\partial f}{\partial x}. The classic is ∂f∂x=−xsin⁡y+ycos⁡x\frac{\partial f}{\partial x} = -x\sin y + y\cos x, where the first term was differentiated with respect to the wrong variable.

Unlike a symmetric function, this one gives you no shortcut check. Swapping xx and yy turns ff into ycos⁡x+xsin⁡yy\cos x + x\sin y, a different function, so the two components are genuinely not mirror images and you cannot verify one from the other.

  • In ∂f∂x\frac{\partial f}{\partial x}, every trig function of yy must survive untouched. Seeing sin⁡y\sin y where the original had cos⁡y\cos y means a yy derivative was taken by accident.
  • In ∂f∂y\frac{\partial f}{\partial y}, every trig function of xx must survive untouched. Seeing cos⁡x\cos x there is the same error mirrored.
  • The minus sign belongs to the yy component only, since only cos⁡y\cos y gets differentiated anywhere.

Frequently asked questions

Why does ∂f∂x\frac{\partial f}{\partial x} contain cos⁡y\cos y but no sin⁡y\sin y?

Because cos⁡y\cos y is a constant as far as xx is concerned. Differentiating with respect to xx can never turn a cosine of yy into a sine of yy, so any sin⁡y\sin y appearing in the xx component is a sign that a yy derivative was taken by mistake.

Do the mixed second partials agree for this function?

Yes, as Clairaut's theorem guarantees for a function this smooth. Differentiating cos⁡y+ycos⁡x\cos y + y\cos x with respect to yy gives −sin⁡y+cos⁡x-\sin y + \cos x, and differentiating sin⁡x−xsin⁡y\sin x - x\sin y with respect to xx gives cos⁡x−sin⁡y\cos x - \sin y. Same expression.