For hours I've been trying to input an equation as an argument to my function. I want the user to be able to write his own equation which my program then integrates.
The best I have got so far is this (it integrates but does not allow the user to choose the equation):
def integrate(x):
def f(x):
return x**2 + x * 42
n = 10000
a = 0 #lower limit
b = 1 #upper limit
small_element = (b - a) / n
result = 0
for k in range(0, n):
result += f(a + (k +0.5)* small_element)
result *= small_element
return result
As I said, the problem with the code above is that the user does not choose the equation. This program only works on this particular equation of x**2 + x * 42 which I wrote down myself. How do I allow the user to input a mathematical equation himself? For example, I would like him to be able to write in the console:
Integrate(x**3 + x**2 + x + 42)
which would then input the equation into my program and integrate it accordingly.
I was also thinking about putting the equation as a string and somehow making it into an float-kinda thing but that didn't work either.