title: A root of a "saw-like" function
body: <p>Say, we have <code>f(t) = v * t + A * sin(w * t)</code>. I call such functions &quot;saw-like&quot;:
  <a href="https://i.stack.imgur.com/hHhl1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hHhl1.png" alt="enter image description here" /></a></p>
  <p>I want to solve <code>saw(t) = C</code>, that is, find a root of <code>saw(t) - C</code> (still &quot;saw-like&quot;).</p>
  <p>I tried writing down a ternary search for function <code>abs(saw(t) - C)</code> to find its minima. If we are lucky (or crafty), it would be the root. Unfortunately, my code does not always work: sometimes we get stuck in those places:</p>
  <p><a href="https://i.stack.imgur.com/fuIWG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fuIWG.png" alt="enter image description here" /></a></p>
  <p>My code (python3):</p>
  <pre class="lang-py prettyprint-override"><code>def calculate(fun):
      eps = 0.000000001
      eps_l = 0.1
      x = terns(fun, 0, 100000000000000)
      t = terns(fun, 0, x)
      cnt = 0
      while fun(x) &gt; eps:
          t = x
          x = terns(fun, 0, t)
          if abs(t - x) &lt; eps_l:
              cnt += 1
          # A sorry attempt  pass some wrong value as a right one. 
          # Gets us out of an infinite loop at least.
          if cnt == 10:
              break
      return t
  
  def terns(f, l, r):
      eps = 0.00000000001
      while r - l &gt; eps:
          x_1 = l + (r - l) / 3
          x_2 = r - (r - l) / 3
  
          if f(x_1) &lt; f(x_2):
              r = x_2 
          else:
              l = x_1
      return (l + r) / 2
  </code></pre>
  <p>So, how is it done? Is using ternary search the right way?</p>
  <hr />
  <p>My other idea was somehow sending the equation over to the net, passing it to <a href="https://wolframalpha.com" rel="nofollow noreferrer">Wolfram Alpha</a> and fetching the answers. Yet, I don't how it's done, as I am not quite fluent at python.</p>
  <p>How could this be done?</p>