This commit is contained in:
lwj
2025-06-14 09:26:42 +08:00
parent 13abd95ada
commit 639646c5e3
2 changed files with 9 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
#把范围与原函数换成题干的############
x1, x2 = 3, 6
def fx(x):
@@ -25,8 +25,8 @@ def CompositeNewtonCotes(n, type):
if __name__ == "__main__":
# 复合梯形公式点数为n+1
print("复合梯形公式\n", CompositeNewtonCotes(8, 1)) #8等分1代表是梯形公式
print("复合梯形公式\n", CompositeNewtonCotes(8, 1)) #8等分1代表是梯形公式####################
# 复合辛普生公式点数为2n+1
print("复合辛普生公式\n", CompositeNewtonCotes(4, 2)) #4等分2代表是辛普生公式
print("复合辛普生公式\n", CompositeNewtonCotes(4, 2)) #4等分2代表是辛普生公式###############

View File

@@ -1,23 +1,28 @@
#把函数改成题干的####################
def fx(x):
return 1/x
# 逐次分半梯形递推公式
def SplitTrapezoidal(a,b,err):
count = 1
t1 = (b-a)*(fx(a)+fx(b))/2
print(f"t{count}={t1}")
k = 1
while True:
tmp = 0
for i in range(1, 2**(k-1)+1):
tmp += fx(a + (b-a)*(2*i-1)/(2**k))
t2 = t1/2+(b-a)*tmp/(2**k)
count *= 2
print(f"t{count}={t2}")
if abs(t2-t1) < err:
break
t1 = t2
k += 1
return t2,k
#把范围ab换成题干的范围err换成题干的精度要求############
if __name__ == "__main__":
a = 1
b = 3