Ed's Big Plans

Computing for Science and Awesome

Partial Derivatives for Residuals of the Gaussian Function

without comments

I needed to get the partial derivatives for the residuals of the Gaussian Function this week. This is needed for a curve fit I’ll use later. I completely forgot about Maxima, which can do this automatically — so I did it by hand (Maxima is like Maple, but it’s free). I’ve included my work in this post for future reference. If you want a quick refresh on calculus or a step-by-step for this particular function, enjoy :D. The math below is rendered with MathJax.

The Gaussian Function is given by …

$$ f(x) = ae^{-\frac{(x-b)^2}{2c^2}} $$

  • a, b, c are the curve parameters with respect to which we differentiate the residual function
  • e is Euler’s number

Given a set of coordinates I’d like to fit (xi, yi), i ∈ [1, m], the residuals are given by …

$$ r_i = y_i – ae^{-\frac{(x_i-b)^2}{2c^2}} $$

We want to get …

$$ \frac{\partial{r}}{\partial{a}}, \frac{\partial{r}}{\partial{b}}, \frac{\partial{r}}{\partial{c}} $$

In each of these partials, we can omit the first term yi because it drops to zero immediately when held constant.

Let’s start with r'(a) … (b, c, x, yk)

\begin{align}
r &= -ae^{-\frac{(x-b)^2}{2c^2}} \\
r &= -ae^{u}, (u \in k) \\
r’ &= -e^{u}u’ \\
\frac{\partial{r}}{\partial{a}} &= -e^{-\frac{(x-b)^2}{2c^2}}
\end{align}

Now let’s get r'(b) … (a, c, x, yk)

\begin{align}
r &= -ae^{-\frac{(x-b)^2}{2c^2}} \\
r &= -ae^{u} \\
u &= -\frac{(x-b)^2}{2c^2} \\
u &= -\frac{x^2-2bx+b^2}{2c^2} \\
u &= -\frac{x^2}{2c^2}-\frac{2bx+b^2}{2c^2}, (-\frac{x^2}{2c^2} \in k) \\
u’ &= -\frac{-2x + 2b}{2c^2} \\
u’ &= -\frac{b – x}{c^2} \\
r’ &= -ae^{u}(u’) \\
r’ &= -ae^{-\frac{(x-b)^2}{2c^2}}(-\frac{b – x}{c^2}) \\
\frac{\partial{r}}{\partial{b}} &= \frac{a(b – x)e^{-\frac{(x-b)^2}{2c^2}}}{c^2}
\end{align}

Finally, let’s get r'(c) … (a, b, x, yk)

\begin{align}
r &= -ae^{-\frac{(x-b)^2}{2c^2}} \\
r &= -ae^{u} \\
u &= -\frac{(x-b)^2}{2c^2} \\
u &= -{(x-b)^2}\frac{1}{2c^2}, (-{(x-b)^2} \in k) \\
(\frac{1}{v})’ &= \frac{-v’}{v^2} \\
(\frac{1}{2c^2})’ &= \frac{-(2c^2)’}{(2c^2)^2} \\
&= \frac{-4c}{4c^4} \\
&= \frac{-1}{c^3}, (c \neq 0) \\
u’ &= -(x-b)^2\frac{-1}{c^3} \\
u’ &= \frac{(x-b)^2}{c^3} \\
r’ &= -ae^{u}(u’) \\
r’ &= -ae^{-\frac{(x-b)^2}{2c^2}}(\frac{(x-b)^2}{c^3}) \\
\frac{\partial{r}}{\partial{c}} &= \frac{-a(x-b)^2e^{-\frac{(x-b)^2}{2c^2}}}{c^3}
\end{align}

That’s all three partials!

Here are Maxima’s answers for r'(b) and r'(c)

dr/db = -a*(x-b)*%e^-((x-b)^2/(2*c^2))/c^2
dr/dc = -a*(x-b)^2*%e^-((x-b)^2/(2*c^2))/c^3

😀

Eddie Ma

October 10th, 2011 at 11:40 am