我在情节上有些问题。这里的Exatlct解是正确的,但是eulerbmethod给出了同样的曲线,但要低得多。
import numpy as np
import matplotlib.pyplot as plt
# Define parameters
f = lambda x, y: 2*x
h = 0.1
x = np.arange(-10, 10, h)
x0 = 0
y0 = 2
# Explicit Euler Method
y = np.zeros(len(x))
y[x0] = y0
for i in range(0, len(x) - 1):
y[i + 1] = y[i] + h*f(x[i], y[i])
plt.figure(figsize=(12, 8))
plt.plot(x, y, 'b--', label='Euler')
plt.plot(x, 2+x**2, 'g', label='Exact')
plt.title('Numerical integration methods')
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.legend()
plt.show()发布于 2022-09-01 15:20:29
那是因为你的“精确解决方案”不正确。集成时,必须考虑x的非零值:

https://stackoverflow.com/questions/73570944
复制相似问题