fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. double f(double x, double y) {
  4. return x + y;
  5. }
  6. double rk(double x0, double y0, double h, double x_end) {
  7. double x = x0;
  8. double y = y0;
  9. while (x < x_end) {
  10. double k1 = h * f(x, y);
  11. double k2 = h * f(x + h / 2, y + k1 / 2);
  12. double k3 = h * f(x + h / 2, y + k2 / 2);
  13. double k4 = h * f(x + h, y + k3);
  14.  
  15. y = y + (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);
  16. x = x + h;
  17. }
  18. return y;
  19. }
  20.  
  21. int main() {
  22. double x0 = 0.0;
  23. double y0 = 1.0;
  24. double h = 0.1;
  25. double x_end1 = 0.1;
  26. double x_end2 = 0.2;
  27. double result1 = rk(x0, y0, h, x_end1);
  28. double result2 = rk(x0, y0, h, x_end2);
  29. cout << "y(0.1) is: " << result1 << endl;
  30. cout << "y(0.2) is: " << result2 << endl;
  31. return 0;
  32. }
  33.  
  34. //C213115
  35. //Nazmul Hasan Naeem
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
y(0.1) is: 1.11034
y(0.2) is: 1.24281