I'm writing a program to calculate left-hand, right-hand, and mid-point Riemann Sums (I misspelled it everywhere else). I decided to use python (I have some experience with it) to write the script and eventually build up a library of math tools that I could quickly execute using bash aliases and scripts. However, I'm running into problems with my script. Because I need to evaluate a function, I used this library. After writing the script, I tried to execute it using the following command:
python -c "from reimann import reimann; print(reimann(1.0,2.718281828459045,10,'1/x','midpoint'))"
This should return about 1, and it does when run from the script, but I get the following error when run as shown above:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "reimann.py", line 3, in reimann
delta_x = ((float(interval_b) - float(interval_a))/float(num_intervals * 1.0))
TypeError: can't multiply sequence by non-int of type 'float'
even when run with the following command:
python -c "from reimann import reimann; print(reimann(float(1.0),float(2.718281828459045),10.0,'1/x','midpoint'))"
Any help is greatly appreciated! Below is my code:
from simpleeval import simple_eval
def reimann (equation, interval_a, interval_b, num_intervals, type_sum):
delta_x = ((float(interval_b) - float(interval_a))/float(num_intervals * 1.0))
ans = 0;
if type_sum == "left":
x = interval_a
while x < (interval_b - delta_x):
ans += simple_eval(equation, names={"x": x})
x += delta_x
else:
if type_sum == "right":
x = interval_a + delta_x
while x < interval_b:
ans += simple_eval(equation, names={"x": x})
x += delta_x
else:
if type_sum == "midpoint":
x = interval_a
while x < (interval_b - delta_x):
ans += simple_eval(equation, names={"x": x})
x += delta_x
x = interval_a + delta_x
while x < interval_b:
ans += simple_eval(equation, names={"x": x})
x += delta_x
ans /= 2
ans *= delta_x
return ans
#print(reimann("1/x", 1, 2.718281828459045, 10000, "midpoint") This works correctly
NOTE: I put the parameters in wrong, I'm sorry for wasting everyones' time. Thanks for the help.