Advertisement
luizaspan

Método de Euler II (+ fprintf)

Oct 21st, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define dt 1e-2
  5.  
  6. #define x0 0
  7. #define t0 0
  8. #define tf 10
  9.  
  10. #define N ((tf-t0)/dt)
  11.  
  12. double dxdt(double x, double t)
  13. {
  14.     return -x*x*x + sin(t);
  15. }
  16.  
  17. int main(void)
  18. {
  19.     double x = x0, xn, t = t0, tn;
  20.     int i;
  21.  
  22.     FILE *h = fopen("./euler.dat","w+");
  23.  
  24.     for (i = 0; i < N; ++i)
  25.     {
  26.         xn  = x + dxdt(x,t)*dt;
  27.         tn = t + dt;
  28.         fprintf(h, "%d %f %f\n", i, t, x);
  29.         x = xn;
  30.         t = tn;
  31.     }
  32.  
  33.     fclose(h);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement