You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中从滴定曲线获取已知y值对应的x值

找到滴定曲线pH=7对应的滴定剂体积

你已经有了滴定剂体积数组titrant_vols和对应的pH数组phs,不需要从图像里提取数据,直接用数值插值就能算出pH=7时的x值。这里提供两种简单方法:

方法1:用SciPy插值函数(推荐)

先安装scipy(未安装的话执行pip install scipy),通过插值函数直接根据pH值反查滴定剂体积:

from pHcalc.pHcalc import Acid, Neutral, System
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d  # 导入插值模块

# 原参数定义与计算部分不变
start_vol = 0.010 # liter
conc_acid=0.5 # mol/liter, starting concentration
Ka_acid=[7.11E-003, 6.34E-008, 4.22E-013]
charge_acid=0 

conc_base=1.0 # mol/liter, starting concentration
max_vol_titrant=0.007 # Liter
charge_base=1 # 1 for NaOH

phs = []
titrant_vols = np.linspace(0,max_vol_titrant,200)
for tvol in titrant_vols:
    acid = Acid(Ka=Ka_acid, charge=charge_acid, conc=conc_acid*start_vol/(start_vol+tvol))
    base = Neutral(charge=charge_base, conc=conc_base*tvol/(start_vol+tvol))
    system = System(acid, base)
    system.pHsolve(guess_est=True)
    phs.append(system.pH)

# 转换为numpy数组并统一单位为ml
phs = np.array(phs)
titrant_vols_ml = titrant_vols * 1000

# 创建pH到滴定剂体积的插值函数
vol_from_ph = interp1d(phs, titrant_vols_ml, kind='linear')
# 计算pH=7对应的体积
target_vol = vol_from_ph(7)
print(f"pH=7时对应的滴定剂体积为: {target_vol:.3f} ml")

# 绘图时标注目标点
plt.plot(titrant_vols_ml, phs)
plt.scatter(target_vol, 7, color='red', label='pH=7')
plt.ylabel('pH')
plt.xlabel('ml titrant')
plt.legend()
plt.show()

方法2:手动线性插值(无需额外库)

如果不想依赖SciPy,可以手动定位pH跨越7的相邻点,用线性插值公式计算:

# 原参数定义与计算部分不变,得到phs和titrant_vols后执行以下代码
phs = np.array(phs)
titrant_vols_ml = titrant_vols * 1000

# 找到pH首次大于等于7的索引,取其前一个点组成插值区间
idx = np.where(phs >= 7)[0][0]
x1, y1 = titrant_vols_ml[idx-1], phs[idx-1]
x2, y2 = titrant_vols_ml[idx], phs[idx]

# 线性插值计算目标体积
target_vol = x1 + (7 - y1) * (x2 - x1)/(y2 - y1)
print(f"pH=7时对应的滴定剂体积为: {target_vol:.3f} ml")

# 绘图标注目标点
plt.plot(titrant_vols_ml, phs)
plt.scatter(target_vol, 7, color='red', label='pH=7')
plt.ylabel('pH')
plt.xlabel('ml titrant')
plt.legend()
plt.show()

注意事项

  • 确保pH数组中存在跨越7的点(即同时有小于7和大于7的值),如果曲线整体pH未到达7,需调整max_vol_titrant扩大滴定剂体积范围。
  • 线性插值是近似估算,若需要更高精度,可增加np.linspace的点数(比如把200改为1000),让离散采样更密集。

内容的提问来源于stack exchange,提问作者Annemarie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 19:05:23