problem3_shortestpath.m 872 B

1234567891011121314151617181920212223242526
  1. % Constants for test
  2. a = 3; b = 5; d = 12; w = 1;
  3. % Testing all possible bridge positions (0 to d)
  4. [bridge_pos, path_distance] = fminbnd(@(x) calc_path(a,b,w,d,x), 0, d) %[output:32ce9065] %[output:590b847e]
  5. % a : vertical distance from town A to the river
  6. % b : vertical distance from town B to the river
  7. % w : width of the river
  8. % d : horizontal distance from town A to town B
  9. % x : horizontal distance of the bridge from town A
  10. function path_distance = calc_path(a, b, w, d, x)
  11. path_distance = sqrt(x^2+a^2) + sqrt((d-x)^2+b^2) + w;
  12. end
  13. %[appendix]{"version":"1.0"}
  14. %---
  15. %[metadata:view]
  16. % data: {"layout":"onright"}
  17. %---
  18. %[output:32ce9065]
  19. % data: {"dataType":"textualVariable","outputData":{"name":"bridge_pos","value":"4.5000"}}
  20. %---
  21. %[output:590b847e]
  22. % data: {"dataType":"textualVariable","outputData":{"name":"path_distance","value":"15.4222"}}
  23. %---