lec20_classes

lec20_classes - try: from numpy import * except: print...

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

View Full Document Right Arrow Icon
# 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
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: 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

Page1 / 2

lec20_classes - try: from numpy import * except: print...

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