I'm relatively new to Python, so my code is very simple.
I have a project to write code which approximates integrals using the rectangle rule, followed by the trapezium rule:
a = float(input('Lower limit ---> '))
while True:
b = float(input('Upper limit ---> '))
if b > a:
break
elif a == b:
print('Integral = 0.')
else:
print('Invalid input.')
N = float(input('Number of integral divisions ---> '))
h = float((b - a) / N)
print('For the integral in the range {} to {} with {} divisions, the step size is {}.'.format(a,b,N,h))
def f(x):
return(np.exp(-x) * sin(x))
summation = (f(a) + f(b))
for points in range(a, N - 1):
summation = summation + f(a + (points * h))
I = h * summation
print(I)
Near the end, I try to use a for loop from the initial limit to minus 1 the number of step sizes.
I've defined this as a float but it I keep getting the error
TypeError: 'float' object cannot be interpreted as an integer.
Any ideas how to fix this?