title: Find optimized input value of a custom function for specific target value
body: <p>Assuming we have a custom function like:</p>
  
  <pre><code>f &lt;- function(i){
    return(i*i)
  }
  </code></pre>
  
  <p>and we have a target value like 25. I want to find the "ideal" input <code>i</code> for this function in a certain range. This is pretty simple, but i don't want to solve this issue by solving the equation, since my original function is much more complicated (which still can be solved, but that's not the point).</p>
  
  <p>Which function / algorithm can I use to optimize the input value <code>i</code>, so we get the closest to the target value. I using brute force by just creating a mass of inputs like:</p>
  
  <pre><code>for (i in -1000:1000){
      test_values &lt;- c(test_values, f(i))
  }
  </code></pre>
  
  <p>and then finding the minimum of this by using:</p>
  
  <pre><code>which.min(abs(test_values-p_target))
  </code></pre>
  
  <p>But i can't imagine there is not an algorithm for this, but I also can't find anything for this. Obviously my method is very inefficient. Optimization functions like <code>optimize</code> or <code>uniroot</code> are all looking for min or max and not target values. So anyone knows an algorithm or function for optimizing this problem in general?</p>