Advertisement
Guest User

Python lab script

a guest
Nov 12th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #*************************************************************
  2. #
  3. # Experiment: E3, Measuring an electric current, voltage and resistance.
  4. # Names: Fraser McDonald & Robert Kyle
  5. # Date: 06/11/2018
  6. #
  7. #*************************************************************
  8.  
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11. import lab1
  12.  
  13.  
  14.  
  15. #********************************************************
  16. # Load the data from file "E3data.txt"
  17. # Create an numpy array "d" with the voltage in the 1st
  18. # column and the current in the 2nd column.
  19. #
  20. # d = ....
  21. #********************************************************
  22.  
  23. d = np.loadtxt("E2data.txt")
  24.  
  25.  
  26. #********************************************************
  27. # Use d to create variables for the voltage and current
  28. # Use the variable names V and I.
  29. #
  30. # What are the units?
  31. #
  32. # suggestion: V = ...
  33. # I = ...
  34. #********************************************************
  35.  
  36. V=d[:,0]
  37. I=d[:,1]
  38.  
  39. #********************************************************
  40. # Convert V and I to SI units (Volts and Ampere) if necessary
  41. #
  42. # suggestion: V = V * ...; I = I * ...;
  43. #********************************************************
  44.  
  45.  
  46.  
  47.  
  48.  
  49. #********************************************************
  50. # Plot your raw data with
  51. # (a) V on the x-axis
  52. # (b) I on the y-axis.
  53. # (c) circle symbol for the data points
  54. # (e) labels for x and y axes
  55. # (f) a helpful title
  56. #********************************************************
  57. plt.figure(1)
  58. plt.plot(V,I,"ro")
  59. plt.xlabel("Voltage (V)")
  60. plt.ylabel("Current (a)")
  61. plt.title("Relationship between Voltage and Current")
  62.  
  63.  
  64. #********************************************************
  65. # Create a linear fit for the data:
  66. #
  67. # Use the command c,m,dm = lab1.fitLine( V, I)
  68. #
  69. # m - is the gradient of the fitted line.
  70. # c - is the offset of the fitted line.
  71. # dm - is the the error of the gradient.
  72. #********************************************************
  73. c,m,dm = lab1.fitLine( V, I)
  74.  
  75.  
  76.  
  77. #********************************************************
  78. # Plot the straight line on top of your linearized data:
  79. # (a) generate an array with x-data points, call it "xr"
  80. # e.g. from 1 to 1.5 with 0.1 spacint
  81. # xr = np.arange(1, 1.5, 0.1)
  82. # (b) calculate the corresponding y-values for a straight
  83. # line, e.g.
  84. # yr = m*xr + c
  85. # (c) plot yr vs. xr with a line style
  86. #********************************************************
  87. xr= np.arange(0.4,1.5,0.005)
  88. yr=(m * xr) + c
  89. plt.plot(xr,yr,"-")
  90.  
  91.  
  92. #********************************************************
  93. # Calculate the resitance R
  94. #
  95. # Use the eq. 3 of the script (I = 1/R*V) to determine R
  96. # from the gradient of you fit (y = m *x + c).
  97. #
  98. # R = ...
  99. #********************************************************
  100.  
  101. print("Gradient = ",m)
  102. R = 1/m
  103.  
  104. #********************************************************
  105. # Calculate the error of R
  106. #
  107. # (Help : The error dR of the derived quatity R = 1/m depends
  108. # on the error dm of parameter m with dR/R = dm/m.)
  109. #
  110. # dR = dm/m * ...
  111. #********************************************************
  112.  
  113. dR=(dm/m*R)
  114. print("Measurement Results")
  115. print('R',R)
  116. print("dR",dR)
  117.  
  118. #********************************************************
  119. # do not forget to print this script and the figures
  120. #********************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement