I've got a function $$g(x,y) = \| f(x,y) \|_2$$ and I want to calculate its derivatives with respect to $x$ and $y$.

Using Mathematica, differentiating w.r.t. $x$ gives me $ f'_x(x,y) \text{Norm}'( f(x,y))$, where Norm is $\| \cdot \|$.

I read here that

$$d\|{\bf x}\| = \frac{ {\bf x}^Td{\bf x}}{\|{\bf x}\|}$$

at least for the $2$-norm. Point is, as inside the norm I have a multivariate function, I'm still confused on how to calculate $ f'_x(x,y) \text{Norm}'( f(x,y))$

I think it should be $f'_x(x,y) \frac{f(x,y)}{||f(x,y)||}$, but some verification would be great :)

  • 1
    $\begingroup$ When you are not sure, writing out all components should help. It's nasty, I know, but it always works. $\endgroup$
    – Tunococ
    Jan 31, 2013 at 12:30
  • $\begingroup$ First find the derivative of $h(x,y) = \|f(x,y)\|^2 = f(x,y) \cdot f(x,y)$, using the product rule. Then you can get the derivative of $g(x,y) = \sqrt{h(x,y)}$ from the chain rule. $\endgroup$ Jan 31, 2013 at 12:36
  • 1
    $\begingroup$ Just saw your edit. This answer is correct. $\endgroup$ Jan 31, 2013 at 12:36
  • $\begingroup$ @YellowSkies: how is your link related to this question? $\endgroup$
    – Watson
    Sep 14, 2016 at 9:14
  • $\begingroup$ @Tunococ can you solve this one ? math.stackexchange.com/questions/2587031/… thanx. $\endgroup$ Dec 31, 2017 at 21:23

2 Answers 2


Suppose $f:\mathbb R^m \to \mathbb R^n$. Decompose into $f = (f_1, \ldots, f_n)$. Each $f_i$ is a real-valued function, i.e., $f_i: \mathbb R^m \to \mathbb R$. Then $$ g(X) = \|f(X)\|_2 = \sqrt{\sum_{i=1}^n f_i(X)^2}. $$ Therefore, $$\nabla g(X) = \frac 12\left(\sum_{i=1}^n f_i(X)^2\right)^{-\frac 12}\left(\sum_{i=1}^n 2f_i(X)\nabla f_i(X)\right) = \frac{\sum_{i=1}^n f_i(X)\nabla f_i(X)}{\|f(X)\|_2}. $$ This matches your answer.

If you want to write in terms of the Jacobian matrix of $f$ instead of components $f_i$, you can: $$ \nabla g(X) = \frac{J_f(X)^T f(X)}{\|f(X)\|_2}. $$

  • 1
    $\begingroup$ Many thanks for the answer! $\endgroup$
    – Babis
    Jan 31, 2013 at 13:14
  • 1
    $\begingroup$ May I know if $m = 1$, e.g. $f(t) = (f_1(t), f_2(t), \cdots, f_n(t))$, then $\nabla f_i(t) = \dfrac{df_i(t)}{dt}$? $\endgroup$
    – Yuki.F
    Nov 5, 2020 at 4:55
  • 1
    $\begingroup$ That seems correct. $\endgroup$
    – Tunococ
    Nov 6, 2020 at 6:31

To calculate the derivative of the function $ g(x, y) = \|f(x, y)\|^2 $ with respect to $x$, you can use the chain rule and the derivative of the Euclidean norm $\|x\|$ as you mentioned. Here's the correct calculation:

Given $$g(x, y) = \|f(x, y)\|^2,$$ let's find $\dfrac{{\partial g}}{{\partial x}}$. We have

$$ \begin{align*} g(x, y) &= \|f(x, y)\|^2 \\ &= [f(x, y) \cdot f(x, y)] \\ &= [f(x, y)]^T \cdot f(x, y) \quad \text{ (assuming \(f\) is a column vector)} \\ \end{align*} $$ Now, we can differentiate both sides with respect to $x$: $$ \begin{align*} \frac{{\partial g}}{{\partial x}} &= \frac{{\partial}}{{\partial x}} \left([f(x, y)]^T \cdot f(x, y)\right) \\ &= \left(\frac{{\partial}}{{\partial x}}[f(x, y)]^T\right) \cdot f(x, y) + [f(x, y)]^T \cdot \left(\frac{{\partial}}{{\partial x}} f(x, y)\right) \\ \end{align*} $$

Now, the first term is the derivative of the transpose of $f$ which is simply the transpose of the derivative of $f$ with respect to $x$. So, it becomes $f_x^\prime(x, y)^T$.

The second term $$\frac{{\partial}}{{\partial x}} f(x, y)$$ is the derivative of $f$ with respect to $x$, therefore the final result for $\dfrac{{\partial g}}{{\partial x}}$ is:

$$\frac{{\partial g}}{{\partial x}} = f_x'(x, y)^T \cdot f(x, y) + [f(x, y)]^T \cdot f_x(x, y)$$

This expression accounts for the derivative of the norm as well as the derivative of the function $f(x, y)$ with respect to $x$.

The approach to calculate $\dfrac{{\partial g}}{{\partial y}}$ is similar.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .