—-
Triathlete's Dilemma
You are a triathlete in the water and you need to get to your bike parked in a rack at a specific location on the shore in the shortest amount of time possible. You can run faster than you can swim. Do you swim directly to the shore, then run to your bike? Do you swim to a point closer to your bike, then run? How do you solve this dilemma? Here are the specific parameters:
Your goal is to determine the optimal x-coordinate shore point to swim towards to get to your bike in the shortest possible time. Using the starter code below, decide what you need in order to compute the sim time and corresponding run time. You are probably aware that there is no reason to swim away from your bike or beyond your bike, so limit your shore point analysis to the x-axis range established by the initial x-coordinates of the Start and End locations. As a starting point, you should consider every half meter point along the shoreline.
You will examine your total time results graphically. Notice in the code, that the graph commands are already there, but the graph won't be generated until you produce some data. Your “while” loop should include a calculation for the swim time, run time, and total time for each half-meter position along the beach. Your graph will plot corresponding shore positions versus total time.
Extension questions:
GlowScript 2.8 VPython #scene attributes scene.width = 750 scene.height = 568 scene.fov = .3 scene.align = "left" ## Objects for the lake, beach, start and end points, and labels lake = box(pos=vector(0,100,0), size=vector(300,200,2), axis=vector(1,0,0), color=color.cyan, texture=textures.rough) beach = box(pos=vector(0,-50,0), size=vector(300,100,2), axis=vector(1,0,0), color=color.yellow, texture=textures.stucco) start = sphere(pos=vec(-50,120,0), radius=10, color=color.green) end = sphere(pos=vec(100,-50,0), radius=10, color=color.red) starttext = text(pos=vec(-65,130,0), text='Start', depth=2, height=10, color=color.green) endtext = text(pos=vec(90,-70,0), text='End', depth=2, height=10, color=color.red) #Initial swim speed and run speed swimv = 2 runv = 9 #Calculations to determine the amount of time for swim and amount of time for run--think distance/speed... swimtime = 0 runtime = 0 totaltime = swimtime+runtime #this draws a line from start to shore point and from shore point to end target = 0 swimroute = cylinder(pos=vec(start.pos.x,start.pos.y,1), axis=vector(target-start.pos.x,0-start.pos.y,1), radius=1, color=color.green) runroute = cylinder(pos=vec(end.pos.x,end.pos.y,1), axis=vec(target-end.pos.x,0-end.pos.y,1), radius=1, color=color.red) ##graph total time vs shore point position Grph1 = graph(title='Finding Optimal Path', xtitle='Shoreline Coordinate (m)', ytitle='Total Time (s)', fast=False, align="right", ymin=0, ymax=200, xmin=-50, xmax=100) TimeOpt = gcurve(color=color.red, label='Optimal Path') while target<0: rate(120) swimtime = runtime = totaltime = swimtime+runtime swimroute.axis = vec(target-start.pos.x,0-start.pos.y,1) runroute.axis = vec(target-end.pos.x,0-end.pos.y,1) TimeOpt.plot(target,totaltime) target = target + dt
Our first modifications to the code are defining the variables “target” (line 22), and “final” (line 24). This represents at which x-coordinate you reach the shore and the x-coordinate of the bike rack, respectively. We add the variable “dt” (line 23) which is a miniscule increment in time, and used later in our while loop. We then create equations for the “swimtime” and “runtime” variables (lines 25 & 26); these equations are just a rearranged form of the Pythagorean Theorem solved for the length of the hypotenuse and divided by the swim speed and run speed variables—remember that distance divided by speed is time. Finally, the limits of the while loop are defined (line 35) and the swim time and run time equations are reentered (lines 37 & 38). Looking at the output graph, the optimal x-coordinate along the shore line is equal to -24.5.
Extension Solutions:
GlowScript 2.8 VPython #scene attributes scene.width = 750 scene.height = 568 scene.fov = .3 scene.align = "left" ## Objects for the lake and beach lake = box(pos=vector(0,100,0), size=vector(300,200,2), axis=vector(1,0,0), color=color.cyan, texture=textures.rough) beach = box(pos=vector(0,-50,0), size=vector(300,100,2), axis=vector(1,0,0), color=color.yellow, texture=textures.stucco) start = sphere(pos=vec(-50,120,0), radius=10, color=color.green) end = sphere(pos=vec(100,-50,0), radius=10, color=color.red) starttext = text(pos=vec(-65,130,0), text='Start', depth=2, height=10, color=color.green) endtext = text(pos=vec(90,-70,0), text='End', depth=2, height=10, color=color.red) #Initial swim speed and run speed swimv = 2 runv = 9 #Calculations to determine the amount of time for swim and amount of time for run--think distance/speed... target = -50 dt = 0.5 final = 100 swimtime = sqrt(start.pos.y**2 + (target-start.pos.x)**2)/swimv runtime = sqrt(end.pos.y**2 + (end.pos.x-target)**2)/runv totaltime = swimtime+runtime swimroute = cylinder(pos=vec(start.pos.x,start.pos.y,1), axis=vector(target-start.pos.x,0-start.pos.y,1), radius=1, color=color.green) runroute = cylinder(pos=vec(end.pos.x,end.pos.y,1), axis=vec(target-end.pos.x,0-end.pos.y,1), radius=1, color=color.red) ##graph net force vs position Grph1 = graph(title='Finding Optimal Path', xtitle='Shoreline Coordinate (m)', ytitle='Total Time (s)', fast=False, align="right", ymin=0, ymax=300, xmin=-50, xmax=100) TimeOpt = gcurve(color=color.red, label='Optimal Path') while target<final: rate(120) swimtime = sqrt(start.pos.y**2 + (target-start.pos.x)**2)/swimv runtime = sqrt(end.pos.y**2 + (end.pos.x-target)**2)/runv totaltime = swimtime+runtime swimroute.axis = vec(target-start.pos.x,0-start.pos.y,1) runroute.axis = vec(target-end.pos.x,0-end.pos.y,1) TimeOpt.plot(target,totaltime) target = target + dt