Advertisement
ivodevweb

Untitled

Jan 21st, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | Software | 0 0
  1. Aqui está um exemplo de um programa em PHP que implementa o método de Heun para integrar a equação diferencial que você mencionamos anteriormente e cria um gráfico dos resultados usando a biblioteca GD:
  2.  
  3. <?php
  4.  
  5. // Constantes
  6. $m = 2.6;  // Massa em g
  7. $L = 1.0;  // Comprimento em m
  8. $R = 0.03; // Raio em m
  9.  
  10. // Valores iniciais
  11. $theta0 = 0.05;  // Ângulo inicial em rad
  12. $w0 = 0.0;       // Velocidade angular inicial em rad/s
  13. $t0 = 0.0;       // Tempo inicial em s
  14. $h = 0.1;        // Passo de tempo em s
  15.  
  16. // Arrays para armazenar os resultados
  17. $t = [$t0];
  18. $theta = [$theta0];
  19. $w = [$w0];
  20.  
  21. // Loop de tempo
  22. while ($t[count($t) - 1] < 100.0) {
  23.     // Cálculo dos valores intermediários
  24.     $k1x = $h * $w[count($w) - 1];
  25.     $k1v = $h * ($w[count($w) - 1] < 0.0 ? -1.0 : 1.0);
  26.     $k2x = $h * ($w[count($w) - 1] + $k1v);
  27.     $k2v = $h * (($w[count($w) - 1] + $k1v) < 0.0 ? -1.0 : 1.0);
  28.  
  29.     // Atualização dos valores de θ e w
  30.     array_push($theta, $theta[count($theta) - 1] + ($k1x + $k2x) / 2.0);
  31.     array_push($w, $w[count($w) - 1] + ($k1v + $k2v) / 2.0);
  32.  
  33.     // Atualização do tempo
  34.     array_push($t, $t[count($t) - 1] + $h);
  35. }
  36.  
  37. // Cria o gráfico usando a GD
  38. $width = 800;
  39. $height = 600;
  40. $image = imagecreate($width, $height);
  41. $background = imagecolorallocate($image, 255, 255, 255);
  42. $color1 = imagecolorallocate($image, 255, 0, 0);
  43. $color2 = imagecolorallocate($image, 0, 0, 255);
  44.  
  45. // Desenha os eixos
  46. imageline($image, 50, $height - 50, $width - 50, $height - 50, 0);
  47. imageline($image, 50, $height - 50, 50, 50, 0);
  48.  
  49. // Desenha os gráficos
  50. for ($i = 0; $i < count($t) - 1; $i++) {
  51.     $x1 = 50 + ($t[$i] - $t0) / (100.0 - $t0)
  52. ($width - 100);
  53. $y1 = $height - 50 - $theta[$i] / 0.2 * ($height - 100);
  54. $x2 = 50 + ($t[$i + 1] - $t0) / (100.0 - $t0) * ($width - 100);
  55. $y2 = $height - 50 - $theta[$i + 1] / 0.2 * ($height - 100);
  56. imageline($image, $x1, $y1, $x2, $y2, $color1);
  57. }
  58. for ($i = 0; $i < count($t) - 1; $i++) {
  59. $x1 = 50 + ($t[$i] - $t0) / (100.0 - $t0) * ($width - 100);
  60. $y1 = $height - 50 - $w[$i] / 20.0 * ($height - 100);
  61. $x2 = 50 + ($t[$i + 1] - $t0) / (100.0 - $t0) * ($width - 100);
  62. $y2 = $height - 50 - $w[$i + 1] / 20.0 * ($height - 100);
  63. imageline($image, $x1, $y1, $x2, $y2, $color2);
  64. }
  65. // Salva o gráfico em um arquivo
  66. imagepng($image, "plot.png");
  67.  
  68. // Limpa a memória
  69. imagedestroy($image);
  70.  
  71. ?>
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement