我编写了一个简单的程序,它使用数值积分来近似计算定积分。然而,当谈到为什么我在标题中收到错误时,我感到很困惑。请记住,我已经一年半没有接触过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)
这是 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'
Python 的求幂运算符是**
, not ^
. ^
是按位异或。
f=math.sqrt(1+(6*x+4)**2)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)