Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.65 KB | None | 0 0
  1. function trabalho01_ex2a()
  2.     molas = 100;
  3.     variaveis = 2 * (molas + 1) - 4;
  4.     x0 = zeros(1, variaveis);
  5.     for i=1:2:variaveis
  6.         if i <= variaveis/2
  7.             percentual = i / (variaveis/2);
  8.             x0(i) = 0.01 * percentual;
  9.             x0(i+1) = -0.1 * percentual;
  10.         else
  11.             percentual = (variaveis-i) / (variaveis/2);
  12.             x0(i) = 0.01 * percentual;
  13.             x0(i+1) = -0.1 * percentual;
  14.         end
  15.     end
  16.  
  17.     for i=1:2:variaveis
  18.         u(floor(i/2)+1) = x0(i);
  19.         v(floor(i/2)+1) = x0(i+1);
  20.     end
  21.  
  22.     x0 = [u,v];
  23.  
  24.     %x0 = [0.01, -0.1];
  25.     comprimento1 = 30;
  26.     comprimento2 = 30;
  27.     setComprimento(comprimento1, comprimento2, molas);
  28.     setRho(8, 16);
  29.     setEA(27000, 18000);
  30.     %setRho(8, 8);
  31.     %setEA(27000, 27000);
  32.  
  33.     global g_l;
  34.     global g_k;
  35.     for k = 1:molas
  36.         g_l(k) = getL(k, molas);
  37.         g_k(k) = getK(k, molas);
  38.     end
  39.  
  40.     global g_w;
  41.     nos_internos = molas - 1;
  42.     for k = 1:nos_internos
  43.         mola_antes = k;
  44.         mola_depois = k + 1;
  45.         P_antes = getP(mola_antes, molas);
  46.         P_depois = getP(mola_depois, molas);
  47.         g_w(k) = (P_antes/2) + (P_depois/2);
  48.     end
  49.  
  50.     [x1,iter] = univariante(x0, @f)
  51.    
  52.     x = zeros(1,molas+1);
  53.     y = zeros(1,molas+1);
  54.     for i=1:molas
  55.         xi = (comprimento1+comprimento2) / molas * i;
  56.         x(i+1) = xi;
  57.         if i < molas
  58.             y(i+1) = -x1(molas + i - 1);
  59.         end
  60.     end
  61.     plot(x, y);
  62.     pause;
  63. end
  64.  
  65. function setComprimento(comprimento1, comprimento2, num_molas)
  66.     global L1;
  67.     global L2;
  68.     L1 = comprimento1 / (num_molas / 2);
  69.     L2 = comprimento2 / (num_molas / 2);
  70. end
  71.  
  72. function setRho(densidade1, densidade2)
  73.     global rho1;
  74.     global rho2;
  75.     rho1 = densidade1;
  76.     rho2 = densidade2;
  77. end
  78.  
  79. function setEA(modulo1, modulo2)
  80.     global EA1;
  81.     global EA2;
  82.     EA1 = modulo1;
  83.     EA2 = modulo2;
  84. end
  85.  
  86. function y = getL(indice, num_mola)
  87.     global L1;
  88.     global L2;
  89.     if indice <= (num_mola)/2
  90.         y = L1;
  91.     else
  92.         y = L2;
  93.     end
  94. end
  95.  
  96. function y = getP(indice, num_mola)
  97.     global rho1;
  98.     global rho2;
  99.     global L1;
  100.     global L2;
  101.     if indice <= (num_mola)/2
  102.         y = rho1 * L1;
  103.     else
  104.         y = rho2 * L2;
  105.     end
  106. end
  107.  
  108. function y = getK(indice,num_mola)
  109.     global EA1;
  110.     global EA2;
  111.     global L1;
  112.     global L2;
  113.     if indice <= (num_mola/2)
  114.         y = EA1/L1;
  115.     else
  116.         y = EA2/L2;
  117.     end
  118. end
  119.  
  120. function y = g(L)
  121.     global g_d;
  122.     global g_x;
  123.     global g_f;
  124.     y = g_f(g_d*L + g_x);
  125. end
  126.  
  127. function [u,iter] = univariante(x0,f)
  128.     global g_d;
  129.     global g_x;
  130.     global g_f;
  131.     g_f = f;
  132.     g_x = x0;
  133.     tol = 1e-3;
  134.     n = length(x0);
  135.  
  136.     g_d = zeros(1,n);
  137.     for k = 1:5000
  138.         xi = g_x;
  139.         for i = 1:n
  140.             g_d(i) = 1;
  141.             [a1,b1] = passoconstante(0, @g, 100, 1);
  142.             c1 = bissecao(a1, b1, 1e-6, tol/10, @g);
  143.             x1 = g_x + g_d*c1;          
  144.  
  145.             g_x = x1;
  146.             g_d(i) = 0;
  147.         end
  148.        
  149.         norm(x1 - xi)
  150.         if norm(x1 - xi) <= tol
  151.             break;
  152.         end
  153.     end
  154.    
  155.     u = x1;
  156.     iter = k;
  157. end
  158.  
  159. function y = f(uv)
  160.     global g_k;
  161.     global g_l;
  162.     tamanho_u = length(uv)/2;
  163.    
  164.     u = uv(1:tamanho_u);
  165.     v = uv(tamanho_u+1:end);
  166.     num_molas = tamanho_u + 1;
  167.  
  168.     du = u - [0, u(1:end-1)];
  169.     dv = v - [0, v(1:end-1)];
  170.     du(num_molas) = -u(num_molas-1);
  171.     dv(num_molas) = v(num_molas-1);
  172.  
  173.     U = 0.5 * sum(g_k .* ((hypot(g_l + du, dv) - g_l).^2));
  174.    
  175.     global g_w;
  176.     V = sum(g_w .* v);
  177.    
  178.     y = U - V;
  179. end
  180.  
  181. function [y,w] = passoconstante(x0,f,da,d)
  182.    y0 = f(x0);
  183.     for k = 1:5000
  184.         x1 = x0 + da*d;
  185.         y1 = f(x1);
  186.         if k == 1
  187.             if y1 > y0
  188.                 d = -d;
  189.                 continue
  190.             elseif y1 == y0
  191.                 y = min([x0 - da*d,x0 + da*d]);
  192.                 w = max([x0 - da*d,x0 + da*d]);
  193.                 break
  194.             end
  195.         else
  196.             if y1 >= y0
  197.                 y = min([x0 - da*d,x0 + da*d]);
  198.                 w = max([x0 - da*d,x0 + da*d]);
  199.                 break
  200.             end
  201.         end
  202.         x0 = x1;
  203.         y0 = y1;
  204.     end
  205. end
  206.  
  207. function y = bissecao(aL,aU,eps,tol,f)
  208.     for k = 1:5000
  209.         aM = (aL + aU)/2;
  210.         a1 = aM - eps;
  211.         a2 = aM + eps;
  212.         if abs(aL - aU) <= tol
  213.             y = aM;
  214.             break
  215.         elseif f(a2) > f(a1)
  216.             aU = aM;
  217.         elseif f(a1) > f(a2)
  218.             aL = aM;
  219.         else
  220.             y = aM;
  221.             break
  222.         end
  223.     end
  224. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement