I’m trying to evaluate the following function in python:
f(x) = (1 + cos(x))^(1/3)
def eval( i ):
return math.pow( (1 + math.cos( i )), 1/3)
why is it always returning me 1?
I’m trying to calculate the Right and Left approximation of an integral, and latter apply Simpson's Rule, but Python does not seem to like that expression.
Help?
*Complete Code *
import math
min = 0
max = math.pi / 2
n = 4
delta = ( min + max ) / n
def eval( i ):
return math.pow( (1 + math.cos( i )), 1/3)
def right( ):
R = 0
for i in range(1, n+1):
R += eval( i )
return R
def left():
L = 0
for i in range(0, n):
print eval( i )
L += eval( i )
Use floating point math (1 / 3 truncates to zero). Also, no need for math.pow (** for exponentiation)…
Also,
min,maxandevalare built-in functions – you are shadowing them.Also, the extra spaces you are adding in your function call arguments are against PEP-8 (Python Style Guide). Specifically this paragraph:
http://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements