Advertisement
Guest User

dff

a guest
Sep 12th, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.92 KB | None | 0 0
  1. import numpy
  2. import pylab
  3. import matplotlib.pyplot
  4. import scipy.optimize
  5. from scipy.optimize import curve_fit
  6.  
  7. ''' A Program That Determines The Reduced Chi Squared Value For Various Theoretical Models.'''
  8. '''The Best Fit Parameters Are Derived Using Levenberg-Marquardt Algorithm Which Solves The Non-Linear Least Squares Problem.'''
  9.  
  10. #Recorded Observed Data
  11. xdata = numpy.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  12. ydata = numpy.array([-10, -7, 1, 5, 7, 7, 6, 7, 10, 15])
  13. xerror = numpy.array([0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3])
  14. yerror = numpy.array([0.1, 0.1, 0.4, 0.3, 0.2, 0.1, 0.1, 0.2, 0.1, 0.1])
  15.  
  16.    
  17.     #Theoretical Models To Be Tested
  18.     def test_linear_model():
  19.         print 'Testing linear model', '\n'
  20.  
  21.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Linear Model
  22.         def func(x, a, b):
  23.             return a*x + b
  24.  
  25.         #Constants Of Theoretical Model
  26.         popt, pcov = curve_fit(func, xdata, ydata)
  27.         print 'Constant Ax is',("%.2f" %popt[0])
  28.         print 'Constant B is',("%.2f" %popt[1]), '\n'
  29.         return func, popt
  30.  
  31.     def test_quadratic_model():
  32.         print 'Testing quadratic model', '\n'
  33.        
  34.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Quadratic Model Polynomial Model
  35.         def func(x, a, b, c):
  36.             return a*x**2 + b*x + c
  37.    
  38.             #Constants Of Theoretical Model
  39.         popt, pcov = curve_fit(func, xdata, ydata)
  40.         print 'Constant Ax^2 is', ("%.2f" %popt[0])
  41.         print 'Constant Bx is', ("%.2f" %popt[1])
  42.         print 'Constant C is', ("%.2f" %popt[2]), '\n'
  43.  
  44.     def test_cubic_model():
  45.         print 'Testing cubic model', '\n'
  46.    
  47.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Cubic Model Polynomial Model
  48.         def func(x, a, b, c, d):
  49.             return a*x**3 + b*x**2 + c*x + d
  50.    
  51.         #Constants Of Theoretical Model
  52.         popt, pcov = curve_fit(func, xdata, ydata)
  53.         print 'Constant Ax^3 is', ("%.2f" %popt[0])
  54.         print 'Constant Bx^2 is', ("%.2f" %popt[1])
  55.         print 'Constant Cx is', ("%.2f" %popt[2])
  56.         print 'Constant D is', ("%.2f" %popt[3]), '\n'
  57.  
  58.     def test_quartic_model():
  59.         print 'Testing quartic model', '\n'
  60.    
  61.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Quartic Polynomial Model
  62.         def func(x, a, b, c, d, e):
  63.             return a*x**4 + b*x**3 + c*x**2 + d*x + e
  64.    
  65.         #Constants Of Theoretical Model
  66.         popt, pcov = curve_fit(func, xdata, ydata)
  67.         print 'Constant Ax^4 is', ("%.2f" %popt[0])
  68.         print 'Constant Bx^3 is', ("%.2f" %popt[1])
  69.         print 'Constant Cx^2 is', ("%.2f" %popt[2])
  70.         print 'Constant Dx is', ("%.2f" %popt[3])
  71.         print 'Constant E is', ("%.2f" %popt[4]), '\n'
  72.  
  73.     def test_quintic_model():
  74.         print 'Testing quintic model', '\n'
  75.    
  76.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Quintic Polynomial Model
  77.         def func(x, a, b, c, d, e, f):
  78.             return a*x**5 + b*x**4 + c*x**3 + d*x**2 + e*x + f
  79.    
  80.         #Constants Of Theoretical Model
  81.         popt, pcov = curve_fit(func, xdata, ydata)
  82.         print 'Constant Ax^5 is', ("%.2f" %popt[0])
  83.         print 'Constant Bx^4 is', ("%.2f" %popt[1])
  84.         print 'Constant Cx^3 is', ("%.2f" %popt[2])
  85.         print 'Constant Dx^2 is', ("%.2f" %popt[3])
  86.         print 'Constant Ex is', ("%.2f" %popt[4])
  87.         print 'Constant F is', ("%.2f" %popt[5]), '\n'
  88.  
  89.     def test_logarithmic_model():
  90.         print 'Testing logarithmic model', '\n'
  91.    
  92.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Logarithmic Model
  93.         def func(x, a, b):
  94.             return a*numpy.log(x)+ b
  95.    
  96.         #Constants Of Least Squared Theoretical Model
  97.         popt, pcov = curve_fit(func, xdata, ydata)
  98.         print 'Constant A is', ("%.2f" %popt[0])
  99.         print 'Constant Bx is', ("%.2f" %popt[1]), '\n'
  100.        
  101.     def test_powerlaw_model():
  102.         print 'Testing power law model', '\n'
  103.    
  104.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Power Law Model
  105.         def func(x, a, b):
  106.             return a*(x**b)
  107.    
  108.         #Constants Of Least Squared Theoretical Model
  109.         popt, pcov = curve_fit(func, xdata, ydata)
  110.         print 'Constant A is', ("%.2f" %popt[0])
  111.         print 'Constant B is', ("%.2f" %popt[1]), '\n'
  112.        
  113.     def test_exponential_model():
  114.         print 'Testing exponential model', '\n'
  115.    
  116.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Exponential Model
  117.         def func(x, a, b, c):
  118.             return a*numpy.exp(b*x + c)
  119.    
  120.         #Constants Of Least Squared Theoretical Model
  121.         popt, pcov = curve_fit(func, xdata, ydata)
  122.         print 'Constant A is', ("%.2f" %popt[0])
  123.         print 'Constant Bx is', ("%.2f" %popt[1])
  124.         print 'Constant C is', ("%.2f" %popt[2]), '\n'
  125.  
  126.     def test_exponential_model():
  127.         print 'Testing exponetial offset model', '\n'
  128.    
  129.         #Scipy Optimise cuve_fit Model Produces Expected Values Using A Exponential Offset Model
  130.         def func(x, a, b, c, d):
  131.             return a*numpy.exp(b*x + c) + d
  132.    
  133.         #Constants Of Least Squared Theoretical Model
  134.         popt, pcov = curve_fit(func, xdata, ydata)
  135.         print 'Constant A is', ("%.2f" %popt[0])
  136.         print 'Constant Bx is', ("%.2f" %popt[1])
  137.         print 'Constant C is', ("%.2f" %popt[2])
  138.         print 'Constant D is', ("%.2f" %popt[3]), '\n'
  139.  
  140. def main():
  141.     commands = {
  142.         1: test_linear_model,
  143.         2: test_quadratic_model,
  144.         3: test_cubic_model,
  145.         4: test_quartic_model,
  146.         5: test_quintic_model,
  147.         6: test_logarithmic_model,
  148.         7: test_powerlaw_model,
  149.         8: test_exponential_model,
  150.         9: test_exponentialoffset_model,
  151.     }
  152.  
  153.     # User Prompted Input Values
  154.     test = int(raw_input("Enter A Number Assigned Theoretical Model To Test: "))
  155.     func, popt = commands[test]()
  156.  
  157.  
  158.     #Derived Chi Squared Value For This Model
  159.     chi_squared = numpy.sum(((func(xdata, *popt) - ydata) / xerror) ** 2)
  160.     reduced_chi_squared = chi_squared / (len(xdata) - len(popt))
  161.     print 'The degrees of freedom for this test is', len(xdata) - len(popt)
  162.     print 'The chi squared value is: ', ("%.2f" % chi_squared)
  163.     print 'The reduced chi squared value is: ', ("%.2f" % reduced_chi_squared)
  164.  
  165.  
  166.     #Observed Values Are Plotted With Expected Values
  167.     matplotlib.pyplot.figure()
  168.     matplotlib.pyplot.scatter(xdata, ydata, s=0)
  169.     matplotlib.pyplot.plot(xdata,func(xdata, *popt), label='Theoretical Model')
  170.     matplotlib.pyplot.errorbar(xdata, ydata, xerr=xerror, yerr=yerror, linestyle="", color='red')
  171.     matplotlib.pyplot.xlabel(' Observed Values For x ')
  172.     matplotlib.pyplot.ylabel(' f(x)')
  173.     matplotlib.pyplot.legend(loc='lower right')
  174.     matplotlib.pyplot.show()      
  175.  
  176. if __name__ == '__main__':
  177.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement