This commit is contained in:
2025-04-17 18:42:23 +08:00
parent 18f97ed631
commit a7fb18055c

View File

@@ -4,7 +4,7 @@ x1, x2 = 3, 6
def fx(x):
return x/(4+x**2)
# n等分x1到x2的区间type=1表示梯形法type=2表示辛普森法
# n等分参数x1到x2的区间type=1表示梯形法type=2表示辛普森法
def CompositeNewtonCotes(n, type):
if type == 1:
h = (x2 - x1) / n
@@ -23,5 +23,9 @@ def CompositeNewtonCotes(n, type):
if __name__ == "__main__":
print("Composite Trapezoidal Rule: ", CompositeNewtonCotes(8, 1))
print("Composite Simpson's Rule: ", CompositeNewtonCotes(4, 2))
# 复合梯形公式点数为n+1
print("复合梯形公式\n", CompositeNewtonCotes(8, 1))
# 复合辛普森公式点数为2n+1
print("复合辛普森公式\n", CompositeNewtonCotes(4, 2))