我为集成而编写的代码给出了错误的结果,我把c_0、c_1、...c_4变成零!我做错了什么?我只在mac上使用0.7.6。
from numpy import *
from matplotlib.pyplot import *
from sympy import *
x = Symbol('x')
f = 1.0*sin(np.pi * x)
phi_0 = 1.0
phi_1 = 1.0*x
phi_2 = 1./2*(3*x**2-1)
phi_3 = 1./2*(5*x**3-3*x)
phi_4 = 1./8*(35*x**4-30*x**2+3)
c_0 = integrate(f*phi_0, (x, -1.0, 1.0))
c_1 = integrate(f*phi_1, (x, -1.0, 1.0))
c_2 = integrate(f*phi_2, (x, -1.0, 1.0))
c_3 = integrate(f*phi_3, (x, -1.0, 1.0))
c_4 = integrate(f*phi_4, (x, -1.0, 1.0))
print c_0
print c_1
print c_2
print c_3
print c_4发布于 2015-04-22 17:51:22
除了需要将numpy作为np导入之外,我在最近的版本(0.7.6)中没有看到任何问题。有些值为零(由于对称考虑而预期为零),但另一些值则不是:
>>> print c_0
0
>>> print c_1
0.636619772367581
>>> print c_2
0
>>> print c_3
-0.330926260628403
>>> print c_4
0发布于 2015-04-22 17:54:38
我同意smichr的观点。只要你的进口没问题,一切都会好起来的。你也可以使用枕,但这取决于你的喜好和需要。
from scipy.integrate import quad
quad(lambda x: x**2, 0, 1)https://stackoverflow.com/questions/29804970
复制相似问题