數學建模習題7.3

VVV1發表於2024-11-17

`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")`

相關文章