Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- #define inf 10000000
- double xo, yo, x,error;
- int step;
- double f(double x, double y) {
- return x+y;
- }
- void mod_euler(double xx,double yy,double h)
- {
- for(int i = 0 ; i < step; i++) {
- double dif = inf;
- double cur = yy + h * f(xx, yy);
- int cnt = 0;
- while(dif >= error) {
- double pre = cur;
- cur = yy + h * (f(xx, yy) + f(xx+h, cur)) / 2.0;
- dif = fabs(cur - pre);
- cout << "y" << ++cnt << " = " << cur << "\t\tSteps needed = " << cnt << endl;
- }
- cout << endl;
- xx = xx+ h;
- yy = cur;
- }
- cout << endl << endl << "Value of y = " << yy << endl;
- cout << "Value of f(x, y) = " << f(xx, yy) << endl;
- }
- int main()
- {
- cout << "Enter x0, y0, x : ";
- cin >> xo >> yo >> x;
- cout << "Enter number of steps : ";
- cin >> step;
- const double h = (x-xo) / (double)step;
- cout << "The value of h is : " << h << endl << endl;
- cout<<"Enter the Error:"<<endl;
- cin>>error;
- double xx = xo, yy = yo;
- mod_euler(xx,yy,h);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement