Sharing a bicycle (part 3)

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.

library(ggplot2)

h <- array(0,dim=1500)
t <- h

tfinal <- function(wA,rA,wB,rB,s,h) {
  t <- 0
  xA <- 0
  xB <- 0
  xC <- 0
  repeat {
    # Step 1
    if(rA <= wB)
      return(s/rA)
    dt <- (h + xB - xA)/(rA - wB)
    if((xA + rA*dt >= s)||(xB + wB*dt >= s))
      if((s - xA)/rA < (s - xB)/wB)
        return(t + (s - xB)/wB)
      else
        return(t + (s - xA)/rA)
    xA <- xA + rA*dt
    xB <- xB + wB*dt
    xC <- xA
    t <- t + dt
    # Step 2
    dt <- h/wB if(xA + wA*dt >= s)
      return(t + dt + (s - xC)/rB)
    xA <- xA + wA*dt
    xB <- xB + h
    t <- t + dt
    # Step 3
    if(rB <= wA)
      return(t + dt + (s - xB)/rB)
    dt <- (h + xA - xB)/(rB - wA)
    if((xB + rB*dt >= s)||(xA + wA*dt >= s))
      if((s - xB)/rB < (s - xA)/wA)
        return(t + (s - xA)/wA)
      else
        return(t + (s - xB)/rB)
    xB <- xB + rB*dt
    xA <- xA + wA*dt
    xC <- xB
    t <- t + dt
    # Step 4
    dt <- h/wA if(xB + wB*dt >=s)
      return(t + dt + (s - xC)/rA)
    xA <- xA + h
    xB <- xB + wB*dt
    t <- t + dt
  }
}

for(i in 1:1500) {
  h[i] <- i
  t[i] <- tfinal(2.9333,8.8,2.9333,8.8,5280,h[i])
}

fdat <- data.frame(dist=h,time=t)

fgraph <- ggplot(data=fdat,aes(h,t)) + geom_line() +
  xlab("Distance ahead (ft)") + ylab("Completion time (sec)")

fgraph

The above code reproduces the graph of completion time vs. the distance ahead from Nahin’s book, which corresponds to the case where the distance between the two houses is 5,280 feet (1 mile) and wA = wB =2.9333 ft/sec, rA = rB = 8.8 ft/sec. The distance ahead varies from 1 ft to 1500 ft in increments of one foot. The graph is below:

The figure is identical to the one in Nahin’s book.

The original question was whether the two complete the journey faster than if they had just walked. Given their walking speed of 2.9333 ft/sec, it would have taken them 5,280/2.9333 = 1,800 seconds to complete the journey had they walked. Clearly it is better to share the bike. The minimum travel time when sharing the bike seems to be 1,200 seconds, a reduction in travel time of one-third.

Now let’s see what happens when one of the friends is faster than the other. Let’s keep the distance between the houses at one mile, and wA = 2.9333, rA = 8.8. But now B is faster than A, with wB = 4, rB = 12. Here is the same plot of completion time vs. distance ahead:

The figure is somewhat different: the completion time is a rather weird function of the distance ahead. There is also a new minimum completion time of about 1,010 ft/sec, which occurs for various values of h. Very interesting!

One thought on “Sharing a bicycle (part 3)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s