In this script we will analyse the spectral data from your measurements, for the concentration experiments we will apply the Beer-Lambert Law to calculate the extinction co-efficient of Methylene Blue. Having calculated the extinction co-efficient you can determine the concentration of the unknown solution.
The first thing we need to do is import the libraries that we will need to use, for plotting we will use matplotlib and for analysis we will use numpy and pylab.
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
In this example we will calculate the absorbance from the voltage data, if you have collected data from the pi you will not need to do this and should skip this section. Change the filename to your data file that should be tab or comma separated. The skiprows command is to tell the script to ignore the header rows. If you file does not contain header rows then you can ignore that.
data = np.loadtxt(inputfile, skiprows=2)
V = []
C = []
for i in range(0,len(data)):
C.append(data[i,0])
C.append(data[i,1])
The for loop loads the data now we need to calculate the absorbance using the Beer-Lambert law. You need to enter your VS and VD values, where VS is V solvent and VD is V dark.
A = []
VS = 1
VD = 0
for i in range(0,len(data)):
Ab = np.log10((VS-VD)/(V[i]-VD))
A.append[Ab]
If you are using data from the Raspberry Pi then you just need to run the commands below, if you have data from the multimeter skip this step. You just need to put your file name as the inputFile.
data = np.loadtxt(inputFile, skiprows=2)
C = []
A = []
for i in range(0,len(data)):
C.append(data[i,0])
A.append(data[i,1])
The first thing we should do is plot the data and have a look at it to see where the behaviour becomes non-linear.
plt.plot(C,A, '-bo')
plt.ylabel('Absorbance')
plt.xlabel('Concentration (M)')
Now we need to trim the data-set a little bit to remove a plateau, have a look at the plot and see where the behaviour becomes non-linear. Set the C_cutoff to the value of concentration where the behvaiour becomes non-linear.
C_cutoff=
CF = []
AF = []
for i in range(0,len(data)):
if C[i] >= C_cutoff:
CF.append(C[i])
AF.append(A[i])
We now have final datasets CF and AF that only contain the data in the region where the behaviour is linear.
We can now fit the data to the Beer-Lambert law to obtain the extinction co-efficient to do this we will use the polyfit function of order 1. This fits a polynomial of order 1 to the data.
fit = polyfit(CF,AF,1)
outfit = np.poly1d(fit)
print "The fit to the data is:"
print "y=%s" %(outfit)
fit_fn = poly1d(fit)
We have now got the fit as a function and the value for the extinction co-efficient the final thing to do is plot the data along with the fit.
plt.plot(C, A, 'bo', C, fit_fn(C), '--k')
plt.ylabel('Absorbance')
plt.xlabel('Concentration (M)')
plt.show()