lec4_code - return minnum, maxnum, avg, sd #returns a tuple...

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

View Full Document Right Arrow Icon
def sumnums(*nums): sum=0 for num in nums: sum+=num return sum #can call functions from other functions def myavg(*nums): #pass the sequence *nums just as it is, otherwise it sends a tuple object to sumnums sum=float(sumnums(*nums)) #convert to float to avoid division problems return sum/len(nums) #Define this and have students write it. sigma=sqrt(1/N sum_i^N (x_i - xavg)^2) def stddev(nums): ''' sigma=stddev(sequence) returns the standard deviation of the sequence of numbers ''' from math import sqrt N=len(nums) avgnum=myavg(*nums) sum=0. for num in nums: sum+=(num-avgnum)**2 return sqrt(sum/N) def stats(nums): avg=myavg(*nums) maxnum=max(nums) minnum=min(nums) sd=stddev(nums) #Define this and have students write it.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: return minnum, maxnum, avg, sd #returns a tuple with all the values #Then assign by lowest,highest,average,sd=stats(range(0,20)) #Test with a gaussian distributed random sequence: random.gauss(mean,sd) def randseq(mean,sd,number,type='g'): import random seq= if type=='g': for i in range(number): seq.append(random.gauss(mean,sd)) elif type=='u': for i in range(number): seq.append(random.uniform(mean,sd)) else: print "The type of distribution is 'g' for gaussian (default) or 'u' for uniform" return return seq randlst=randseq(50,3,1000) stddev(randlst) #should be about 3 #pylab has a histogram function from pylab import * hist(randlst)...
View Full Document

This note was uploaded on 10/05/2010 for the course PHYS phy503 taught by Professor Gladden during the Spring '09 term at Ole Miss.

Page1 / 2

lec4_code - return minnum, maxnum, avg, sd #returns a tuple...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online