The code is easy to understand, but the speed function must be between [1e-8,1], otherwise you get a complex number to value of T, where T is arrival time - this is not correct. Anybody observe/solve that? *Notice that such a constraint for F is theoretically wrong.*