Advertisement
luizaspan

Método de Euler VI (variando dt)

Oct 21st, 2015
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define x0 2
  5.  
  6. #define v0 1
  7.  
  8. #define t0 0
  9. #define tf 2.5
  10.  
  11. #define dt0 0.01
  12. #define dtf 1e-6
  13. #define ddt -1e-4
  14.  
  15. #define w02 3
  16. #define Ndt ((dtf-dt0)/ddt)
  17.  
  18. double dvdt(double x, double v, double t)
  19. {
  20.     return -w02*x;
  21. }
  22.  
  23. double E(double x, double v)
  24. {
  25.     return v*v/w02 + x*x;
  26. }
  27.  
  28. int main(void)
  29. {
  30.     double x, xn,
  31.            t, tn,
  32.            v, vn,
  33.            dt;
  34.  
  35.     FILE *h = fopen("./eulerdt.dat","w+");
  36.  
  37.     for(int k = 0; k < Ndt; k++)
  38.     {
  39.         dt = dt0 + k * ddt;
  40.  
  41.         x = x0; t = t0; v = v0;
  42.  
  43.         double E0 = E(x,v);
  44.  
  45.         for (int i = 0; i < ((tf-t0)/dt); ++i)
  46.         {
  47.             vn = v + dvdt(x,v,t)*dt;
  48.             xn = x + vn*dt;
  49.             tn = t + dt;
  50.    
  51.             x = xn;
  52.             v = vn;
  53.             t = tn;
  54.         }
  55.         fprintf(h, "%f %f\n", dt, E(x,v)/E0-1);
  56.     }
  57.  
  58.     fclose(h);
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement