Advertisement
luizaspan

RK4 Method

Nov 11th, 2015
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define h 0.1 // "salto"
  5.  
  6. #define x0 1 // parâmetros iniciais
  7. #define y0 2
  8.  
  9. #define N 100 // número de iterações
  10.  
  11. double f(double x, double y) // dy/dx = f(x,y)
  12. {
  13.     return 3*x*x*y; // função a ser resolvida
  14. }
  15.  
  16. int main(void)
  17. {
  18.     int i;
  19.     double K1, K2, K3, K4, x, y, yn;
  20.  
  21.     x=x0;
  22.     y=y0;
  23.  
  24.     for (i=0; i<N; i++)
  25.     {
  26.         K1 = f(x,y);
  27.         K2 = f(x+h/2,y+(h/2)*K1);
  28.         K3 = f(x+h/2,y+(h/2)*K2);
  29.         K4 = f(x+h,y+h*K3);
  30.  
  31.         yn = y + (h/6)*(K1 + 2*K2 + 2*K3 + K4);
  32.  
  33.         x+=h;
  34.  
  35.         printf("%lf\t%lf\n",yn,x);
  36.     }
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement