如何正确使用cumtrapz?
计算科学
Python
scipy
一体化
绘图
2021-12-01 15:03:44
1个回答
除非您添加一个常数并使用给定的 x 坐标绘图,否则您正在用它逼近一个定积分cumtrapz不会给您相同的结果作为积分方程:
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as it
x = np.arange(-10,10, 0.01) # start,stop,step
f = x**2
f_int=it.cumtrapz(f,x, initial=0)
plt.plot(x, (1/3) * np.power(x,3))
plt.plot(x, f_int + (1/3) * np.power(-10,3), '--')
plt.legend(["(1/3) x^3", "cumtrapz(x^2) + C"])

