import numpy as np
def myfun(x):
if x == 0:
return 1
else:
return np.sin(x)/x
def tixing(fun, a, b, h):
n = int((b-a)/h)+1
x = np.linspace(a, b, n)
sum1 = 0
for i in range(1, n-1):
sum1 = sum1 + h*fun(x[i])
sum1 = sum1 + h/2*(fun(x[0])+fun(x[-1]))
return sum1
def simpson(fun, a, b, h):
n = int((b-a)/h)+1
x = np.linspace(a, b, n)
sum1 = 0
for i in range(1, n-1):
sum1 = sum1 + (h/6)*fun(x[i])*2+4*(h/6)*fun((x[i-1]+x[i])/2)
sum1 = sum1 + h/6*(fun(x[0])+fun(x[-1]))+(2/3)*h*fun((x[i-1]+x[i])/2)
return sum1
s1 = tixing(myfun, 0, 1, 0.125)
print('复化梯形求积公式积分结果:')
print(s1)
print('复化辛普森求积公式积分结果:')
s2 = simpson(myfun, 0, 1, 0.125)
print(s2)
复化求积公式计算积分
最新推荐文章于 2023-10-27 19:12:03 发布