# Approximate inverse of a definite integral

**URL:** <https://discourse.julialang.org/t/approximate-inverse-of-a-definite-integral/83181>\
**Category:** Numerics\
**Tags:** question, integral\
**Created:** [June 22, 2022, 12:21pm UTC](https://discourse.julialang.org/t/approximate-inverse-of-a-definite-integral/83181 "2022-06-22T12:21:43Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 22, 2022, 1:45pm UTC](https://discourse.julialang.org/t/approximate-inverse-of-a-definite-integral/83181/3 "2022-06-22T13:45:10Z")

</div>

I would just use Newton’s method here to compute h^{-1} from a quadrature routine for h(x). Something like:

```julia
using QuadGK
const g₁ = quadgk(f, 0, 1; rtol=1e-12)[1] # compute g(1) accurately
g(x; atol=1e-9 * g₁) = quadgk(f, 0, x; atol=atol)[1]
h(x; atol=1e-9) = g(x; atol = atol * g₁) / g₁
h′(x) = f(x) / g₁ # the derivative, using the fundamental theorem of calculus
function h⁻¹(y; atol=1e-9)
    x = 0.5 # arbitrary initial guess
    while true
        δx = (h(x; atol) - y) / h′(x)
        x -= δx
        abs(δx) ≤ atol && return x
    end
end

```

which gives, e.g. for `f(x) = x^3 + 3`:

```julia
julia> y = h(0.3)
0.2775461538461539

julia> h⁻¹(y)
0.30000000000000004

```

You can then plug `h⁻¹` into e.g. [FastChebInterp](https://github.com/stevengj/FastChebInterp.jl) or [ApproxFun](https://github.com/JuliaApproximation/ApproxFun.jl) to form an efficient Chebyshev interpolant rather than applying Newton’s method every time you want to evaluate it.

(If f is extremely expensive, you might want to first replace it by a Chebyshev interpolant too, before using the interpolant to construct h and h^{-1}. Of course, once you have the interpolant, you don’t need QuadGK, e.g. with ApproxFun using `cumsum` will form the integral function directly. You still probably want Newton’s method to invert it.)

> [@Tamas\_Papp](#):
>
> I thought of formulating the integral as an ODE

In general, you almost never want to evaluate definite integrals by solving ODEs — specialized integration routines are _much_ more efficient (and simpler to use), because they exploit the fact that you have “random access” to your integrand values at any point in the domain (rather than having to build it up from “left to right” as in a general ODE).

---

_[View the full topic](https://discourse.julialang.org/t/approximate-inverse-of-a-definite-integral/83181)._
