Advertisement
luizaspan

Método de Euler IV (+ energia)

Oct 21st, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 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 100
  10.  
  11. #define w02 3
  12.  
  13. #define dt 1e-2
  14. #define N ((tf-t0)/dt)
  15.  
  16. double dvdt(double x, double v, double t)
  17. {
  18.     return -w02*x;
  19. }
  20.  
  21. double E(double x, double v)
  22. {
  23.     return v*v/w02 + x*x;
  24. }
  25.  
  26. int main(void)
  27. {
  28.     double x = x0, xn,
  29.            t = t0, tn,
  30.            v = v0, vn;
  31.  
  32.     FILE *h = fopen("./euler.dat","w+");
  33.  
  34.     double E0 = E(x,v);
  35.  
  36.     for (int i = 0; i < N; ++i)
  37.     {
  38.         xn = x + v*dt;
  39.         vn = v + dvdt(x,v,t)*dt;
  40.         tn = t + dt;
  41.  
  42.         fprintf(h, "%d %f %f %f\n", i, t, x, E(x,v)/E0-1);
  43.  
  44.         x = xn;
  45.         v = vn;
  46.         t = tn;
  47.     }
  48.  
  49.     fclose(h);
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement