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
using Optim
f(x) = abs(x-sin(x) - y)
optimize(f, 0, 2π).minimizer
 
But to do this for a matrix of ys I need to
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.
             
            
               
               
               
            
            
           
          
            
              
                DNF  
                
               
                 
              
                  
                    October 21, 2022,  5:09pm
                   
                   
              2 
               
             
            
              What you want to do is called “root finding”. Check out this library:
             
            
               
               
               
            
            
           
          
            
            
              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
             
            
               
               
               
            
            
           
          
            
            
              Never really used Roots.jl before but this should work for you?
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
 
             
            
               
               
              1 Like