# How to calculate non linear function

**URL:** https://discourse.julialang.org/t/how-to-calculate-non-linear-function/89072
**Category:** General Usage
**Tags:** optimization
**Created:** [October 21, 2022, 4:06pm UTC](https://discourse.julialang.org/t/how-to-calculate-non-linear-function/89072 "2022-10-21T16:06:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [October 21, 2022, 4:06pm UTC](https://discourse.julialang.org/t/how-to-calculate-non-linear-function/89072/1 "2022-10-21T16:06:37Z")

</div>

So I need to calculate `x-sin(x)=y` where y is known. It isn’t possible to solve for `x` so I need to approximate.  
My first try is

```julia
using Optim
f(x) = abs(x-sin(x) - y)
optimize(f, 0, 2π).minimizer

```

But to do this for a matrix of `y`s I need to

```julia
using Optim
function cacly(y)
	f(x) = abs(x-sin(x) - y)
	optimize(f, 0,2π).minimizer
end
x = cacly.(y)

```

I feel like there has to be a better way. To me, this is extremely ugly.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [October 21, 2022, 5:09pm UTC](https://discourse.julialang.org/t/how-to-calculate-non-linear-function/89072/2 "2022-10-21T17:09:17Z")

</div>

What you want to do is called “root finding”. Check out this library:

> **[GitHub - JuliaMath/Roots.jl: Root finding functions for Julia](https://github.com/JuliaMath/Roots.jl)**
>
> Root finding functions for Julia. Contribute to JuliaMath/Roots.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [October 21, 2022, 5:36pm UTC](https://discourse.julialang.org/t/how-to-calculate-non-linear-function/89072/3 "2022-10-21T17:36:14Z")

</div>

Thank you for the response but, sorry, I don’t immediately see anything here that solves my problem.  
Sorry for being unclear, my problem is that I need to recompile the function `x-sin(x)` for every y, which results in 99% compilation time everytime.  
What I need is a general method of calculating `x` for a given `y`

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [October 21, 2022, 5:59pm UTC](https://discourse.julialang.org/t/how-to-calculate-non-linear-function/89072/4 "2022-10-21T17:59:22Z")

</div>

Never really used Roots.jl before but this should work for you?

```julia
julia> using Roots

julia> f(x, p=0.0) = x-sin(x) - p
f (generic function with 2 methods)

julia> Z = ZeroProblem(f, 0.01) # 0.01 is an arbitrary start value
ZeroProblem{typeof(f), Float64}(f, 0.01)

julia> @time solve(Z, p=0.1)
  0.614689 seconds (6.49 M allocations: 312.507 MiB, 21.92% gc time, 99.99% compilation time)
0.8537501566408657

julia> @time solve(Z, p=0.2)
  0.000012 seconds (1 allocation: 16 bytes)
1.083691880314489

julia> @time solve(Z, p=0.3)
  0.000011 seconds (1 allocation: 16 bytes)
1.2485154675427026

julia> @time solve(Z, p=0.4)
  0.000011 seconds (1 allocation: 16 bytes)
1.3822841337179512

```

---

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [October 21, 2022, 6:08pm UTC](https://discourse.julialang.org/t/how-to-calculate-non-linear-function/89072/5 "2022-10-21T18:08:46Z")

</div>

Thank you very much
