Advertisement
luizaspan

RK4 Method (new)

Nov 11th, 2015
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define h 0.1
  5.  
  6. #define x0 1
  7. #define y0 2
  8.  
  9. #define N 50
  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. double T4(double x, double y)
  17. {
  18.     double K1 = f(x,y);
  19.     double K2 = f(x+h/2.,y+(h/2.)*K1);
  20.     double K3 = f(x+h/2.,y+(h/2.)*K2);
  21.     double K4 = f(x+h,y+h*K3);
  22.  
  23.     return (1./6)*(K1 + 2.*K2 + 2.*K3 + K4);
  24. }
  25.  
  26. int main(void)
  27. {
  28.     int i;
  29.     double x, y, yn=0.;
  30.  
  31.     x=x0;
  32.     y=y0;
  33.  
  34.     for (i=0; i<N; i++)
  35.     {
  36.         // K1 = f(x,y);
  37.         // K2 = f(x+h/2,y+(h/2)*K1);
  38.         // K3 = f(x+h/2,y+(h/2)*K2);
  39.         // K4 = f(x+h,y+h*K3);
  40.  
  41.         // yn = y + (h/6)*(K1 + 2*K2 + 2*K3 + K4);
  42.  
  43.         yn = y + h*T4(x,y);
  44.  
  45.         x+=h;
  46.  
  47.         printf("%f\t%f\n",x,yn);
  48.  
  49.         // printf("%lf\t%lf\t%lf\n",i,yn,x);
  50.     }
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement