P3.1.3Plotting the Gaussian function
The normalized Gaussian function centered at \(x=0\) is
\[g(x) = \frac{1}{\sigma\sqrt{2\pi}}\exp\left(-\frac{x^2}{2\sigma^2}\right).\]
Plot and compare the shapes of these functions for standard deviations \(\sigma=1, 1.5\;\mathrm{and}\;2\).
Show solution
The following code plots three normalized Gaussian functions with different standard deviations.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-6, 6, 1000)
def gaussian(x, sigma):
""" Return the normalized Gaussian with standard deviation sigma. """
c = np.sqrt(2 * np.pi)
return np.exp(-0.5 * (x / sigma)**2) / sigma / c
sigma1, sigma2, sigma3 = 1, 1.5, 2
g1, g2, g3 = gaussian(x, sigma1), gaussian(x, sigma2), gaussian(x, sigma3)
plt.plot(x, g1)
plt.plot(x, g2)
plt.plot(x, g3)
plt.show()