2

Hello I would like what is the x value that would give me an specific numeric integral.

So far I am using scipy.integrate and works ok, but given a integral value is there a way to know what is the x that gave that result?

let say I have a function f(x) = |2-x| 1<=x<=3

I would like to know what is the x value that give me 0.25 (the first quartile).

What in scipy.stats for normal distribution is norm.ppf(), in this particular case I am using it for PDF (probability density function) but it can be any integral.

Thanks and regards

sep9407
  • 23
  • 1
  • 6
  • Besides integral value what else is known? We need something to know about function itself to revert integral value. Is function given somehow? Symbolically or numerically? – Arty Sep 20 '20 at 04:39
  • Hello @Arty, as I mention in the question I have the function, and lets say the initial value for example for the function given, lets say from 1 to ? and I have the result 0.25 – sep9407 Sep 20 '20 at 04:54
  • And how your function is given? I mean is it given as a regular python function (`def` or `lambda`). If so you can use binary search to revert integral. – Arty Sep 20 '20 at 05:06
  • I am a begginer to python, I have the function f(x) defined between 1 and 3, I can define it as convinient in order to find the x value that makes the integral between 1 and x to give the result 0.25... does it make sense? – sep9407 Sep 20 '20 at 05:12
  • I posted answer with binary search. – Arty Sep 20 '20 at 05:25

1 Answers1

1

I use binary search to find answer in logarithmic time (very fast). You can also run code below online here.

import math, scipy.integrate, scipy.optimize

def f(x):
    return math.sin(x)

a, b = 0, 10
integral_value = 0.5

res_x = scipy.optimize.bisect(
    lambda x: scipy.integrate.quad(f, a, x)[0] - integral_value,
    a, b
)
print(
    'found point x', res_x, ', integral value at this point',
    scipy.integrate.quad(f, a, res_x)[0]
)
Arty
  • 14,883
  • 6
  • 36
  • 69