hw6_solns - diagonal covariance errbars=20 #Computer...

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

View Full Document Right Arrow Icon
############ # Solutions for HW#6 by JRG ############ from pylab import * from scipy.optimize import curve_fit def sumsqr(x,y,model,params): sum = 0.0 for i in range(len(y)): sum += (y[i] - model(x[i],*params))**2 return sum def model(t,A,tau,f0): return A*(1.0-exp(-t/tau)) + f0 #Load data from file data=np.loadtxt('freq_time.dat') xdata=data[:,0] ydata=data[:,1] #Run fit #Initial guess (starting point) params0=[1000,10,ydata[0]] #Using curve_fit fit=curve_fit(model,xdata,ydata,p0=params0) fitparams=fit[0] A_fit=fitparams[0] tau_fit=fitparams[1] f0_fit=fitparams[2] #Generate fit curve xfit=linspace(min(xdata),max(xdata),200) yfit=model(xfit,A_fit,tau_fit,f0_fit) #Generate plots plot(xdata,ydata,'o') plot(xfit,yfit,'-') xlabel('Time (min)') ylabel('Frequency (Hz)') #Fit statistics cov=fit[1] #Covariance matrix dof=len(xdata)-len(fitparams) #degrees of freedom chisqr=sumsqr(xdata,ydata,model,fitparams) #chi-square is sum of squares of
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: diagonal covariance errbars=20 #Computer uncertainties in estimated parameters from covariance matrix and reduced chisqr del_A=sqrt(cov[0,0]*sqrt(chisqr/dof)) del_tau=sqrt(cov[1,1]*sqrt(chisqr/dof)) del_f0=sqrt(cov[2,2]*sqrt(chisqr/dof)) print 'A = %2.3f +\- %1.3f Hz (%2.2f %%)' % (A_fit,del_A,del_A/A_fit*100.) print 'tau = %2.3f +\- %1.3f minutes (%2.2f %%)' % (tau_fit,del_tau,del_tau/tau_fit*100.) print 'f0 = %2.3f +\- %1.3f Hz (%2.2f %%)' % (f0_fit,del_f0, del_f0/f0_fit*100.) ############## #Prob. 3 ############## def avg(x): return sum(x)/float(len(x)) def sst(x): sum=0.0 xbar=avg(x) for i in range(len(x)): sum += (x[i]-xbar)**2 return sum r=sqrt(1.0-sumsqr(xdata,ydata,model,fitparams)/sst(ydata)) rsq=r**2 print print 'Correlation coefficient (r^2) is: %1.3f'%rsq #sigma = sqrt(sumsqr(xdata,ydata,model,fitparams)/(len(ydata)-3.)) #error=sigma/avg(ydata)*100...
View Full Document

Page1 / 2

hw6_solns - diagonal covariance errbars=20 #Computer...

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