I would very much like to implement analytical integration in my calculator. I have used numerical integration prior to this attempt with success. However, I can't seem to make my code work.
I'm using the scipy.integrate module and the error code I get is TypeError: can't convert expression to float
My code is:
from sympy.functions import sin,cos,tan
from sympy.abc import x
from sympy import series
import scipy.integrate as integrate
import scipy.special as special
function1 = str(input("The function that should be expanded: "))
x0 = float(input("Point of developement: "))
n = 1 + int(input("Grade of polynomial: "))
# input 0 -> n=1 -> konstant (1 term, konstant)
# input 1 -> n=2 -> lineƦr (2 termer, konstant + linear)
# input 2 -> n=3 -> kvadratisk (3 termer, konstant + linear + kvadratisk)
# ...
function2 = series(function1, x, x0, n)
print(function2)
a = float(input("Integrate from: "))
b = float(input("Integrate to: "))
def f(x):
return function2.removeO()
x = Symbol('x')
result = integrate.quad(lambda x: f(x), a, b)
print("...................")
print("Here is your answer: ")
print(result)
I believe it might be due to the code using different modules (sympy and scipy) or that I just need to convert the expression in some way. I attempted to use lambdify but without success.
def f(x):
return function2.removeO()
lam_f = lambdify(x, f(x))
result = integrate.quad(lambda x: lam_f, a, b)
And got another TypeError, since lambdify turns the expression into a function instead.
Can anyone help?