I am trying to calculate the area of a graph using integrals.
The user is supposed to give me 3 numbers:
- x1, x2 – the bounds of the integral
- N is in how many pieces the program will divide the function
However, I keep getting wrong results.
The first difficulty that I faced is that range accepts only integers.
Also z=(x2-x1)/N
if I try and make it a float, I can't make it a step after, and I don't make it float it approaches to zero so Python shows me an error that the step is zero.
Also how can I summarize (z*(f(i)+f(i+z)/2)
?
Here is my code:
# -*- coding: UTF-8 -*-
import math
def f(x) :
y = (-1/6.0)*(x-1)*(x-2)*(x+2)*(x-4)
return y
x1=int(raw_input ('Δωστε το χ1 οπου αρχιζει η μετρηση του ολοκληρωματος \n ')) #greek letters
x2=int(raw_input ('Δωστε χ2 οπου θελετε να ολοκληρωνεται η μετρηση \n '))
N=int(raw_input('Δωστε τον αριθμο n που θα ειναι το πληθος \n των τραπεζιων που θα χρησιμοπιουνται στη προσσεγγιση \n '))
z=(x2-x1)/N
for i in range(x1,x2,z):
z=float(z)
x1=float(x1)
x2=float(x2)
print (z*(f(i)+f(i+z))/2)