Skip to content

Instantly share code, notes, and snippets.

@Ionizing
Created November 22, 2017 08:57
Show Gist options
  • Save Ionizing/233deb187f8d735f451037c16d050078 to your computer and use it in GitHub Desktop.
Save Ionizing/233deb187f8d735f451037c16d050078 to your computer and use it in GitHub Desktop.
醋酸解离常数的数据处理
import numpy as np
import xlrd
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
path = 'Workbook1.xlsx'
workbook = xlrd.open_workbook(path)
sheet = workbook.sheet_by_index(0)
V_NaOH = []
pH = []
for row in range(1,68):
V_NaOH.append(sheet.row(row)[0].value)
pH.append(sheet.row(row)[1].value)
plt.plot(V_NaOH, pH)
# plt.show()
xvals = np.arange(0, V_NaOH[-1], 0.01)
f = interp1d(V_NaOH, pH, kind='cubic')
yinterp = f(xvals)
print('size of xvals', np.size(xvals))
plt.plot(xvals, yinterp)
V_wanted = []
pH_wanted = []
for i in range(1, np.size(xvals)):
dx = xvals[i] - xvals[i-1]
dy = yinterp[i] - yinterp[i-1]
diff = dy/dx
if diff <= 1.1 and diff >= 0.9 and i in range(500, 900):
V_wanted.append(xvals[i])
pH_wanted.append(yinterp[i])
print('V_wanted', V_wanted)
print('pH_wanted', pH_wanted)
# plt.plot(V_wanted, pH_wanted, 'o')
intersections = []
for i in range(np.size(V_wanted)):
if i == 0 or i == np.size(V_wanted)-1:
x = V_wanted[i]
y = pH_wanted[i]
intersection = y - x
intersections.append(intersection)
xp = np.arange(x-2, x+2, 0.01)
yp = xp + intersection
plt.plot(xp, yp, '--')
plt.plot(x, y, 'o')
xp = np.arange(np.average(V_wanted)-2, np.average(V_wanted)+2, 0.01)
yp = xp + np.average(intersections)
plt.plot(xp, yp, '--')
# Solve intersection
mid = (np.average(xp)*100).astype(int)
print(mid)
yi = yinterp[mid-200:mid+200]
print(np.size(yi), np.size(yp))
# plt.plot(xp, yi, 'b--')
for i in range(np.size(yi)):
if np.abs(yi[i] - yp[i]) <= 0.1:
V_res = xp[i]
pH_res = yp[i]
print('@64 ', V_res, pH_res)
plt.plot(V_res, pH_res, 'o')
plt.plot(V_res*np.ones(100), np.linspace(1, pH_res, 100), '--')
print(i)
break
pKa = yinterp[(V_res/2*100).astype(int)]
print('The end point requires sodium hydroxide at volume of ', V_res/2, ' mL')
print('pKa = ', pKa, ', which means Ka = ', np.power(10, -pKa))
plt.plot(V_res/2*np.ones(100), np.linspace(0, pKa, 100), '--')
plt.plot(np.linspace(0, V_res/2, 100), pKa*np.ones(100), '--')
plt.grid(True)
# plt.rc('grid', linestyle='--', color='black')
# plt.show()
plt.ylabel('pH')
plt.xlabel('$V_{NaOH}$')
plt.savefig('fig.pdf', format='pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment