4

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?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
DorianOlympia
  • 841
  • 1
  • 10
  • 22

2 Answers2

6

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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
DorianOlympia
  • 841
  • 1
  • 10
  • 22
0

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)
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • Welcome to Stack Overflow! Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should vote it up instead of repeating the same information. – Toby Speight Oct 01 '19 at 16:07