Skip to content

Instantly share code, notes, and snippets.

@Shoeboxam
Last active August 29, 2015 14:19
Show Gist options
  • Save Shoeboxam/dbc9f65e3f5580279790 to your computer and use it in GitHub Desktop.
Save Shoeboxam/dbc9f65e3f5580279790 to your computer and use it in GitHub Desktop.
Spectrophotometry Lab
import matplotlib.pyplot as plt
import numpy
from numpy.linalg import lstsq
concentrations = numpy.asarray((.25, .20, .15, .1, .05))
absorbtivities = numpy.asarray((.131, .129, .126, .119, .109))
# Prepare data for fitting
conc_vstack = numpy.vstack([concentrations, numpy.ones(len(concentrations))]).T
# Calculate least squares linear fit
slope, intercept = lstsq(conc_vstack, absorbtivities)[0]
# Plot data as points
plt.scatter(concentrations, absorbtivities)
# Plot best fit line
plt.plot(concentrations, slope * concentrations + intercept, label='fit')
# Labels
plt.title('Concentration vs Absorbtivity')
plt.xlabel('Concentration (M)')
plt.ylabel('Absorbtivity')
plt.show()
# Additional metrics
print('Dilutions (ML): ' + str(concentrations / .25 * 10))
print('Variance: ' + str(numpy.var(absorbtivities)))
print('Standard deviation: ' + str(absorbtivities.std()))
print('Slope: ' + str(slope))
print('Y-int: ' + str(intercept))
print('Mean Concentration, Absorption: (' + str(numpy.mean(concentrations)) + ', ' + str(numpy.mean(absorbtivities)) + ')')
print('Unknown substitution: ' + str(slope * .142 + intercept))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment