我写了一个简单的程序,它近似于使用数值积分对定积分的评估。但是,当我遇到标题中的错误时,我很难过。请记住,我在一年半的时间里没有碰过蟒蛇,所以我可能会感到非常明显,但是如果你能帮助我,我仍然感激不尽:)是代码:
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)
以下是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'
答案 0 :(得分:24)
尝试加注时,请使用操作数**
而非^
。
f=math.sqrt(1+(6*x+4)**2)