RybaSG

runge

Jan 2nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double h=0.1;           //krok początkowy
  4. double w[4],k[4];
  5.  
  6. double f(double x,double y) //funkcja występująca w równaniu różniczkowym
  7. {
  8.     return 0.5*x*y;
  9. }
  10.  
  11. void licz_k(double x, double y)     //współczynniki k dla metody Runge-Kutta rzędu IV
  12. {
  13.     k[0]=h*f(x,y);
  14.     k[1]=h*f(x+h/2,y+k[0]/2);
  15.     k[2]=h*f(x+h/2,y+k[1]/2);
  16.     k[3]=h*f(x+h,y+k[2]);
  17. }
  18.  
  19. double T()              //wskaźnik poprawności kroku
  20. {
  21.     double x;
  22.     x=(k[1]-k[2])/(k[0]-k[1]);
  23.     if(x>=0) return x;
  24.     else return -x;
  25. }
  26.  
  27. int main()
  28. {
  29.     int i;
  30.     double x,y,y1;
  31.  
  32.     w[0]=w[3]=1./6;     //współczynniki w dla metody Runge-Kutta rzędu IV
  33.     w[1]=w[2]=1./3;
  34.  
  35.     x=0;
  36.     y=1;
  37.  
  38.     printf("\ny(%lf) = %lf\th = %lf",x,y,h);
  39.  
  40.     do{         //algorytm metody Runge-Kutta rzędu IV
  41.         h *= 2;
  42.         do{
  43.             h /= 2;
  44.             licz_k(x,y);
  45.             y1 = y;
  46.             for(i=0;i<4;i++)
  47.                 y1 += w[i]*k[i];
  48.         }while(T()>0.05);
  49.         y = y1;
  50.         x += h;
  51.         printf("\ny(%lf) = %lf\th = %lf\tT = %lf",x,y,h,T());
  52.     }while(x<1);
  53.  
  54.     getchar();
  55.     return 0;
  56. }
Add Comment
Please, Sign In to add comment