首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中的集成

python中的集成
EN

Stack Overflow用户
提问于 2015-04-22 17:32:59
回答 2查看 161关注 0票数 0

我为集成而编写的代码给出了错误的结果,我把c_0、c_1、...c_4变成零!我做错了什么?我只在mac上使用0.7.6。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-22 17:51:22

除了需要将numpy作为np导入之外,我在最近的版本(0.7.6)中没有看到任何问题。有些值为零(由于对称考虑而预期为零),但另一些值则不是:

代码语言:javascript
复制
>>> print c_0
0
>>> print c_1
0.636619772367581
>>> print c_2
0
>>> print c_3
-0.330926260628403
>>> print c_4
0
票数 1
EN

Stack Overflow用户

发布于 2015-04-22 17:54:38

我同意smichr的观点。只要你的进口没问题,一切都会好起来的。你也可以使用枕,但这取决于你的喜好和需要。

代码语言:javascript
复制
from scipy.integrate import quad

quad(lambda x: x**2, 0, 1)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29804970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档