0

Say, we have f(t) = v * t + A * sin(w * t). I call such functions "saw-like": enter image description here

I want to solve saw(t) = C, that is, find a root of saw(t) - C (still "saw-like").

I tried writing down a ternary search for function abs(saw(t) - C) 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:

enter image description here

My code (python3):

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) > eps:
        t = x
        x = terns(fun, 0, t)
        if abs(t - x) < 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 > eps:
        x_1 = l + (r - l) / 3
        x_2 = r - (r - l) / 3

        if f(x_1) < f(x_2):
            r = x_2 
        else:
            l = x_1
    return (l + r) / 2

So, how is it done? Is using ternary search the right way?


My other idea was somehow sending the equation over to the net, passing it to Wolfram Alpha and fetching the answers. Yet, I don't how it's done, as I am not quite fluent at python.

How could this be done?

Zhiltsoff Igor
  • 1,696
  • 5
  • 21
  • Why do you assume that the minima of `saw(t) - C` would be zero? At these minima, the _derivative_ is zero, but the value can be anything. – MSalters Jan 09 '21 at 16:16
  • Getting stuck in local minima — that's one of the first problems we learned about in my undergraduate AI course. From there, we learned of search techniques that are efficient but not perfectly accurate, like simulated annealing. If Wolfram gives accurate answers, I have to think they combine direct analysis of the function (rather than naive searching) with a lot of computing power. – Noah Jan 09 '21 at 16:17
  • @MSalters I calculate the minima of `abs(saw(t) - C)`, not `saw(t) - C`. I believe `|f(x)|` (for any `f`) to have its minimum in `f`'s root (if there are any). – Zhiltsoff Igor Jan 09 '21 at 16:17
  • @Noah, well, my second idea is basically "not re-inventing the bicycle", although it is of much less fun. – Zhiltsoff Igor Jan 09 '21 at 16:19
  • You can use the `requests` module and [WolphramAlpha's API](https://products.wolframalpha.com/api/) for your second idea – Vthechamp Jan 09 '21 at 16:21
  • @ZhiltsoffIgor: Ah, right. I now understand where you're coming from, but that's generally not a very efficient method of finding roots. Ternary search doesn't really make sense on functions where the derivative is not continuous. – MSalters Jan 09 '21 at 16:23
  • @MSalters I agree, therefore the loop in `calculate` :). It rarely gets the right answer on the first iteration, but it often reaches it later. Unfortunately, still not always, hence the question. – Zhiltsoff Igor Jan 09 '21 at 16:25
  • If your function is differentiable then you can use Newton's method. There are many alternatives which should converge faster than what is essentially interval bisection. – kaya3 Jan 09 '21 at 16:51

0 Answers0