Advertisement
luizaspan

Método de Euler III (osc. harmônico)

Oct 21st, 2015
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 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. int main(void)
  22. {
  23.     double x = x0, xn,
  24.            t = t0, tn,
  25.            v = v0, vn;
  26.     int i;
  27.  
  28.     FILE *h = fopen("./euler.dat","w+");
  29.  
  30.     for (i = 0; i < N; ++i)
  31.     {
  32.         xn  = x + v*dt;
  33.         vn = v + dvdt(x,v,t)*dt;
  34.         tn = t + dt;
  35.  
  36.         fprintf(h, "%d %f %f\n", i, t, x);
  37.  
  38.         x = xn;
  39.         v = vn;
  40.         t = tn;
  41.     }
  42.  
  43.     fclose(h);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement