The function tfinal that I developed for part 3 returns the time for the two friends to complete a journey as a function of six variables: the total distance (s), the distance ahead of the other friend that the rider leaves the bicycle (h), and the walking and riding speeds of each friend (wA,rA,wB, and rB respectively). The graph of completion time vs. distance ahead is so strange-looking that I decided I wanted to see what the whole process looked like for given values of wA, rA, wB, rB, s, and h. That meant writing a function to return a data frame containing the elapsed time and the position of each friend and the bicycle at the end of each event.
Having derived the equations of motion, it’s a simple matter to translate the math into R code. As we’ll see, the difficult part is understanding the results. The code is below; the most important part is the function tfinal, which implements the four steps as described in previous posts. There are a lot of if statements, and if it weren’t for the return statement I’m not sure how I would have worked this out. This is one case where I really felt the need for a goto statement.