Skip to content

Instantly share code, notes, and snippets.

@JJL772
Last active April 6, 2020 20:44
Show Gist options
  • Save JJL772/f27687e5f011ebd1c478e2a880a333b8 to your computer and use it in GitHub Desktop.
Save JJL772/f27687e5f011ebd1c478e2a880a333b8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Plots a dataset of voltage/current measurements to test ohm's law
import matplotlib, sys, os, math, random, matplotlib.pyplot
import matplotlib.pyplot as plt
import numpy as np
# Measured voltages (No epsilon added)
datapointsx = [
1.5,
3.05,
2.6,
7.42,
6.47
]
# Measured currents (No epsilon added)
datapointsy = [
0.15,
0.31,
0.26,
0.74,
0.65,
]
orig = []
for x in datapointsy:
orig.append(x)
# Epsilon values
current_epsilon_max = 0.009
current_epsilon_min = -0.009
# Add random epsilon
datapointsy[:] = [x + random.uniform(current_epsilon_max, current_epsilon_min) for x in datapointsy]
for k,v in zip(orig, datapointsy):
print(v-k)
# Draw best fit line
plt.plot(np.unique(datapointsx), np.poly1d(np.polyfit(datapointsx, datapointsy, 1))(np.unique(datapointsx)))
# Now draw a scatter plot of all data
plt.scatter(datapointsx, datapointsy)
plt.ylabel("Current (A)")
plt.xlabel("Voltage Drop (V)")
plt.ylim(0,1)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment