Advertisement
FelipeNeto2

Matlab backup 3

Apr 1st, 2024 (edited)
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.60 KB | None | 0 0
  1. function test(U0, UL, phi, vel, D, x_adim, t_adim)
  2.     % Parâmetros adimensionais
  3.     L = max(x_adim);                    % Comprimento do intervalo
  4.     T = max(t_adim);                    % Tempo final
  5.     t_ref = D / (phi * L^2);            % Tempo característico
  6.     T_estrela = T / t_ref;              % Tempo final adimensional
  7.  
  8.     % Número de pontos na malha espacial
  9.     n = length(x_adim);
  10.     dx = L / (n - 1);
  11.  
  12.     % Variáveis e malhas adimensionais
  13.     dt = t_adim(2) - t_adim(1);
  14.  
  15.     B = zeros(n, 1);
  16.     % Preenchimento da matriz inicial B
  17.     B(1) = U0;
  18.     B(end) = UL;
  19.  
  20.     % Inicialização da matriz A
  21.     A = zeros(n);
  22.  
  23.     pe = (vel*L)/D;
  24.    
  25.     % Coeficientes da matriz A
  26.     A1 = dt * (-1/dx^2);
  27.     A2 = dt * (1/dt + pe/dx + 2/dx^2);
  28.     A3 = dt * (-pe/dx - 1/dx^2);
  29.  
  30.     for i = 2:n-1
  31.         A(i, i - 1) = A1;
  32.         A(i, i) = A2;
  33.         A(i, i + 1) = A3;
  34.     end
  35.  
  36.     A(1, 1) = 1;
  37.     A(end, end) = 1;
  38.  
  39.     tempos = [0.09, 0.14, 0.19, 0.32] * T_estrela;
  40.  
  41.     % Solução dos sistemas A*u = B
  42.     for j = 2:length(t_adim)
  43.         u = A \ B;  % Resolve o sistema linear
  44.  
  45.         % Verificar se o tempo atual está nos tempos desejados
  46.         if any(abs(t_adim(j) - tempos) < eps)
  47.             % Se sim, plotar a solução numérica
  48.             plot(x_adim * L, u, 'r'); % Convertendo x' de volta para x
  49.             hold on;
  50.         end
  51.  
  52.         % Atualiza o B para o próximo passo de tempo
  53.         B = u;
  54.     end
  55.  
  56.     xlabel('x');
  57.     ylabel('C');
  58.     title('Solução da Equação de Convecção-Difusão (Adimensional)');
  59.     grid on;
  60. end
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement