Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

I wrote a simple program which approximates the evaluation of a definite integral using numerical integration. However, I am stumped when it comes to why I am getting the error in the title. Keep in mind that I haven't touched python in one and a half years so it might be something incredibly obvious that I'm missing, however I'd still be grateful if you could help me :) Here is the code:

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)

Here is the full error report IDLE gives me when trying to run the code:

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'
like image 345
Aris Pap Avatar asked Dec 14 '15 01:12

Aris Pap


1 Answers

Python's exponentiation operator is **, not ^. ^ is bitwise XOR.

f=math.sqrt(1+(6*x+4)**2)
like image 55
ilyas patanam Avatar answered Oct 31 '22 21:10

ilyas patanam