title: Euler method in c++
body: <p>The following code uses Euler's Method to approximate a value of y(x). My code currently accepts the endpoints a and b as user input and values for values for alpha which is the initial condition and the step size value which is h. Given my code I can now approximate a value of y, say y(8) given the initial condition y(0)=6.</p>
  
  <p>However I have a small mistake in my code and I am not quite sure how to fix it and am looking for help. Right now my code does not check to ensure that the right endpoint b is an integer multiple of the stepsize h. Due to this the final approximation may not be for f(b) but for f(c) where c is the closest integer multiple of h to b. I am looking for some help on how to fix this, Thanks!</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;iomanip&gt;
  #include&lt;cmath&gt;
  using namespace std;
  
  double dydx (double x, double y)
      {
          double f = y*(2-y)/(x+3);
          return f;
      }
  int main()
  {
      double a,b,alpha,h,z;
      cout&lt;&lt;"\nEnter the value of endpoint a: \n";
      cin&gt;&gt;a;
      cout&lt;&lt;"\nEnter the value of endpoint b: \n";
      cin&gt;&gt;b;
      cout&lt;&lt;"\nEnter the y value of the initial condition: \n";
      cin&gt;&gt;alpha;
      cout&lt;&lt;"\nEnter the stepsize, h: \n";
      cin&gt;&gt;h;
      cout&lt;&lt;"\n";
      while((b-a)&gt;0.0000001)
      {
          z=alpha+(h*dydx(a,alpha));
          cout&lt;&lt;"z("&lt;&lt;a&lt;&lt;")="&lt;&lt;z&lt;&lt;endl;
          alpha=z;
          a=a+h;
      }
      cout&lt;&lt;"\nThe approximate solution of y("&lt;&lt;b&lt;&lt;") is "&lt;&lt;z&lt;&lt;"."&lt;&lt;endl;
      return 0;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/UwyUx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UwyUx.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/sEgTR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sEgTR.png" alt="enter image description here"></a></p>