Created
November 3, 2011 18:16
-
-
Save Xion/1337256 to your computer and use it in GitHub Desktop.
Very simple optimization problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The wreck of a plane in a desert is 15 miles from the nearest point A on a straight road. | |
# A rescue truck starts for the wreck at a point on the road that is 30 miles distant from A. | |
# If the truck can travel at 80 mph on the road and at 40 mph on a straight path in the desert, | |
# how far from A should the truck leave the road to reach the wreck in a minimum time? | |
from math import sqrt | |
def truck_time(turn_point): | |
road_distance = turn_point | |
skipped_road_distance = 30 - turn_point | |
desert_distance = sqrt(skipped_road_distance ** 2 + 15 ** 2) | |
road_time = road_distance / 80.0 | |
desert_time = desert_distance / 40 | |
return road_time + desert_time | |
for i in xrange(0, 30): | |
print "Turning at %s results in %s h" % (i, truck_time(i)) | |
# Minimum is around 22, which agrees with intuitive guess 30 * (1/sqrt(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool :D