Learning Scientific Programming with Python (2nd edition)

P6.1.3: Gaussian functions and their derivatives

Question P6.1.3

Using NumPy, it is possible to do this exercise without using a single (Python) loop.

The normalized Gaussian function with mean $\mu$ and standard deviation $\sigma$ is $$ g(x) = \frac{1}{\sigma\sqrt{2\pi}}\exp\left( -\frac{(x-\mu)^2}{2\sigma^2}\right). $$ Write a program to calculate and plot the Gaussian functions with $\mu=0$ and the three values $\sigma = 0.5, 1, 1.5$. Use a grid of 1000 points in the interval $-10 \le x \le 10$.

Verify (by direct summation) that the functions are normalized with area 1.

Finally, calculate the first derivative of these functions on the same grid using the first-order central difference approximation: $$ g'(x) \approx \frac{g(x+h) - g(x-h)}{2h} $$ for some suitably-chosen, small $h$.