Advertisement
rakoczyn

MN_RR

Jan 12th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. //#define _USE_MATH_DEFINES
  3. //#include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. double h = 0.0;
  8. int N = 0;
  9.  
  10.  
  11.  
  12. double f(double x, double y)
  13. {
  14.     return x+y;
  15. }
  16.  
  17. double k1(double h, double x, double y)
  18. {
  19.     return h*f(x,y);
  20. }
  21.  
  22. double k2(double h, double x, double y)
  23. {
  24.     return h*f(x + 0.5 * h, y + 0.5 * k1(h,x,y));
  25. }
  26.  
  27. double k3(double h, double x, double y)
  28. {
  29.     return h*f(x + 0.5 * h, y + 0.5 * k2(h,x,y));
  30. }
  31.  
  32. double k4(double h, double x, double y)
  33. {
  34.     return h*f(x + h, y + k3(h,x,y));
  35. }
  36.  
  37. double yn(double h, double x, double y)
  38. {
  39.     return y + (1/6.0) * (k1(h,x,y) + 2* k2(h,x,y) + 2*k3(h,x,y) + k4(h,x,y));
  40. }
  41.  
  42.  
  43.  
  44. int main ()
  45. {
  46.     double x0 = 0.0;
  47.     double y0 = 0.0;
  48.  
  49.     //cout << "Podaj x0 = "; cin >> x0;
  50.     cout << "Podaj y0 = "; cin >> y0;
  51.     cout << "Podaj N = "; cin >> N;
  52.  
  53.     double x = x0;
  54.     double y = y0;
  55.  
  56.     h = (y0-x0) / (double)N;
  57.  
  58.     for(int i = 0; i < N; i++)
  59.     {
  60.         y = yn(h,x,y);
  61.         x += h;
  62.  
  63.     }
  64.  
  65.     cout << y;
  66.    
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement