# hw4_sol - #! /usr/bin/env python # AST5765/4762 2009 HW4...

This preview shows pages 1–2. Sign up to view the full content.

#! /usr/bin/env python # AST5765/4762 2009 HW4 Solutions # 1. import numpy as np import matplotlib.pyplot as plt import numpy.random as nr Num = 10000 sigma = 13. cx = 55. gsamp = nr.normal(cx, sigma, Num) # 2. nbin = 100 dbin = 1. sbin = 0. plt.figure(1) bins = np.arange(nbin+1, dtype=float) * dbin + sbin # The plt.hist() function returns the histogram data as well as # plotting it. Use a dummy variable to hold the result, or else it # prints on the screen. Traditionally such variables are called # things like "dummy". dummy = plt.hist(gsamp, bins=bins) plt.xlabel('x') plt.ylabel('N(x)') plt.title('Histogram of a Gaussian') plt.savefig('hw4_sol_prob2_plot1.png') # 3. x = bins[:100] + dbin/2 y = Num * 1./np.sqrt(2.*np.pi*sigma**2) * np.exp(-0.5 * ((x-cx)/sigma)**2 ) plt.plot(x, y, 'g-', linewidth=2) plt.savefig('hw4_sol_prob3_plot1.png') maxpow = 6 nsamp = 10 nrec = 3 sample = np.zeros((maxpow, nsamp, nrec), dtype=float) fname = 'hw4_sol.txt' file = open(fname, mode='w') # Thanks to Patricio Cubillos for finding a way to do this in ONE # loop! First, he generates all 10 samples in each set with a single # command. Then he uses the axis= keyword to mean() and std() to take # the mean along just one axis of the resulting array, generating a # mean or standard deviation for each array row. He assembles these # with the np.array() command and then transposes it to get the right # format, and saves the data. Read the help page for np.mean() to # learn about the axis= keyword. Doing this in two loops is

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/09/2009 for the course AST 4762 taught by Professor Harrington during the Fall '09 term at University of Central Florida.

### Page1 / 4

hw4_sol - #! /usr/bin/env python # AST5765/4762 2009 HW4...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online