# How to get the inverse of a function

**URL:** https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977
**Category:** New to Julia
**Tags:** question
**Created:** [September 29, 2022, 11:02am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977 "2022-09-29T11:02:19Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![zoezoe1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zoezoe1/32/43066_2.png) [@zoezoe1](https://discourse.julialang.org/u/zoezoe1)
#### Post date: [September 29, 2022, 11:02am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/1 "2022-09-29T11:02:19Z")

</div>

Hello,

I have the following function which I have been plotting as follows:

```julia
function f(x,p)
    v0,S,D,v,K,n,µ = p
    return (((S*x)^n*(µ*x - v - v0)/(v0 - µ*x))^(1/n)-D*x-K)/D
end

plot(x -> f(x,(0.4,4.0,1.0,2.0,0.7,2,1.0)),0.0,1.0)

```

I would now like to find and plot the inverse of the function. How can I get this? I tried the InverseFunctions package that I found online but it does not seem to be working.

Thanks in advance!

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [September 29, 2022, 11:17am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/2 "2022-09-29T11:17:41Z")

</div>

You can solve an equation with for example `Roots.jl` to obtain the inverse function:

```julia
using Roots

invf(y, p) = find_zero(x -> f(x, p) - y, x0)

```

---

<div class="post-metadata">

### Author: ![zoezoe1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zoezoe1/32/43066_2.png) [@zoezoe1](https://discourse.julialang.org/u/zoezoe1)
#### Post date: [September 29, 2022, 11:22am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/3 "2022-09-29T11:22:59Z")

</div>

Thanks a lot! How do I know what to set for x0?

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [September 29, 2022, 11:30am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/4 "2022-09-29T11:30:34Z")

</div>

You need good guess for that, which is problem-dependent.

---

<div class="post-metadata">

### Author: ![zoezoe1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zoezoe1/32/43066_2.png) [@zoezoe1](https://discourse.julialang.org/u/zoezoe1)
#### Post date: [September 29, 2022, 11:33am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/5 "2022-09-29T11:33:00Z")

</div>

In addition, when I do `plot(x -> f(x,(0.4,4.0,1.0,2.0,0.7,2,1.0)),0.0,1.0)` I get the output below  
 ![Screenshot 2022-09-29 at 12.28.56](https://global.discourse-cdn.com/julialang/original/3X/7/4/742b83315b8a7225ff0106c5407664bd933982ee.png)

Now to plot the inverse I’m doing `invf(y, p) = find_zero(x -> f(x, p) - y, 1)` where I set x0 to 1 just to test whether it works.

However if I now do `plot(x -> invf(x,(0.4,4.0,1.0,2.0,0.7,2,1.0)),0.0,1.0)` , I get the same axes as above but a totally blank plot.

Any clue why? Thanks!

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [September 29, 2022, 11:36am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/6 "2022-09-29T11:36:13Z")

</div>

Does `find_zero` find a zero with that initial value? You can also try different algorithms for root-finding.

---

<div class="post-metadata">

### Author: ![zoezoe1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zoezoe1/32/43066_2.png) [@zoezoe1](https://discourse.julialang.org/u/zoezoe1)
#### Post date: [September 29, 2022, 11:46am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/7 "2022-09-29T11:46:11Z")

</div>

Hm I don’t think I know enough math (haven’t gone above high school level) to understand what root finding is nor do I actually get what `find_zero` does, which I think is why I’m a bit lost! Any chance you could explain a bit, or is there an alternative Julia method that is more compatible with my poor math skills? Apologies for the ignorance!

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [September 29, 2022, 11:49am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/8 "2022-09-29T11:49:50Z")

</div>

If it is monotonic, bisection over the whole interval will work. So no guess is needed here.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [September 29, 2022, 11:52am UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/9 "2022-09-29T11:52:27Z")

</div>

I don’t have any reference off the top of my head. There’s this [Root-finding algorithms - Wikipedia](https://en.wikipedia.org/wiki/Root-finding_algorithms) but I’m not sure it is at your level. Perhaps youtube or Khan academy can help?

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [September 29, 2022, 12:37pm UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/10 "2022-09-29T12:37:04Z")

</div>

If you know that a function is invertible, and you don’t use its inverse later in your code, then you can plot it, taking into account that the graph of the inverse is the reflection (mirror) of the direct one in the line y=x.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [September 29, 2022, 12:59pm UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/11 "2022-09-29T12:59:46Z")

</div>

See also [https://docs.juliahub.com/CalculusWithJulia/AZHbv/0.0.2/precalc/inversefunctions.html](https://docs.juliahub.com/CalculusWithJulia/AZHbv/0.0.2/precalc/inversefunctions.html)

---

<div class="post-metadata">

### Author: ![zoezoe1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zoezoe1/32/43066_2.png) [@zoezoe1](https://discourse.julialang.org/u/zoezoe1)
#### Post date: [September 29, 2022, 1:00pm UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/12 "2022-09-29T13:00:56Z")

</div>

Ah yes this is exactly what I want 🙂 Do you know how I can flip a plot along the line y=x?

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [September 29, 2022, 1:06pm UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/13 "2022-09-29T13:06:23Z")

</div>

For the mirrored just do `plot(y, x)` instead of `plot(x, y)`.

So in your case you might want to actually calculate `y=f.(x,[(...)])` for some range `x=0:dx:1` and use those when plotting.

---

<div class="post-metadata">

### Author: ![zoezoe1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zoezoe1/32/43066_2.png) [@zoezoe1](https://discourse.julialang.org/u/zoezoe1)
#### Post date: [September 29, 2022, 1:37pm UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/14 "2022-09-29T13:37:42Z")

</div>

Awesome, thanks a lot!

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [September 29, 2022, 3:01pm UTC](https://discourse.julialang.org/t/how-to-get-the-inverse-of-a-function/87977/15 "2022-09-29T15:01:29Z")

</div>

Just to give an example with some code to modify, this function will find the inverse over `[a,b]` for a monotonic function `f`, if performance is not an issue:

```julia
using Roots
function inverse_function(f, a, b, args...; kwargs...)
    fa, fb = f(a), f(b)
    m, M = fa < fb ? (fa, fb) : (fb, fa)
    y -> begin # return a function
        @assert m ≤ y ≤ M
        find_zero(x ->f(x) - y, (a,b), args...; kwargs...)
    end
end

```

This can be plotted, for example:

```julia
f(x) = x - sin(x)
a, b = 0, 5pi
plot(inverse_function(f, a, b), f(a), f(b))

```

(though for a plot, using `plot(f.(xs), xs)` is going to be more direct)

If you want an analytic solution, then you can try `SymPy` along the lines of

```julia
using SymPy
@syms x y
solve(y ~ f(x), x)

```

But this only gets you so far (e.g. `f(x) = x- sin(x)` won’t work; `f(x) = x^3 -3` will have multiple answers to sift through, …)
