import numpy as np
import scipy.interpolate as spi
import scipy.integrate as spi_integrate
def g(x):
return ((3*x**2 + 4*x + 6) * np.sin(x)) / (x**2 + 8*x + 6)
# 生成x值
x_values = np.linspace(0, 10, 1000)
# 计算对应的y值
y_values = g(x_values)
# 创建三次样条插值
spline = spi.CubicSpline(x_values, y_values)
def h(x):
return spline(x)
# 积分g(x)从0到10
integral_g, _ = spi_integrate.quad(g, 0, 10)
# 生成更细的x值
x_fine = np.linspace(0, 10, 10000)
# 计算对应的y值
y_fine = h(x_fine)
# 积分h(x)从0到10
integral_h = np.trapz(y_fine, x_fine)
# 打印结果
print(f"积分 g(x) 从 0 到 10 的结果: {integral_g}")
print(f"积分 h(x) 从 0 到 10 的结果: {integral_h}")
print("学号:3008")
结果如下图所示
标签:10,integral,spi,7.1,values,np,习题,fine From: https://www.cnblogs.com/fang---/p/18541662