|
发表于 2024-6-5 19:57:23
|
显示全部楼层
回帖奖励 +2 鱼币
- from mpmath import *
- import math
- # 设置高精度
- mp.dps = 53
- # 定义函数f
- def f(x):
- return 3 * x ** 2 + 181 * x - 312
- # 定义函数g
- def g(x):
- return exp(2 * x) + log10(x ** 2 + 5 * x) + 32 * x - 201
- # 定义函数product_fg
- def product_fg(x):
- return f(x) * g(x)
- # 获取用户输入并检查范围
- a = mp.mpf(input("Please enter the lower limit of integration (0.0001 < a < 10000): "))
- b = mp.mpf(input("Please enter the upper limit of integration (0.0001 < b < 10000): "))
- if not (0.0001 < a < 10000 and 0.0001 < b < 10000):
- print("Error: The limits must be in the range (0.0001, 10000).")
- else:
- # 使用mpmath的quad函数进行积分
- result = quad(product_fg, [a, b], method='tanh-sinh', epsabs=1e-15, epsrel=1e-15)
-
- # 打印结果
- print(f"The result of integration is: {nstr(result, 10)}")
复制代码
主要修正点:
修正quad函数的返回值:quad函数返回的是单个值,所以不需要解包为integral, error。
增加输入范围检查:确保输入的上下限在规定范围内。
使用nstr函数打印结果:nstr函数可以格式化高精度数字以便于阅读。 |
|