【数值分析】python实现复化辛普森积分
作者:互联网
- python代码
import numpy as np
a, b = input("积分区间:").split(' ')
n = int(input("子区间个数:"))
a = int(a)
b = int(b)
x_i = np.linspace(a, b, n+1) # 区间结点
h = (b-a)/n
# 原函数
def f(x):
return 4/(1+x**2)
def piece(x_i, i, h):
return h/6 * (f(x_i[i-1]) + 4*f((x_i[i-1] + x_i[i])/2) + f(x_i[i]))
S = 0
for i in range(1, n+1):
S = S + piece(x_i, i, h)
print("复化辛普森积分公式计算结果:" + str(S))
- 输入格式及测试结果
标签:return,python,积分,辛普森,int,复化,np,input 来源: https://blog.csdn.net/qq_51283283/article/details/115419711