# Find the value of the inverse of a function in some point

**URL:** <https://discourse.julialang.org/t/find-the-value-of-the-inverse-of-a-function-in-some-point/67479>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 1, 2021, 8:15am UTC](https://discourse.julialang.org/t/find-the-value-of-the-inverse-of-a-function-in-some-point/67479 "2021-09-01T08:15:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [September 1, 2021, 8:15am UTC](https://discourse.julialang.org/t/find-the-value-of-the-inverse-of-a-function-in-some-point/67479/1 "2021-09-01T08:15:18Z")

</div>

I have a particular function and I want to calculate its inverse. My function is locally injective and I want to calculate its inverse, more specifically, values. For example, suppose my function is f such that f(0.1) = 0.2. I would like to compute f^-1(0.21), f^-1(0.19) etc.

Is there any function that does this?

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [September 1, 2021, 8:27am UTC](https://discourse.julialang.org/t/find-the-value-of-the-inverse-of-a-function-in-some-point/67479/2 "2021-09-01T08:27:37Z")

</div>

You want to minimize `abs(f(y) - x)` where `x` is fixed. You may want to look at some optimization packages, like [Optim.jl](https://github.com/JuliaNLSolvers/Optim.jl). This will be even better if you know the gradient of `f`.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 1, 2021, 8:29am UTC](https://discourse.julialang.org/t/find-the-value-of-the-inverse-of-a-function-in-some-point/67479/3 "2021-09-01T08:29:05Z")

</div>

Rather than expressing it as a minimization problem, you want to find the root (zero) of

```julia
x -> f(x)-y

```

Example

```julia
using Roots
f(x) = x^2
finv(y) = fzero(x->f(x) - y, 1) # set initial guess here
finv(2)

julia> finv(2)
1.41421

```

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [September 1, 2021, 11:36am UTC](https://discourse.julialang.org/t/find-the-value-of-the-inverse-of-a-function-in-some-point/67479/4 "2021-09-01T11:36:47Z")

</div>

There are indeed some choices, depending on obtaining and evaluating your inverse.

- If an analytic inverse exists (a symbolic calculator can help), can you evaluate it fast enough?
- If you can search the inverse value as the solution of a (possibly) nonlinear equation, you must also take care of convergence to the wanted root.
- If you want a fast inverse with reasonable accuracy, you might consider reverse interpolation:

```julia
# using Pkg
# Pkg.add("Interpolations")

using Interpolations

xs = 1:0.2:64
ys = log2.(xs) # our function with "difficult" inverse

# interp_fun = LinearInterpolation(xs, ys)
interp_inv = LinearInterpolation(ys, xs)

interp_inv(4.0) == 16

```

Depending on your storage and precision requirements, you will choose the number and location of grid points, choose other interpolation methods (quadratic, spline…), and take care of the ranges and [extrapolation](http://juliamath.github.io/Interpolations.jl/latest/).
