import pylab, random def stdDev(X): mean = sum(X)/float(len(X)) tot = 0.0 for x in X: tot += (x - mean)**2 return (tot/len(X))**0.5 def throwNeedles(numNeedles): inCircle = 0 estimates = [] for Needles in xrange(1, numNeedles + 1, 1): x = random.random() y = random.random() if (x*x + y*y)**0.5 <= 1.0: inCircle += 1 return 4*(inCircle/float(Needles)) def estPi(precision = 0.02, numTrials = 20): numNeedles = 1000 numTrials = 20 sDev = precision while sDev >= (precision/4.0): estimates = [] for t in range(numTrials): piGuess = throwNeedles(numNeedles) estimates.append(piGuess) sDev = stdDev(estimates) curEst = sum(estimates)/len(estimates) curEst = sum(estimates)/len(estimates) print 'Est. = ' + str(curEst) +\ ', Std. dev. = ' + str(sDev)\ + ', Needles = ' + str(numNeedles) numNeedles *= 2 return curEst ##estPi() def getData(fileName): dataFile = open(fileName, 'r') distances = [] masses = [] discardHeader = dataFile.readline() for line in dataFile: d, m = line.split() distances.append(float(d)) masses.append(float(m)) dataFile.close() return (masses, distances) def plotData(fileName): xVals, yVals = getData(fileName) xVals = pylab.array(xVals) yVals = pylab.array(yVals) xVals = xVals*9.81 #acc. due to gravity pylab.plot(xVals, yVals, 'bo', label = 'Measured displacements') pylab.title('Measured Displacement of Spring') pylab.xlabel('|Force| (Newtons)') pylab.ylabel('Distance (meters)') ##plotData('springData.txt') ##pylab.show() def fitData(fileName): xVals, yVals = getData(fileName) xVals = pylab.array(xVals) yVals = pylab.array(yVals) xVals = xVals*9.81 pylab.plot(xVals, yVals, 'bo', label = 'Measured displacements') pylab.title('Measured Displacement of Spring') pylab.xlabel('|Force| (Newtons)') pylab.ylabel('Distance (meters)') a,b = pylab.polyfit(xVals, yVals, 1) estYVals = a*pylab.array(xVals) + b k = 1/a pylab.plot(xVals, estYVals, label = 'Linear fit, k = ' + str(round(k, 5))) pylab.legend(loc = 'best') ##fitData('springData.txt') ##pylab.show() def fitData1(fileName): xVals, yVals = getData(fileName) xVals = pylab.array(xVals) yVals = pylab.array(yVals) xVals = xVals*9.81 pylab.plot(xVals, yVals, 'bo', label = 'Measured displacements') pylab.title('Measured Displacement of Spring') pylab.xlabel('|Force| (Newtons)') pylab.ylabel('Distance (meters)') a,b = pylab.polyfit(xVals, yVals, 1) estYVals = a*pylab.array(xVals) + b #pylab.plot(xVals, estYVals, label = 'Linear fit') a,b,c,d = pylab.polyfit(xVals, yVals, 3) estYVals = a*(xVals**3) + b*xVals**2 + c*xVals + d pylab.plot(xVals, estYVals, label = 'Cubic fit') pylab.legend(loc = 'best') fitData1('springData.txt') pylab.show() def fitData2(fileName): xVals, yVals = getData(fileName) extX = pylab.array(xVals + [1.5]) xVals = pylab.array(xVals) yVals = pylab.array(yVals) xVals = xVals*9.81 extX = extX*9.81 pylab.plot(xVals, yVals, 'bo', label = 'Measured displacements') pylab.title('Measured Displacement of Spring') pylab.xlabel('|Force| (Newtons)') pylab.ylabel('Distance (meters)') a,b = pylab.polyfit(xVals, yVals, 1) extY = a*pylab.array(extX) + b pylab.plot(extX, extY, label = 'Linear fit') a,b,c,d = pylab.polyfit(xVals, yVals, 3) extY = a*(extX**3) + b*extX**2 + c*extX + d pylab.plot(extX, extY, label = 'Cubic fit') pylab.legend(loc = 'best') ##fitData2('springData.txt') ##pylab.show() def getTrajectoryData(fileName): dataFile = open(fileName, 'r') distances = [] heights1, heights2, heights3, heights4 = [],[],[],[] discardHeader = dataFile.readline() for line in dataFile: d, h1, h2, h3, h4 = line.split() distances.append(float(d)) heights1.append(float(h1)) heights2.append(float(h2)) heights3.append(float(h3)) heights4.append(float(h4)) dataFile.close() return (distances, [heights1, heights2, heights3, heights4]) def tryFits(fName): distances, heights = getTrajectoryData(fName) distances = pylab.array(distances)*36 totHeights = pylab.array([0]*len(distances)) for h in heights: totHeights = totHeights + pylab.array(h) pylab.title('Trajectory of Projectile (Mean of 4 Trials)') pylab.xlabel('Inches from Launch Point') pylab.ylabel('Inches Above Launch Point') meanHeights = totHeights/float(len(heights)) pylab.plot(distances, meanHeights, 'bo') a,b = pylab.polyfit(distances, meanHeights, 1) altitudes = a*distances + b pylab.plot(distances, altitudes, 'r', label = 'Linear Fit') a,b,c = pylab.polyfit(distances, meanHeights, 2) altitudes = a*(distances**2) + b*distances + c pylab.plot(distances, altitudes, 'g', label = 'Quadratic Fit') pylab.legend() ##tryFits('launcherData.txt') ##pylab.show() def rSquare(measured, estimated): """measured: one dimensional array of measured values estimate: one dimensional array of predicted values""" EE = ((estimated - measured)**2).sum() mMean = measured.sum()/float(len(measured)) MV = ((mMean - measured)**2).sum() return 1 - EE/MV def tryFits1(fName): distances, heights = getTrajectoryData(fName) distances = pylab.array(distances)*36 totHeights = pylab.array([0]*len(distances)) for h in heights: totHeights = totHeights + pylab.array(h) pylab.title('Trajectory of Projectile (Mean of 4 Trials)') pylab.xlabel('Inches from Launch Point') pylab.ylabel('Inches Above Launch Point') meanHeights = totHeights/float(len(heights)) pylab.plot(distances, meanHeights, 'bo') a,b = pylab.polyfit(distances, meanHeights, 1) altitudes = a*distances + b pylab.plot(distances, altitudes, 'r', label = 'Linear Fit' + ', R2 = ' + str(round(rSquare(meanHeights, altitudes), 4))) a,b,c = pylab.polyfit(distances, meanHeights, 2) altitudes = a*(distances**2) + b*distances + c pylab.plot(distances, altitudes, 'g', label = 'Quadratic Fit' + ', R2 = ' + str(round(rSquare(meanHeights, altitudes), 4))) pylab.legend() ##tryFits1('launcherData.txt') ##pylab.show() def getXSpeed(a, b, c, minX, maxX): """minX and maxX are distances in inches""" xMid = (maxX - minX)/2.0 yPeak = a*xMid**2 + b*xMid + c g = 32.16*12 #accel. of gravity in inches/sec/sec t = (2.0*yPeak/g)**0.5 return xMid/(t*12.0) #print 'speed = ' + str(int(xMid/(t*12))) + ' feet/sec' def processTrajectories(fName): distances, heights = getTrajectoryData(fName) distances = pylab.array(distances)*36 totHeights = pylab.array([0]*len(distances)) for h in heights: totHeights = totHeights + pylab.array(h) pylab.title('Trajectory of Projectile (Mean of 4 Trials)') pylab.xlabel('Inches from Launch Point') pylab.ylabel('Inches Above Launch Point') meanHeights = totHeights/len(heights) pylab.plot(distances, meanHeights, 'bo') a,b,c = pylab.polyfit(distances, meanHeights, 2) altitudes = a*(distances**2) + b*distances + c speed = getXSpeed(a, b, c, distances[-1], distances[0]) pylab.plot(distances, altitudes, 'g', label = 'Quad. Fit' + ', R2 = ' + str(round(rSquare(meanHeights, altitudes), 2)) + ', Speed = ' + str(round(speed, 2)) + 'feet/sec') pylab.legend() ##processTrajectories('launcherData.txt') ##pylab.show()