problem3_shortestpath.m 544 B

12345678910111213141516
  1. function main()
  2. % Constants for test
  3. a = 3; b = 5; d = 12; w = 1;
  4. % Testing all possible bridge positions (0 to d)
  5. [bridge_pos, path_distance] = fminbnd(@(x) calc_path(a,b,w,d,x), 0, d)
  6. end
  7. % a : vertical distance from town A to the river
  8. % b : vertical distance from town B to the river
  9. % w : width of the river
  10. % d : horizontal distance from town A to town B
  11. % x : horizontal distance of the bridge from town A
  12. function path_distance = calc_path(a, b, w, d, x)
  13. path_distance = sqrt(x^2+a^2) + sqrt((d-x)^2+b^2) + w;
  14. end