Question
Problem 1: (Gaussian distributed random numbers) a) Run the following template script. Comment each line to see if you understand what the program histogram2.py is
Problem 1: (Gaussian distributed random numbers) a) Run the following template script. Comment each line to see if you understand what the program histogram2.py is doing import numpy as np import matplotlib.pyplot as plt import matplotlib.mlab as mlab from random import gauss, randrange, seed from scipy.stats import norm # Generating random sequence of gaussian random numbers mu, sigma = 100, 15 #Generating random sequence N=10000 x=[] for k in range(N): x.append(gauss(mu,sigma)) # the histogram of the data num_bins = 40 n, bins, patches = plt.hist(x, num_bins, normed=1, edgecolor='black', alpha=0.75) # best fit of data (mu_fit, sigma_fit) = norm.fit(x) # add a 'best fit' line y = mlab.normpdf( bins, mu, sigma) # Option bins takes take values num_bin because of the hist() function y = mlab.normpdf( bins, mu_fit, sigma_fit) plt.plot(bins, y, 'r--', lw=2) #plot options and plot plt.xlabel('x') plt.ylabel('p(x)') plt.title(r'Fitting pdf to Histogram: $\mu$=%0.3f, $\sigma$=%0.3f' %(mu_fit, sigma_fit)) plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started