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

Info icon This 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
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern