Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import numpy as np
  2. from numpy import sqrt, pi, exp, linspace, loadtxt, arctan
  3. from lmfit import Model
  4.  
  5. import matplotlib.pyplot as plt
  6.  
  7. #import data
  8. data = loadtxt('lab3_data1.txt')
  9. frequency = data[:, 0]
  10. Vin = data[:,1]
  11. Vout = data[:,4]
  12. phase = data[:, 5]
  13. VoutVin = data[:,6]
  14.  
  15. #plotting Vout vs. frequency (High Pass) experimental data
  16. plt.figure(1)
  17. plt.plot(frequency*(2*np.pi) ,VoutVin, 'ro')
  18. plt.xlabel('Angular Frequency')
  19. plt.ylabel('Vout/Vin')
  20. plt.show()
  21.  
  22. #plotting phase vs. frequency (High Pass) experimental data
  23. plt.figure(2)
  24. plt.plot(frequency*(2*np.pi),phase, 'rx')
  25. plt.xlabel('Angular Frequency (Hz)')
  26. plt.ylabel('Phase (rad)')
  27. plt.show()
  28.  
  29. #expected data
  30. F = np.arange(50,20000,50)
  31. RC = 470*0.00000022
  32. P = np.arctan(1/(RC*F*2*np.pi))
  33.  
  34. plt.figure(3)
  35. plt.plot(F,P,'bx',frequency, phase, 'rx')
  36. plt.xlabel('Frequency')
  37. plt.ylabel('Phase Expected')
  38. plt.show()
  39.  
  40. import numpy as np
  41. from scipy.optimize import curve_fit
  42. from matplotlib import pylab
  43.  
  44.  
  45.  
  46. #Now fitting measured data for amplitude ratio to model, allowing RC
  47. to vary
  48.  
  49. ratio1 = (F*RC)/((1+(F*RC)**2)**.5)
  50.  
  51. def ratio(frequency, RC):
  52. return ((frequency*RC)/(1+(frequency*RC)**2)**.5)
  53. model = Model(ratio)
  54.  
  55.  
  56. result =model.fit(ratio, frequency=frequency)
  57.  
  58. plt.figure(4)
  59. plt.plot(frequency,ratio,'g.')
  60. plt.xlabel('angular frequency')
  61. plt.ylabel('voltage ratio')
  62. plt.show()
  63. plt.plot(frequency, result.init_fit, 'r--', linewidth=1)
  64. plt.show()
  65. plt.plot(frequency, result.best_fit, 'b-', linewidth=2)
  66. plt.show()
  67. plt.legend()
  68. plt.show()
  69.  
  70. print result.fit_report()
  71. fig=result.plot()
  72. fig.show()
  73. #---------------------------------------------------------------
  74. #Now fitting measured data for phase to model, allowing RC to vary
  75. ratio1 = (F*RC)/((1+(F*RC)**2)**.5)
  76.  
  77. def phasefit(frequency, RC):
  78. return (np.arctan(1/(frequency*RC)))
  79. model = Model(phasefit)
  80.  
  81. result =model.fit(phase, frequency=frequency)
  82.  
  83. plt.figure(5)
  84. plt.plot(frequency,phase,'g.')
  85. plt.xlabel('angular frequency')
  86. plt.ylabel('phase')
  87. plt.show()
  88. plt.plot(frequency, result.init_fit, 'r--', linewidth=1)
  89. plt.show()
  90. plt.plot(frequency, result.best_fit, 'b-', linewidth=2)
  91. plt.show()
  92. plt.legend()
  93. plt.show()
  94.  
  95. print result.fit_report()
  96. fig=result.plot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement