Advertisement
Guest User

Euler

a guest
Sep 17th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. double slope(double x, double y)
  5.     {
  6.         double f = y*(2-y)/(x+3);
  7.         return f;
  8.     }
  9. int main()
  10. {
  11.     double a,b,old_val,h,z;
  12.     cout<<"\nEnter the value of endpoint a: \n";
  13.     cin>>a;
  14.     cout<<"\nEnter the value of endpoint b: \n";
  15.     cin>>b;
  16.     cout<<"\nEnter the y value of the initial condition: \n";
  17.     cin>>old_val;
  18.     /*
  19.      * Obtains step size from number on steps
  20.      * h = 0.1 for [a; b] = [0; 8] can be given by n = 80
  21.     */
  22.     int n = 0;
  23.     cout<<"\nEnter the number of steps, n: \n";
  24.     cin>>n;
  25.     h = (b - a) / n;
  26.     //------
  27.     cout<<"\n";
  28.     //-- Replaced 0.0000001 by h / 2.0 --
  29.     while((b-a)> h / 2.0)
  30.     {
  31.         z=old_val+(h*slope(a,old_val));
  32.         old_val=z;
  33.         a=a+h;
  34.         /*
  35.          * z - function value in next point,
  36.          * so to output correct point a need to be incremented before this.
  37.         */
  38.         cout<<"z("<<a<<")="<<z<<endl;
  39.     }
  40.     cout<<"\nThe approximate solution of y("<<b<<") is "<<z<<"."<<endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement