Advertisement
FaisalAhemdBijoy

Euler

Sep 16th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. #define inf 10000000
  7.  
  8. double xo, yo, x,error;
  9. int step;
  10.  
  11. double f(double x, double y) {
  12.     return x+y;
  13. }
  14.  
  15. void mod_euler(double xx,double yy,double h)
  16. {
  17.  
  18.     for(int i = 0 ; i < step; i++) {
  19.         double dif = inf;
  20.         double cur = yy + h * f(xx, yy);
  21.  
  22.         int cnt = 0;
  23.         while(dif >= error) {
  24.             double pre = cur;
  25.             cur = yy + h * (f(xx, yy) + f(xx+h, cur)) / 2.0;
  26.             dif = fabs(cur - pre);
  27.             cout << "y" << ++cnt << " = " << cur << "\t\tSteps needed = " << cnt << endl;
  28.         }
  29.         cout << endl;
  30.         xx = xx+ h;
  31.         yy = cur;
  32.     }
  33.     cout << endl << endl << "Value of y = " << yy << endl;
  34.     cout << "Value of f(x, y) = " << f(xx, yy) << endl;
  35. }
  36.  
  37. int main()
  38. {
  39.     cout << "Enter x0, y0, x : ";
  40.     cin >> xo >> yo >> x;
  41.  
  42.     cout << "Enter number of steps : ";
  43.     cin >> step;
  44.  
  45.     const double h = (x-xo) / (double)step;
  46.     cout << "The value of h is : " << h << endl << endl;
  47.  
  48.     cout<<"Enter the Error:"<<endl;
  49.     cin>>error;
  50.  
  51.     double xx = xo, yy = yo;
  52.     mod_euler(xx,yy,h);
  53.  
  54.     return 0;
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement