#quote from 'introduction to computation and programming
#using Python, revised, MIT press
import pylab
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 fitData(inputFile):
masses, distances = getData(inputFile)
distances = pylab.array(distances)
masses = pylab.array(masses)
forces = masses*9.81
pylab.plot(forces, distances, 'bo',
label = 'Measured displacement')
pylab.title('Measured Displacement of Spring')
pylab.xlabel('|Force| (Newton)')
pylab.ylabel('Distance (meters)')
#find linear fit
a,b = pylab.polyfit(forces, distances, 1)
predictedDistances = a*pylab.array(forces) + b
k = 1.0/a
pylab.plot(forces, predictedDistances,
label = 'Displacements predicted by\nlinear fit, k = '
+ str(round(k, 5)))
#find cubic fit
a,b,c,d = pylab.polyfit(forces, distances, 3)
predictedDistances = a*(forces**3) + b*forces**2 + c*forces + d
pylab.plot(forces, predictedDistances, 'b:', label = 'cubic fit')
pylab.legend(loc = 'best')
fitData('springData.txt')
pylab.show()
best fit line
最新推荐文章于 2023-01-11 15:52:59 发布