from numpy import * import matplotlib.pylab as plt from quadLSQ import quadLSQ #Linear function, derivation p. 162, equation 5.58 p. 163. t = linspace(1,5,5) y = array([55.2, 44.9, 37.9, 35.3, 30.1]) alpha,beta = quadLSQ(t,y) print 'Linear approximation: y = %.2f %.2ft' %(alpha,beta) def linear(t): return alpha + beta*t # c) z = log(y) # d) gamma,beta2 = quadLSQ(t,z) alpha2 = exp(gamma) print 'Exp approximation: y = %.2f exp(%.2ft)' %(alpha2,beta2) def expLS(t): return alpha2*exp(beta2*t) T = linspace(1,5,501) plt.plot(t,y,'o',T,linear(T),T,expLS(T)) plt.legend(['data','app 1','app2']) plt.show()
from numpy import * from quadLSQ import quadLSQ n = array([65536*i for i in [1,2,4,8,16]]) Tn = array([0.048755, 0.097074, 0.194003, 0.386721, 0.771487]) a,b = quadLSQ(n,Tn) print 'Linear approximation: T(n) = %g + %gn'%(a,b) def T(n): return a+b*n #b) Sending a vector of length 0 still takes time #c) double = 64 print (double/b)*10**(-9)
from numpy import * from quadLSQ import quadLSQ n = linspace(100, 400, 4) # NOTE: n measured in thousands CPU = array([0.05, 0.09, 0.13, 0.18]) a, b = quadLSQ(n, CPU) print "Linear approximation: c(n) = %g + %g n " % (a, b) def CPUtime(n): return a + b * n print "n = 10^6 --> estimated CPU time: %.2f " % CPUtime(10 ** 6) print "n = 10^7 --> estimated CPU time: %.2f " % CPUtime(10 ** 7)