Python -the integral from function multiplication

老子叫甜甜 提交于 2021-01-01 06:26:07

问题


In python, I have two functions f1(x) and f2(x) returning a number. I would like to calculate a definite integral after their multiplication, i.e., something like:

scipy.integrate.quad(f1*f2, 0, 1)

What is the best way to do it? Is it even possible in python?


回答1:


I found out just a second ago, that I can use lambda :)

scipy.integrate.quad(lambda x: f1(x)*f2(x), 0, 1)

Anyway, I'm leaving it here. Maybe it will help somebody out.




回答2:


When I had the same problem, I used this (based on the suggestion above)

from scipy.integrate import quad

def f1(x):
    return x

def f2(x):
    return x**2

ans, err = quad(lambda x: f1(x)*f2(x), 0, 1)
print("the result is", ans)


来源:https://stackoverflow.com/questions/34437346/python-the-integral-from-function-multiplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!