Advertisement
luizaspan

Algoritmo de Velocidade de Verlet (osc. forçado amortecido)

Oct 23rd, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define x0 2.0
  5.  
  6. #define v0 1.0
  7.  
  8. #define t0 0.0
  9. #define tf 100.0
  10.  
  11. #define w02 3.0
  12. #define w0 (sqrt(w02))
  13. #define xi 1.1
  14. #define F0m 0
  15. #define omega w0*sqrt(1-0.2)
  16.  
  17. #define dt 1e-2
  18. #define N ((tf-t0)/dt)
  19.  
  20. double phi(double x, double t, double v)
  21. {
  22.     return -w02*x + F0m*sin(omega*t) - 2*xi*v*w0;
  23. }
  24.  
  25. double E(double x, double v)
  26. {
  27.     return v*v/w02 + x*x;
  28. }
  29.  
  30. int main(void)
  31. {
  32.     double x = x0, xn,
  33.            t = t0, tn,
  34.            v = v0, vn;
  35.  
  36.     FILE *h = fopen("./diferencial.dat","w+");
  37.  
  38.     double E0 = E(x,v);
  39.  
  40.     for (int i = 0; i < N; ++i)
  41.     {
  42.         tn = t + dt;
  43.         xn = x + vn*dt + 0.5*phi(x,t,v)*dt*dt;
  44.         vn = v + 0.5*(phi(xn,tn,v)+phi(x,t,v))*dt;
  45.  
  46.         fprintf(h, "%d %f %f %f\n", i, t, x, E(x,v)/E0-1);
  47.  
  48.         t = tn;
  49.         x = xn;
  50.         v = vn;
  51.     }
  52.  
  53.     fclose(h);
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement