Advertisement
ivodevweb

Untitled

Jan 21st, 2023
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BlitzMax 1.22 KB | Software | 0 0
  1.  
  2. Aqui está um exemplo de um programa em Octave que implementa o método de Heun para integrar a equação diferencial que mencionamos anteriormente e cria um gráfico dos resultados usando a biblioteca Gnuplot:
  3.  
  4. % Constantes
  5. m = 2.6;  % Massa em g
  6. L = 1.0;  % Comprimento em m
  7. R = 0.03; % Raio em m
  8.  
  9. % Valores iniciais
  10. theta0 = 0.05;  % Ângulo inicial em rad
  11. w0 = 0.0;       % Velocidade angular inicial em rad/s
  12. t0 = 0.0;       % Tempo inicial em s
  13. h = 0.1;        % Passo de tempo em s
  14.  
  15. % Listas para armazenar os resultados
  16. t = t0;
  17. theta = theta0;
  18. w = w0;
  19.  
  20. % Loop de tempo
  21. while (t(end) < 100.0)
  22.     % Cálculo dos valores intermediários
  23.     k1x = h * w(end);
  24.     k1v = h * (-1.0 if w(end) < 0.0 else 1.0);
  25.     k2x = h * (w(end) + k1v);
  26.     k2v = h * (-1.0 if w(end) + k1v < 0.0 else 1.0);
  27.  
  28.     % Atualização dos valores de θ e w
  29.     theta = [theta theta(end) + (k1x + k2x) / 2.0];
  30.     w = [w w(end) + (k1v + k2v) / 2.0];
  31.  
  32.     % Atualização do tempo
  33.     t = [t t(end) + h];
  34. end
  35.  
  36. % Cria o gráfico usando o Gnuplot
  37. plot(t, theta, "r", "linewidth", 2);
  38. hold on
  39. plot(t, w, "b", "linewidth", 2);
  40. legend("θ", "w");
  41. title("Heun method");
  42. xlabel("t (s)");
  43. ylabel("θ (rad), w (m/s)");
  44. grid on;
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement