- SciPy 教程
- SciPy - 主页
- SciPy - 简介
- SciPy - 环境设置
- SciPy - 基本功能
- SciPy - 集群
- SciPy - 常量
- SciPy - FFTpack
- SciPy - 整合
- SciPy - 插值
- SciPy - 输入和输出
- SciPy - 线性代数
- SciPy - N 维图像
- SciPy - 优化
- SciPy - 统计
- SciPy - CS 图
- SciPy - 空间
- SciPy - ODR
- SciPy - 特殊包
- SciPy 实用资源
- SciPy - 参考
- SciPy - 快速指南
- SciPy - 实用资源
- SciPy - 讨论
SciPy - integrate.newton_cotes() 方法
SciPy integrate.newton-cotes() 方法用于返回牛顿 - 科茨积分的权重和误差系数。让我们详细了解权重和误差系数的术语 -
- 权重:此系数适用于函数以表示指定点。因此,此系数操作积分。
- 误差系数:使用以下公式计算权重,该公式准确地对一定程度的多项式进行积分。
语法
以下是 SciPy integrate.newton-cotes() 方法的语法 -
newton_cotes(int_val)
参数
此方法仅接受一个参数,以整数形式确定阶值。
返回值
此方法以两种不同的形式返回结果 - 浮点数和列表。
示例 1
以下是一个基本示例,展示了 SciPy integrate.newton-cotes() 方法的用法。
import numpy as np
from scipy import integrate
def exp_fun(x):
return np.exp(-x)
res = integrate.romberg(exp_fun, 0, 1)
print("The result of integrating exp(-x) from 0 to 1:", res)
# Order of Newton-Cotes rule
rn = 4
# Compute weights and error coefficient
weights, error_coeff = integrate.newton_cotes(rn)
print("The weights is ", weights)
print("The error coefficient is ", error_coeff)
输出
以上代码产生以下输出 -
The result of integrating exp(-x) from 0 to 1: 0.63212055882857 The weights is [0.31111111 1.42222222 0.53333333 1.42222222 0.31111111] The error coefficient is -0.008465608465608466
示例 2
下面的程序执行使用权重的数值积分任务。
import numpy as np
from scipy.integrate import newton_cotes
# define the function to integrate
def fun(x):
return np.sin(x)
# define the interval [a, b]
a = 0
b = np.pi
# Order of Newton-Cotes rule
rn = 3
# calculate weights and error coefficient
weights, _ = newton_cotes(rn)
# calculate the points at which the function is evaluated
x = np.linspace(a, b, rn+1)
# calculate the integral approximation
integral_approx = (b - a) * np.dot(weights, fun(x)) / rn
print("The result of approximate integral:", integral_approx)
输出
以上代码产生以下输出 -
The result of approximate integral: 2.040524284763495
scipy_reference.htm
广告