Guest User

Untitled

a guest
Mar 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. %% Searching for optimal bridge place - RIVER_2
  2. % In this problem we will be searching for the most optimal place for the
  3. % bridge between city A and city B. And the river is desricbed by circle
  4. % Author: Paweł Drapiewski
  5.  
  6. clear all;
  7. close all;
  8.  
  9. plot_bound_x_low = 0;
  10. plot_bound_x_high = 5;
  11.  
  12. % Define the paramters for the river
  13. O = [1, 5]; % the center of the river curve
  14. r1 = 2;
  15. r2 = 3;
  16.  
  17. % define the cities
  18. a_point = [2, 5];
  19. b_point = [3, 1];
  20.  
  21. optimal_th = 0;
  22. p1 = get_point_on_circle_based_on_angle(O, r1, optimal_th);
  23. p2 = get_point_on_circle_based_on_angle(O, r2, optimal_th);
  24.  
  25. cvx_solver('sdpt3')
  26. cvx_begin
  27. variable optimal_th
  28. variable p1(2)
  29. variable p2(2)
  30. minimize norm(a_point' - p1) + norm(b_point' - p2);
  31. subject to
  32. p1 == norm(get_point_on_circle_based_on_angle(O, r1, optimal_th), Inf);
  33. p2 == norm(get_point_on_circle_based_on_angle(O, r2, optimal_th), Inf);
  34. optimal_th > 0;
  35. optimal_th < 2 * pi;
  36.  
  37. cvx_end
  38.  
  39. % find cooridnates for the plotting the river
  40. % Circle 1
  41. th = 0:pi/50:2*pi;
  42. x_circle1 = r1 * cos(th) + O(1);
  43. y_circle1 = r1 * sin(th) + O(2);
  44. % Circle 2
  45. th = 0:pi/50:2*pi;
  46. x_circle2 = r2 * cos(th) + O(1);
  47. y_circle2 = r2 * sin(th) + O(2);
  48.  
  49. % find cooridnates for ploting the dashed line (f)
  50. m = (p2(2)-p2(2))/(p2(1)-p1(1));
  51. n = p1(2)- p1(1)*m;
  52. f_y1 = m*plot_bound_x_low + n;
  53. f_y2 = m*plot_bound_x_high + n;
  54.  
  55. % draw the plot
  56. hold on;
  57. title("River 2");
  58. daspect([1 1 1]);
  59. xlim([plot_bound_x_low plot_bound_x_high]);
  60. ylim([0 6]);
  61. grid on;
  62.  
  63. plot(O(1), O(2), '*');
  64. plot(x_circle1, y_circle1, 'blue');
  65. plot(x_circle2, y_circle2, 'blue');
  66. plot([plot_bound_x_low plot_bound_x_high], [f_y1, f_y2], 'r--');
  67. plot(p1(1), p1(2), 'black*');
  68. plot(p2(1), p2(2), 'black*');
  69. plot([p1(1) p2(1)], [p1(2), p2(2)], 'black', 'LineWidth', 2);
  70. plot([a_point(1), p1(1)], [a_point(2), p1(2)], 'black')
  71. plot([b_point(1), p2(1)], [b_point(2), p2(2)], 'black')
  72. plot(a_point(1), a_point(2) ,'ro');
  73. plot(b_point(1), b_point(2) ,'ro');
  74.  
  75. hold off;
  76.  
  77. function result = get_point_on_circle_based_on_angle(O, r, th)
  78. x = r * cos(th) + O(1);
  79. y = r * sin(th) + O(2);
  80. result = [x, y];
  81. end
Add Comment
Please, Sign In to add comment