Использование Python для расчета pKa по кривой титрования дипротонной кислотыPython

Программы на Python
Anonymous
Использование Python для расчета pKa по кривой титрования дипротонной кислоты

Сообщение Anonymous »

Проблема
Я хотел бы рассчитать pKa кислоты на основе данных титрования. Чтобы сделать это, я хотел бы использовать нелинейную регрессию для соответствия кривой титрования, чтобы я мог использовать уравнение кривой для расчета pKa. Мне трудно понять, какое уравнение соответствует этой кривой.
Попытка решения

Код: Выделить всё

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import scipy
import numpy as np

# The Data

x_list = [0.0,
20.0,
40.0,
60.0,
80.0,
100.0,
120.0,
140.0,
160.0,
180.0,
200.0,
220.0,
230.0,
240.0,
250.0,
260.0,
270.0,
275.0,
280.0,
285.0,
290.0,
295.0,
300.0,
305.0,
310.0,
315.0,
320.0,
325.0,
330.0,
332.5,
335.0,
337.5,
340.0,
342.5,
347.5,
352.5,
357.5,
362.5,
367.5,
377.5,
387.5,
397.5,
407.5,
417.5,
427.5,
437.5,
447.5,
457.5,
467.5,
477.5,
487.5,
497.5,
507.5,
517.5,
527.5,
537.5,
547.5,
557.5,
567.5,
577.5,
587.5,
597.5,
607.5,
617.5,
627.5,
637.5,
647.5,
657.5,
667.5,
677.5,
687.5,
697.5,
707.5,
717.5,
727.5,
737.5,
747.5,
757.5,
767.5,
787.5,
807.5,
827.5,
847.5,
867.5]

y_list = [2.22,
2.25,
2.28,
2.31,
2.34,
2.38,
2.42,
2.46,
2.51,
2.56,
2.62,
2.69,
2.73,
2.77,
2.83,
2.88,
2.94,
3.01,
3.05,
3.09,
3.14,
3.19,
3.26,
3.33,
3.41,
3.52,
3.66,
3.87,
4.54,
5.8,
7.23,
7.73,
7.94,
8.12,
8.43,
8.63,
8.76,
8.87,
8.96,
9.12,
9.24,
9.36,
9.46,
9.54,
9.63,
9.71,
9.78,
9.86,
9.93,
10.01,
10.08,
10.15,
10.23,
10.32,
10.4,
10.49,
10.59,
10.69,
10.81,
10.93,
11.05,
11.18,
11.3,
11.41,
11.51,
11.61,
11.69,
11.76,
11.83,
11.89,
11.94,
11.99,
12.04,
12.08,
12.12,
12.16,
12.18,
12.21,
12.24,
12.3,
12.36,
12.41,
12.45,
12.49]

fig, axes = plt.subplots(1,2, sharex=False, figsize=(15,4))

# smooth titration curve using Savitzky-Golay filter
x = np.array(x_list)
y = np.array(y_list)
y_smooth = scipy.signal.savgol_filter(y, window_length=3, polyorder=2, mode="nearest", deriv=0)

# Calculate first derivative curve
first_derivative_gradient = np.gradient(y_smooth, x)

# Plot titration curve and first derivative curve
titration = sns.lineplot(ax=axes[0], x=x, y=y_smooth, marker='.', markerfacecolor='black', markersize=10, color='black').set(title='Titration Curve', xlabel=r"Volume NaOH ($\mu$L)", ylabel='pH')
first_derivative = sns.lineplot(ax=axes[1], x=x, y=first_derivative_gradient, marker='.', markerfacecolor='black', markersize=10, color='black').set(title='1st Derivative', xlabel="Volume NaOH ($\mu$L)", ylabel="$\Delta$pH/$\Delta$V")

# display the plot

plt.show()

# Attempt to use Logistic Regression for curve fitting

def my_function_1(x, A, B, C, D, E):
# this gives the best output
return A/(1 + B**(x - C)) + D

def my_function_2(x, A, B, C, D, E):
return A/(1 + B**(x - C)) + D + E*x

def my_function_3(x, A, B, C, D, E):
return C/(1 + (A*E)**(-B*x))

def run_regression(my_function, x, y_smooth):

parameters, covariance = scipy.optimize.curve_fit(f = my_function, xdata = x, ydata = y_smooth)

for parameter, name in zip(parameters, ['A', 'B', 'C', 'D', 'E']):
print(f'{name} = {parameter:14.10f}')

x_fitted = np.linspace(x[0], x[-1], 1000)
y_fitted = my_function(x_fitted, *parameters)

return x_fitted, y_fitted

print('First Equation')
x_fitted_1, y_fitted_1 = run_regression(my_function_1, x, y_smooth)
print('\nSecond Equation')
x_fitted_2, y_fitted_2 = run_regression(my_function_2, x, y_smooth)
print('\nThird Equation')
x_fitted_3, y_fitted_3 = run_regression(my_function_3, x, y_smooth)

titration = sns.lineplot(x=x, y=y_smooth, marker='.', markerfacecolor='black', markersize=10, color='black').set(xlabel=r"Volume NaOH ($\mu$L)", ylabel='pH')
fitted_titration_curve_1 = sns.lineplot(x=x_fitted_1, y=y_fitted_1, marker='.', markerfacecolor='red', markersize=10, color='red')
fitted_titration_curve_2 = sns.lineplot(x=x_fitted_2, y=y_fitted_2, marker='.',  markerfacecolor='blue', markersize=10, color='blue')
fitted_titration_curve_3 = sns.lineplot(x=x_fitted_3, y=y_fitted_3, marker='.', markerfacecolor='green', markersize=10, color='green')

plt.show()
Вывод сценария: кривая титрования и первая производная
Изображение

Вывод сценария — кривая титрования, адаптированная к логистической регрессии< /strong>
Изображение

Изображение

Я попробовал три формулы логистической регрессии, ни одна из которых не работает. Второе уравнение, которое я попробовал, взято из статьи «Как создать функцию точек титрования в Python?». Я также рассмотрел вопрос «Невозможно подогнать кривые к точкам данных с помощью Curve_fit() из scipy из-за ошибки «Оптимальные параметры не найдены» [python], но моя кривая имеет ту же форму, что и в примере».

Подробнее здесь: https://stackoverflow.com/questions/789 ... rotic-acid

Вернуться в «Python»