lec20_classes

lec20_classes - try from numpy import except...

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

# Example of an electric Charge class. All statements in '__init__' are # executed as soon as a Charge instance is created (e.g. q1=Charge() ) class Charge: def __init__(self): #Provide some default properties which can be changed later #Everything in here will be executed when a "Charge" instance #is created. self.charge = 1.0 self.mass = 1.0 def setCharge(self,value): self.charge = value def getCharge(self): return self.charge def getPolarity(self): if self.charge > 0.0: return '+' elif self.charge == 0.0: return '0' else: return '-' def switchPolarity(self): self.charge = - self.charge def setMass(self,value): self.mass = value def getMass(self): return self.mass # Example of an Integral class which uses either Trapazoid # or Simpson's 1/3 rule to evaluate an integral

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.

Unformatted text preview: try: from numpy import * except: print "ERROR: The Integral class requires the Numpy module." class Integral: ''' Example of an Integral class which uses either Trapazoid or Simpson's 1/3 rule to evaluate an integral. ''' def __init__(self,funct,lower,upper,N): self.lower = lower self.upper = upper self.h = (upper - lower)/float(N-1) self.numpts = int(N) self.x = linspace(self.lower,self.upper,self.numpts) self.y = funct(self.x) def evalTrap(self): self.Isum=0. for i in range(self.numpts-1): self.Isum+=(self.y[i+1]+self.y[i])*self.h/2. return self.Isum def evalSimp13(self): self.Isum=0. for i in range(0,self.numpts-2,2): self.Isum+=(self.y[i]+4*self.y[i+1]+self.y[i+2])*self.h/3 return self.Isum class Work(Integral): pass...
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

lec20_classes - try from numpy import except...

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

View Full Document
Ask a homework question - tutors are online