Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Nov 20 09:49:10 2017
  4.  
  5. @author: Eben Myrddin
  6. """
  7.  
  8.  
  9. #Q1
  10.  
  11. import numpy as np
  12. import matplotlib.pyplot as plt
  13. import scipy
  14.  
  15. plt.close("all")
  16.  
  17. #myarray = np.fromfile('dat1.dat',dtype=float, count=-1) #this hasn't worked at all.
  18. data=np.loadtxt("dat1.dat",skiprows=1)
  19.  
  20.  
  21. #plt.plot(myarray)
  22.  
  23. X=data[:,0]
  24. Y=data[:,1]
  25. sig=data[:,2]
  26.  
  27. plt.plot(X,Y,'x')
  28. plt.errorbar(X,Y,yerr=sig, linestyle="None")
  29.  
  30. #graph formatting
  31. plt.title("plot of data", fontsize=20)
  32. plt.xlabel("X", fontsize=20)
  33. plt.ylabel("Y", fontsize=20)
  34.  
  35.  
  36.  
  37.  
  38.  
  39. """
  40. I thought it would be a good idea to use an off the shelf function to
  41. find a value for the fit with which to compare my own method.
  42.  
  43. polyfit uses a least squares polynomial fit
  44.  
  45. """
  46. ComparisonCoeffs=np.polyfit(X,Y,1)
  47.  
  48.  
  49. #define function for chai^2
  50.  
  51. def chaisquared(x,y,A,B,sigy):
  52.     return sum(((y-A-B*x)**2)/(sigy**2))
  53.    
  54. #we want to minimise chai sq
  55.  
  56. N=len(Y) #100 data points
  57.  
  58. #now define function for finding separate coeffs
  59.  
  60. def findA(x,y,N):
  61.     numerator=sum(x**2)*sum(y)-sum(x)*sum(x*y)
  62.     denominator=N*sum(x**2)-(sum(x))**2
  63.     return numerator/denominator #checked
  64.  
  65. def findB(x,y,N):
  66.     num=N*sum(x*y)-sum(x)*sum(y)
  67.     denom=N*sum(x**2)-(sum(x))**2
  68.     return num/denom #checked
  69.  
  70. #get on with finding them
  71.  
  72. A=findA(X,Y,N)
  73. B=findB(X,Y,N)
  74.  
  75. """
  76. Values compare very favourably with the polyfit least squares method
  77. (8 decimal places of agreement)
  78. """
  79.  
  80. #next, must find error in y value
  81.  
  82.  
  83. #######################################################################
  84.  
  85.  
  86. def findsigY(N,Y,X,A,B):
  87.     return(np.sqrt(sum((Y-A-B*X)**2)/(N-2)))
  88.  
  89. sigy=findsigY(N,Y,X,A,B)
  90. #######################################################################
  91.  
  92.  
  93.  
  94.  
  95.  
  96. print "The A and B values given by my equations are", round(A,5), round(B,5), "respectively"
  97.  
  98.  
  99. print "This compares to off-the-shelf least squares fit values of", round(ComparisonCoeffs[1],5),"and",round(ComparisonCoeffs[0],5),"respectively."
  100.  
  101. print "They clearly use the same method.", "\n", "The error in y (sigma y) was found to be equal to", sigy
  102.  
  103. #error in A
  104. sigA=sigy*np.sqrt(sum(X**2)/(N*sum(X**2)-(sum(X)**2)))
  105.  
  106. #error in B
  107. sigB=sigy*np.sqrt(N/(N*sum(X**2)-(sum(X)**2)))
  108.  
  109. #F*ck it let's plot
  110.  
  111.  
  112. x=np.arange(0,1,0.01)
  113. y=B*x+A
  114. plt.plot(x,y, label="line plotted based on least squares fit") #seems to fit quite nicely.
  115.  
  116. plt.legend(loc=0)
  117.  
  118.  
  119. print "\n \n \n Finally, errors for the paramters A and B are", round(sigA,5), round(sigB,5), "and the error in y is", round(sigy,5)
  120.  
  121.  
  122. """
  123. B-B-B-BINGO!
  124. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement