DorianOlympia
DorianOlympia

Reputation: 851

Python -the integral from function multiplication

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?

Upvotes: 4

Views: 2225

Answers (2)

DorianOlympia
DorianOlympia

Reputation: 851

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.

Upvotes: 6

user12149505
user12149505

Reputation: 11

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)

Upvotes: 0

Related Questions