我写了一个简单的程序,它近似于使用数值积分对定积分的评估.但是,当我遇到标题中的错误时,我很难过.请记住,我在一年半的时间内没有触及过python,所以我可能会非常明显地忘记这一点,但是如果你能帮助我,我仍然会感激不尽:)以下是代码:
import math
def f(x):
f=math.sqrt(1+(6*x+4)^2)
return f
lbound=int(input("Input lower bound for the integral"))
ubound=int(input("Input upper bound for the integral"))
n=int(input("Input number of intervals"))
dx=((ubound-lbound)/n)
integral=0
for i in range(1,n):
integral=integral+dx*f(i*dx)
print (integral)
Run Code Online (Sandbox Code Playgroud)
以下是IDLE在尝试运行代码时给出的完整错误报告:
Traceback (most recent call last):
File "C:\Users\******\Desktop\integrals.py", line 13, in <module>
integral=integral+dx*f(n*dx)
File "C:\Users\******\Desktop\integrals.py", line 3, in f
f=math.sqrt(1+(6*x+4)^2)
TypeError: unsupported operand type(s) for ^: 'float' and 'int'
Run Code Online (Sandbox Code Playgroud)
ily*_*nam 29
当试图提高到电源使用操作数**不是^.
f=math.sqrt(1+(6*x+4)**2)
Run Code Online (Sandbox Code Playgroud)