fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f(double t, double x) {
  7. return x + exp(t) + t*x;
  8. }
  9.  
  10. int main() {
  11. double x = 2.0;
  12. double t = 1.0;
  13. double h = 0.01;
  14. double x1, x2, x3;
  15.  
  16. // Taylor series method of order 5
  17. for (int i = 1; i <= 200; i++) {
  18. x1 = x + h*f(t, x) + (h*h/2.0)*((t*t*x) + (t*x) + x + exp(t));
  19. x2 = x + h*f(t+h/2.0, x+h*f(t,x)/2.0) + (pow(h, 2)/4.0)*(2*t*x + 2*x*exp(t+h/2.0) + pow(t, 2)*x + t + exp(t+h/2.0));
  20. x3 = x + h*f(t+h/2.0, x2/2.0 + x/2.0) + (pow(h, 2)/4.0)*(2*t*x2 + 2*x2*exp(t+h/2.0) + pow(t, 2)*x2 + t + exp(t+h/2.0));
  21. x = x + (h/6.0)*(f(t,x) + 2*f(t+h/2.0, x+h*f(t,x)/2.0) + 2*f(t+h/2.0, x2/2.0 + x/2.0) + f(t+h,x3));
  22. t += h;
  23. }
  24. cout << "The value of x at t=3 using Taylor series method of order 5 is: " << x << endl;
  25.  
  26. // One-step Taylor series method at x=3
  27. double x_one_step = x + h*f(t,x);
  28. cout << "The value of x at t=3 using one-step Taylor series method is: " << x_one_step << endl;
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5424KB
stdin
Standard input is empty
stdout
The value of x at t=3 using Taylor series method of order 5 is: 1521.82
The value of x at t=3 using one-step Taylor series method is: 1582.89