習題7.1

等我刷把宗师發表於2024-11-06
import numpy as np
from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata
import matplotlib.pyplot as plt
from scipy.integrate import quad
x0 = np.linspace(0, 10, 1000)
g = lambda x: (3*x**2+4*x+6)*np.sin(x) / (x**2+8*x+6)
y0 = g(x0)
ghat = interp1d(x0, y0)
yhat = ghat(x0)
I = quad(g, 0, 10)[0]
Ihat = np.trapz(yhat, x0)
print(f'{I    = }\n{Ihat = }')

fig = plt.figure(dpi=150)
ax = fig.add_subplot(111)
ax.scatter(x0[::50], y0[::50], s=15, marker='x', zorder=10)
ax.plot(x0, yhat, linewidth=2, color='r')
fig.show()


相關文章