`import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d, CubicSpline
已知的温度和对应的体积
T_known = np.array([700, 720, 740, 760, 780])
V_known = np.array([0.0977, 0.1218, 0.1406, 0.1551, 0.1664])
需要求解的温度
T_query = np.array([750, 770])
线性插值
linear_interp = interp1d(T_known, V_known, kind='linear')
V_linear = linear_interp(T_query)
三次样条插值
cubic_spline = CubicSpline(T_known, V_known)
V_cubic = cubic_spline(T_query)
绘制插值函数
T_plot = np.linspace(700, 780, 400) # 用于绘图的温度范围
V_linear_plot = linear_interp(T_plot)
V_cubic_plot = cubic_spline(T_plot)
plt.figure(figsize=(10, 6))
plt.plot(T_known, V_known, 'o', label='Data Points')
plt.plot(T_plot, V_linear_plot, '-', label='Linear Interpolation')
plt.plot(T_plot, V_cubic_plot, '--', label='Cubic Spline Interpolation')
plt.scatter(T_query, V_linear, color='red', label='Linear Interpolated Points')
plt.scatter(T_query, V_cubic, color='green', label='Cubic Spline Interpolated Points')
plt.xlabel('Temperature (T)')
plt.ylabel('Volume (V)')
plt.title('Interpolation of Steam Volume Change with Temperature')
plt.legend()
plt.grid(True)
plt.show()
打印查询温度下的体积变化
print(f"At T=750, Linear Interpolated Volume: {V_linear[0]:.4f}, Cubic Spline Interpolated Volume: {V_cubic[0]:.4f}")
print(f"At T=770, Linear Interpolated Volume: {V_linear[1]:.4f}, Cubic Spline Interpolated Volume: {V_cubic[1]:.4f}")
print("3005")`