Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.86 KB | None | 0 0
  1. clear;
  2. close all;
  3.  
  4. A = [0.4873 -0.8732;
  5.     0.6072 0.7946;
  6.     0.9880 -0.1546;
  7.     -0.2142 -0.9768;
  8.     -0.9871 -0.1601;
  9.     0.9124 0.4093];
  10.  
  11. b = ones(1,6)';
  12. c = [1 ;-1];
  13. x0 = [0 0]';
  14.  
  15. [m,n] = size(A);
  16.  
  17. t_init = 0.2;
  18. t = t_init;
  19. gamma = 2;
  20.  
  21. eps = 10^-15;
  22.  
  23. alfa = 0.5;
  24. beta = 0.8;
  25.  
  26. xk = x0;
  27. sk = 0;
  28.  
  29. psi = @(x,t) t*c'*x - sum(log(b-A*x));
  30. grad_psi = @(x,t) t*c +(A')*(1./(b-A*x));
  31. hessian_psi = @(x)((diag(1./(b-A*x))*A)')*(diag(1./(b-A*x))*A);
  32.  
  33. results = [];
  34.  
  35. while(m/t>eps)
  36.    
  37.     s = 1;
  38.     while(psi(xk-s*(hessian_psi(xk)^-1)*grad_psi(xk,t),t) >= psi(xk,t)-s*alfa*grad_psi(xk,t)'*(hessian_psi(xk)^-1)*grad_psi(xk,t))
  39.         s = s*beta;
  40.     end
  41.    
  42.     sk = s;
  43.     xk = xk-sk*(hessian_psi(xk)^-1)*grad_psi(xk,t);
  44.     results = [results,xk];
  45.     t = gamma * t;
  46.    
  47. end
  48.  
  49.  
  50. xk
  51.  
  52. show_figure(A,b,results,x0);
  53.  
  54. function y = fbar (x1, x2)
  55. A = [0.4873 -0.8732;
  56.     0.6072 0.7946;
  57.     0.9880 -0.1546;
  58.     -0.2142 -0.9768;
  59.     -0.9871 -0.1601;
  60.     0.9124 0.4093];
  61.  
  62. b = ones(1,6)';
  63. x = [x1 ; x2];
  64.     if sum(b - A * x < 0) == 0
  65.         y = -sum(log(b - A * x));
  66.     else
  67.         y = NaN;
  68.     end
  69. end
  70.  
  71. function show_figure(A,b,results,x0)
  72.  
  73. barrier_f = @(x) -sum(log(b-A*x));
  74.  
  75. P = polytope(A,b);
  76. plot(P);
  77. xlim([-2.5 2])
  78. ylim([-1.5 3])
  79. xlabel('$x_{1}$','Interpreter','latex')
  80. ylabel('$x_{2}$','Interpreter','latex')
  81. hold on;
  82. scatter(results(1,:),results(2,:),20,'MarkerEdgeColor','b','MarkerFaceColor','b');
  83. scatter(x0(1),x0(2),20,'MarkerEdgeColor','black','MarkerFaceColor','black');
  84.  
  85. x1_lim = xlim;
  86. x2_lim = ylim;
  87.  
  88. x1_lim = [x1_lim(1)-0.5,x1_lim(2)+0.5];
  89. x2_lim = [x2_lim(1)-0.5,x2_lim(2)+0.5];
  90.  
  91. x1 = x1_lim(1):0.01:x1_lim(2);
  92. x2 = x2_lim(1):0.01:x2_lim(2);
  93.  
  94. [X1, X2] = meshgrid(x1, x2);
  95. Z = arrayfun(@fbar, X1, X2);
  96.  
  97. v = real(barrier_f(results));
  98. [C,h] = contour(X1, X2, Z, v,'color','black');
  99. clabel(C,h)
  100.  
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement