Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 12.74 KB | None | 0 0
  1. function trabalho2()
  2.     ponto_inicial = [0,1];
  3.     r = 10;
  4.     beta = 0.1;
  5.     tol = 1e-6;
  6.    
  7.    
  8.     %[minimo,iteracoes,tempo] = barreira(ponto_inicial,r,beta,tol,@univariante,0)
  9.     %f1(minimo(1),minimo(2))
  10.     %[minimo,iteracoes,tempo] = barreira(ponto_inicial,r,beta,tol,@powell,0)
  11.     %f1(minimo(1),minimo(2))
  12.     %[minimo,iteracoes] = barreira(ponto_inicial,r,beta,tol,@maxdeclive,1)
  13.     %f1(minimo(1),minimo(2))
  14.     %[minimo,iteracoes] = barreira(ponto_inicial,r,beta,tol,@fletcher_reeves,1)
  15.     %f1(minimo(1),minimo(2))
  16.     %[minimo,iteracoes] = barreira(ponto_inicial,r,beta,tol,@BFGS,1)
  17.     %f1(minimo(1),minimo(2))    
  18.     [minimo,iteracoes,tempo] = barreira(ponto_inicial,r,beta,tol,@newton_raphson,2)
  19.     f1(minimo(1),minimo(2))
  20.    
  21.    
  22.     %{
  23.     [X,Y]=meshgrid(0.8:0.01:1.1);
  24.     contourf(X,Y,f(X,Y))
  25.     hold on
  26.     L = 0.8:0.1:1.1;
  27.     plot(L,teste(L))
  28.     %}
  29. end
  30.  
  31. function y = g(L)
  32.     global g_f;
  33.     global g_x;
  34.     global g_d;
  35.     y = g_f(L*g_d(1) + g_x(1),L*g_d(2) + g_x(2));
  36. end
  37.  
  38. function [minimo,iteracoes,tempo] = barreira(ponto_inicial,r,beta,tol,fun,ordem)
  39.     global g_r;
  40.     passo = 0.001;
  41.     j = 1;
  42.     tic;
  43.     for k = 1:100
  44.         g_r = r;
  45.         if ordem == 0
  46.             [min,iter] = fun(ponto_inicial,@phi,passo);
  47.         elseif ordem == 1
  48.             [min,iter] = fun(ponto_inicial,@phi,@grad_phi,passo);
  49.         else
  50.             [min,iter] = fun(ponto_inicial,@phi,@grad_phi,@hess_phi,passo);
  51.         end
  52.         min
  53.         iter;
  54.         converg = r*b(min(1), min(2))
  55.         if converg < 0 || iter < 0
  56.             passo = passo / 10;
  57.             continue;
  58.         else
  59.             %passo = 0.001;
  60.         end
  61.         j = j + 1;
  62.         if abs(converg) < tol
  63.             minimo = min;
  64.             iteracoes = j;
  65.             break
  66.         else
  67.            r = r*beta;
  68.            ponto_inicial = min;
  69.         end
  70.     end
  71.     toc;
  72.     tempo = toc;
  73. end
  74.  
  75. function y = f1(x1,x2)
  76.     y = (x1 - 2).^4 + (x1 - 2*x2).^2;
  77. end
  78.  
  79. function y = c(x1,x2)
  80.     y = x1.^2 - x2;
  81. end
  82.  
  83. function y = b(x1,x2)
  84.     y = -1./c(x1,x2);
  85. end
  86.  
  87. function y = phi(x1,x2)
  88.     global g_r;
  89.     y = f1(x1,x2) + g_r*b(x1,x2);
  90. end
  91.  
  92. function y = grad_phi(x1,x2)
  93.     global g_r;
  94.     g11 = 2*g_r*x1/(x1.^2 - x2).^2 + 2*(x1 - 2*x2) + 4*(x1 - 2).^3;
  95.     g12 = -g_r/(x1.^2 - x2).^2 - 4*x1 + 8*x2;
  96.     y = [g11,g12];
  97. end
  98.  
  99. function y = hess_phi(x1,x2)
  100.     global g_r;
  101.     h11 = 2*g_r/(x1.^2 - x2).^2 - (8*g_r*(x1.^2))/(x1.^2 - x2).^3 + 12*(x1 - 2).^2 + 2;
  102.     h12 = (4*g_r*x1)/(x1.^2 - x2).^3 - 4;
  103.     h21 = (4*g_r*x1)/(x1.^2 - x2).^3 - 4;
  104.     h22 = 8 - 2*g_r/(x1.^2 - x2).^3;
  105.     y = [h11 h12;h21 h22];
  106. end
  107.  
  108. function [u,iter] = univariante(x0,f,passo)
  109.     global g_f;
  110.     g_f = f;
  111.     global g_x;
  112.     global g_d;
  113.     tol = 1e-6;
  114.     tol2 = 1e-7;
  115.     min_d1 = x0;
  116.     min_d2 = x0;
  117.    
  118.     %para plotar a rota de minimização
  119.     listax(1) = x0(1);
  120.     listay(1) = x0(2);
  121.     j = 2;
  122.    
  123.     for k = 1:50000
  124.         if (norm(min_d1 - min_d2) <= tol) && (k > 1)
  125.             u = min_d2;
  126.             iter = k;
  127.             return;
  128.  
  129.         else
  130.             x0 = min_d2;
  131.             d = [1,0];
  132.             g_x = x0;
  133.             g_d = d;
  134.             [a1,b1] = passoconstante(0, @g, passo, 1);
  135.             c1 = secaoaurea(a1, b1, tol2, @g);
  136.             min_d1 = x0 + d * c1;
  137.            
  138.  
  139.             d = [0,1];
  140.             x0 = min_d1;
  141.             g_x = x0;
  142.             g_d = d;
  143.             [a2,b2] = passoconstante(0, @g, passo, 1);
  144.             c2 = secaoaurea(a2, b2, tol2, @g);
  145.             min_d2 = x0 + d * c2;
  146.            
  147.             %para plotar a rota de minimização
  148.             % listax(j) = min_d1(1);
  149.             % listax(j+1) = min_d2(1);
  150.             % listay(j) = min_d1(2);
  151.             % listay(j+1) = min_d2(2);
  152.             % j = j + 2;
  153.            
  154.         end
  155.     end
  156.    
  157.     u = min_d2;
  158.     iter = -1;
  159.    
  160.     %{
  161.     [X,Y]=meshgrid(-5:0.01:5);
  162.     contourf(X,Y,f1(X,Y))
  163.     hold on
  164.     plot(2,2,'r*')
  165.     plot(x0(1),x0(2),'r*')
  166.     plot(listax, listay, 'red')
  167.     %}
  168. end
  169.  
  170. function [min,iter] = powell(x0,f,passo)
  171.     global g_f;
  172.     g_f = f;
  173.     global g_x;
  174.     global g_d;
  175.  
  176.     tol = 1e-6;
  177.     tol2 = 1e-7;
  178.  
  179.     %para plotar a rota de minimização
  180.     %listax(1) = x0(1);
  181.     %listay(1) = x0(2);
  182.     %j = 2;
  183.  
  184.     u = {[1,0], [0,1]};
  185.     n = 2;
  186.    
  187.     for k = 1:1000
  188.        
  189.         p0 = x0;
  190.         for j=1:n
  191.             g_x = x0;
  192.             g_d = u{j};
  193.             [a1,b1] = passoconstante(0, @g, passo, 1);
  194.             c1 = secaoaurea(a1, b1, tol2, @g);
  195.             %sprintf("%d %d %f %f %f %f %f", k, j, x0(1), x0(2), u{j}(1), u{j}(2), c1)
  196.             x0 = g_x + g_d * c1;
  197.         end
  198.  
  199.         for j=1:n-1
  200.             u{j} = u{j+1};
  201.         end
  202.         u{n} = x0 - p0;
  203.  
  204.         g_x = x0;
  205.         g_d = u{n};
  206.         [a1,b1] = passoconstante(0, @g, passo, 1);
  207.         c1 = secaoaurea(a1, b1, tol2, @g);
  208.         %sprintf("%d %d %f %f %f %f %f", k, 3, x0(1), x0(2), u{n}(1), u{n}(2), c1)
  209.         x0 = g_x + g_d * c1;
  210.         %sprintf("%f %f %f %f", x0(1), x0(2), p0(1), p0(2))
  211.  
  212.         if abs(x0 - p0) < tol
  213.             min = x0;
  214.             iter = k;
  215.             return
  216.         end
  217.  
  218.         if mod(k,n+1) == 0
  219.             %u = {[1,0], [0,1]};
  220.         end
  221.  
  222.         % L = -2:0.0001:2;
  223.         % plot(L,g(L))
  224.         % ylim([-50,50])
  225.         % pause
  226.  
  227.        
  228.         %para plotar a rota de minimização
  229.         %listax(j) = min_d1(1);
  230.         %listax(j+1) = min_d2(1);
  231.         %listay(j) = min_d1(2);
  232.         %listay(j+1) = min_d2(2);
  233.         %j = j + 2;
  234.            
  235.        
  236.     end
  237.    
  238.     iter = -1;
  239.    
  240.     %[X,Y]=meshgrid(-5:0.01:5);
  241.     %contourf(X,Y,f1(X,Y))
  242.     %hold on
  243.     %plot(2,2,'r*')
  244.     %plot(x3(1),x3(2),'r*')
  245.     %plot(listax, listay, 'red')
  246. end
  247.  
  248. function [min,iter] = maxdeclive(x0,f,grad_f,passo)
  249.     global g_f;
  250.     g_f = f;
  251.     global g_x;
  252.     global g_d;
  253.  
  254.     tol = 1e-6;
  255.     tol2 = 1e-7;
  256.  
  257.     for k = 1:5000
  258.         x = x0;
  259.         d = -grad_f(x0(1),x0(2));
  260.         if norm(d) <= tol
  261.             min = x0;
  262.             iter = k;
  263.             return
  264.         end
  265.         g_x = x;
  266.         g_d = d;
  267.         [a,b] = passoconstante(0, @g, passo, 1);
  268.         c = secaoaurea(a, b, tol2, @g);
  269.         min = [x(1) + d(1)*c, x(2) + d(2)*c];
  270.         x0 = min;
  271.     end
  272.     iter = -1;
  273.     min = x0;
  274. end
  275.  
  276. function [min,iter] = fletcher_reeves(x0,f,grad_f,passo)
  277.     global g_f;
  278.     g_f = f;
  279.     global g_x;
  280.     global g_d;
  281.  
  282.     tol = 1e-6;
  283.     tol2 = 1e-7;
  284.     x = x0;
  285.     grad_x0 = grad_f(x0(1),x0(2));
  286.     d = -grad_x0;
  287.    
  288.     %para plotar a rota de minimização
  289.     %{
  290.     listax(1) = x0(1);
  291.     listay(1) = x0(2);
  292.     j = 2;
  293.     %}
  294.  
  295.     %tic;
  296.     for k = 1:100
  297.         if norm(d) <= tol
  298.             min = x;
  299.             iter = k;
  300.             break
  301.         end
  302.         g_x = x;
  303.         g_d = d;
  304.         [a,b] = passoconstante(0, @g, passo, 1);
  305.         c = secaoaurea(a, b, tol2, @g);
  306.         %L = c-5:0.1:c+5;
  307.         %plot(L,g(L))
  308.         x1 = [x(1) + d(1)*c, x(2) + d(2)*c];
  309.         grad_x1 = grad_f(x1(1),x1(2));
  310.         if norm(grad_x1) <= tol
  311.             min = x1;
  312.             iter = k;
  313.             break
  314.         end
  315.         beta = (grad_x1*(grad_x1.'))/(grad_x0*(grad_x0.'));
  316.         d = -grad_x1 + beta.*d;
  317.         x = x1;
  318.         grad_x0 = grad_x1;
  319.        
  320.         %para plotar a rota de minimização
  321.         %{
  322.         listax(j) = x1(1);
  323.         listax(j+1) = x(1);
  324.         listay(j) = x1(2);
  325.         listay(j+1) = x(2);
  326.         j = j + 2;
  327.         %}
  328.     end
  329.    
  330.     %{
  331.     toc;
  332.     t = toc;
  333.    
  334.     [X,Y]=meshgrid(-5:0.01:5);
  335.     contourf(X,Y,f1(X,Y))
  336.     hold on
  337.     plot(2,2,'r*')
  338.     plot(x1(1),x1(2),'r*')
  339.     plot(listax, listay, 'red')
  340.     %}
  341. end
  342.  
  343. function [min,iter] = BFGS(x0,f,grad_f,passo)
  344.     global g_f;
  345.     g_f = f;
  346.     global g_x;
  347.     global g_d;
  348.     tol = 1e-6;
  349.     tol2 = 1e-7;
  350.     x = x0;
  351.     S0 = [1 0;0 1];
  352.     g_x0 = grad_f(x0(1),x0(2));
  353.     d = -(S0*g_x0.').';
  354.    
  355.     %para plotar a rota de minimização
  356.     %{
  357.     listax(1) = x(1);
  358.     listay(1) = x(2);
  359.     j = 2;
  360.    
  361.     tic;
  362.     %}
  363.     for k = 1:100
  364.         if norm(d) <= tol
  365.             min = x;
  366.             iter = k;
  367.             break
  368.         end
  369.         g_x = x;
  370.         g_d = d;
  371.         [a,b] = passoconstante(0, @g, passo, 1);
  372.         c = secaoaurea(a, b, tol2, @g);
  373.         x1 = [x(1) + d(1)*c, x(2) + d(2)*c];
  374.         g_x1 = grad_f(x1(1),x1(2));
  375.         if norm(g_x1) <= tol
  376.             min = x1;
  377.             iter = k;
  378.             break
  379.         end
  380.         d_xk = (x1 - x0).';
  381.         d_gk = (g_x1 - g_x0).';
  382.         primeiro_termo = (((d_xk.')*d_gk + (d_gk.')*S0*d_gk)*d_xk*(d_xk.'))/((d_xk.')*d_gk)^2;
  383.         segundo_termo = (S0*d_gk*(d_xk.') + d_xk*((S0*d_gk).'))/((d_xk.')*d_gk);
  384.         S1 = S0 + primeiro_termo - segundo_termo;
  385.        
  386.         d = -(S1*g_x1.').';
  387.         x = x1;
  388.         S0 = S1;
  389.         g_x0 = g_x1;
  390.        
  391.         %para plotar a rota de minimização
  392.         %{
  393.         listax(j) = x(1);
  394.         listax(j+1) = x1(1);
  395.         listay(j) = x(2);
  396.         listay(j+1) = x1(2);
  397.         j = j + 2;
  398.         %}
  399.     end
  400.    
  401.     %{
  402.     toc;
  403.     t = toc;
  404.    
  405.     [X,Y]=meshgrid(-5:0.01:5);
  406.     contourf(X,Y,f1(X,Y))
  407.     hold on
  408.     plot(2,2,'r*')
  409.     plot(x1(1),x1(2),'r*')
  410.     plot(listax, listay, 'red')
  411.     %}
  412. end
  413.  
  414. function [min,iter] = newton_raphson(x0,f,grad_f,hess_f,passo)
  415.     global g_f;
  416.     g_f = f;
  417.     global g_x;
  418.     global g_d;
  419.     tol = 1e-5;
  420.     tol2 = 1e-5;
  421.     x = x0;
  422.     grad_x0 = grad_f(x0(1),x0(2));
  423.     h = hess_f(x0(1),x0(2));
  424.     inv_h = inv(h);
  425.     d = -(inv_h*grad_x0.').';
  426.    
  427.     %para plotar a rota de minimização
  428.     %{
  429.     listax(1) = x(1);
  430.     listay(1) = x(2);
  431.     j = 2;
  432.     %}
  433.     %tic;
  434.    
  435.     for k = 1:1000
  436.         if norm(d) <= tol
  437.             min = x;
  438.             iter = k;
  439.             return
  440.         end
  441.         g_x = x;
  442.         g_d = d;
  443.         [a,b] = passoconstante(0, @g, passo, 1);
  444.         c = secaoaurea(a, b, tol2, @g);
  445.         x1 = [x(1) + d(1)*c, x(2) + d(2)*c];
  446.         grad_x1 = grad_f(x1(1),x1(2));
  447.         if norm(grad_x1) <= tol
  448.             min = x1;
  449.             iter = k;
  450.             %listax(j) = x1(1);
  451.             %listay(j) = x1(2);
  452.             return
  453.         end
  454.        
  455.         h = hess_f(x1(1),x1(2));
  456.         inv_h = inv(h);
  457.         d = -(inv_h*grad_x1.').';
  458.         x = x1;
  459.        
  460.         %para plotar a rota de minimização
  461.         %{
  462.         listax(j) = x(1);
  463.         listax(j+1) = x1(1);
  464.         listay(j) = x(2);
  465.         listay(j+1) = x1(2);
  466.         j = j + 2;
  467.         %}
  468.     end
  469.        
  470.     iter = -1;
  471.     min = x;
  472.     %toc;
  473.     %t = toc;
  474.    
  475.     %{
  476.     [X,Y]=meshgrid(-5:0.01:5);
  477.     contourf(X,Y,f1(X,Y))
  478.     hold on
  479.     plot(2,2,'r*')
  480.     plot(x1(1),x1(2),'r*')
  481.     plot(listax, listay, 'red')
  482.     %}
  483. end
  484.  
  485. function [y,w] = passoconstante(x0,f,da,d)
  486.     % y0 = f(x0);
  487.  
  488.     % for k = 1:10000000
  489.     %     x1 = x0 + da*d;
  490.     %     y1 = f(x1);
  491.     %     if k == 1
  492.     %         if y1 > y0
  493.     %             d = -d;
  494.     %             continue
  495.     %         elseif y1 == y0
  496.     %             y = min([x0 - da*d,x0 + da*d]);
  497.     %             w = max([x0 - da*d,x0 + da*d]);
  498.     %             break
  499.     %         end
  500.     %     else
  501.     %         if y1 >= y0
  502.     %             y = min([x0 - da*d,x0 + da*d]);
  503.     %             w = max([x0 - da*d,x0 + da*d]);
  504.     %             break
  505.     %         end
  506.     %     end
  507.     %     x0 = x1;
  508.     %     y0 = y1;
  509.     % end
  510.  
  511.     x1 = x0;
  512.     h = da * d;
  513.     c = 1.618033989;
  514.     f1 = f(x1);
  515.     x2 = x1 + h; f2 = f(x2);
  516.     % Determine downhill direction & change sign of h if needed.
  517.     if f2 > f1
  518.         h = -h;
  519.         x2 = x1 + h; f2 = f(x2);
  520.         % Check if minimum is between x1 - h and x1 + h
  521.         if f2 > f1
  522.             y = x2; w = x1 - h; return
  523.         end
  524.     end
  525.     % Search loop
  526.     for i = 1:100
  527.         h = c*h;
  528.         x3 = x2 + h; f3 = f(x3);
  529.         if f3 > f2
  530.             y = x1; w = x3; return
  531.         end
  532.         x1 = x2; f1 = f2; x2 = x3; f2 = f3;
  533.     end
  534. end
  535.  
  536. function y = secaoaurea(aL,aU,tol,f)
  537.     Ra = (sqrt(5)-1)/2;
  538.    
  539.     da = aU - aL;
  540.  
  541.     aE = (1-Ra)*da + aL;
  542.     aD = Ra*da + aL;
  543.     faE = f(aE);
  544.     faD = f(aD);
  545.  
  546.     for k = 1:5000
  547.         if abs(da) <= tol
  548.             y = (aL + aU)/2;
  549.             break
  550.         end
  551.  
  552.         if faE >= faD
  553.             aL = aE;
  554.             da = aU - aL;
  555.  
  556.             aE = aD;
  557.             faE = faD;
  558.             aD = Ra*da + aL;
  559.             faD = f(aD);
  560.         else
  561.             aU = aD;
  562.             da = aU - aL;
  563.  
  564.             aD = aE;
  565.             faD = faE;
  566.             aE = (1-Ra)*da + aL;
  567.             faE = f(aE);
  568.         end
  569.     end  
  570. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement