/* euler.c   -- solution of IVP by Euler's Method
 this program provides an approximate (numerical) solution to
 the well-posed initial value problem y' = f(t,y), a <= t <= b, (1)
 y(a) = alpha at (n+1) equally-spaced abscissa points in the interval
 [a,b].  note that y is taken as the dependent function of the
 independent variable t.

 The solution of the ODE (1) is given by the truncated single-variable
 Taylor Series expansion for y(t) as follows:

 y(t[i+1]) = y(t[i]) + h y'(t[i]) + h^2y''(xi)

 Euler's Method constructs an approximation w[i] to the true solution
 y(t[i]) at each mesh point in the interval [a,b] for each
 i = 1,2, ... , n by the recursive scheme

 w[0] = alpha
 w[i+1] = w[i] + h f(t[i],w[i])   */

#include <stdio.h>
#include <math.h>
#include "euler.h"

int main(void)
{
   double a,b,alpha,h,t,w;
   int n;
   int i;
   printf("Euler Method for solution of y' = f(t,y) on [a,b], y(a)=alpha\n");
   printf("Please enter interval limits [a,b], b > a:\n");
   scanf("%lf %lf",&a,&b);
   while (b<=a)
   {
      printf("b must be greater than a.  Please re-enter a and b: \n");
      scanf("%lf %lf",&a,&b);
   }
   printf("Please enter number of steps n to take in [a,b]:\n");
   scanf("%d",&n);
   while (n<=0)
   {
	  printf("n must be a positive integer. Please re-enter n: \n");
      scanf("%d",&n);
   }

   h = (b-a)/n;
   printf("Please enter initial condition alpha, y[%lf] = alpha:\n",a);
   scanf("%lf",&alpha);
   /*  INITIALIZATION       */
   i = 0;
   t=a;
   w = alpha;
   /* END OF INITIALIZATION */
   printf("\n\nIteration\t\tt\t\t\ty\n");
   printf("%d\t\t\t%6.6lf\t\t%6.6lf\n",i,t,w);
   for (i = 1;i<=n;i++)
   {
	   w = w + h*func(t,w);
	   t = a + i*h;
	   printf("%d\t\t\t%6.6lf\t\t%6.6lf\n",i,t,w);
   }
   return 0;
}


