如何在没有scipy的情况下在python中使用simpsons规则进行集成

如何在没有scipy的情况下在python中使用simpsons规则进行集成,python,python-3.x,Python,Python 3.x,我需要使用python中的辛普森规则将函数g(t)=2e^(-t^2)从0集成到3,而不使用scipy中的内置辛普森规则。感谢您提供的任何帮助以下是您如何在不使用scipy的情况下实现辛普森规则。我已经给出了代码的框架,您所要做的就是正确地填写它 我想你知道辛普森定律是什么。我们要做的第一件事是为被积函数构造一个python函数 import numpy as np g = lambda t: 接下来,让我们设置一组均匀分布的点,我们可以在这些点上执行求积 #Sample the funct

我需要使用python中的辛普森规则将函数g(t)=2e^(-t^2)从0集成到3,而不使用scipy中的内置辛普森规则。感谢您提供的任何帮助

以下是您如何在不使用scipy的情况下实现辛普森规则。我已经给出了代码的框架,您所要做的就是正确地填写它

我想你知道辛普森定律是什么。我们要做的第一件事是为被积函数构造一个python函数

import numpy as np

g = lambda t:
接下来,让我们设置一组均匀分布的点,我们可以在这些点上执行求积

#Sample the function
h = 0.01
a = 0
b = 3
x = np.arange(a,b+h,h)
接下来,我们将为辛普森规则的系数构造一个数组

#Coefficients for simpons rule
coefs = np.zeros(x.size)

coefs[0] = #What is the first coefficient or simpson's rule

coefs[1::2] =  #What should the coefficient of the odd xs be
coefs[2::2] =  #What should the coefficient of the even xs be

coefs[-1] = #What is the last coefficient of simpson's rule
辛普森法则只是一个和,可以用内积很快地计算出来

#Perform the integration
area = h/3* #Can you express simpson's rule as a dot product?  Look up np.dot or the @ operator

print(area)


#Absolute Error.  Should be O(10^-16) meaning it is very close to scipy's area
from scipy.integrate import simps
print(simps(g(x),x) - area)

我们已经知道您需要什么,现在我们需要知道您的问题是什么,请改进您的问题,并为此阅读《欢迎使用stackoverflow》指南!这将有助于您发布您尝试过的内容。我们可以帮助您解决问题,但无法为您编写代码。