//======================================================
// Numerical Analysis package in java
// example to show differential equation solution
// and differentiation (derivative) functions
// Dr. Turhan Coban
// =====================================================
// Basit diferansiyel denklemler dy/dx=f(x,y)

import java.io.*;

abstract class f_xy
{
  //single function single independent variable
  // example f=x*x
  abstract double func(double x,double y);
}

class fxy extends f_xy
{
  double func(double x,double y)
  {return y*Math.exp(-x);}
}



class diferansiyel
{
  public static double[][] euler(f_xy f,double x0,double y0,double xn,double h)
  {
  int n=(int)((xn-x0)/h);	
  System.out.println("n="+n);  
  double a[][]=new double[2][n+2];
  int i=0;
  a[0][0]=x0;
  a[1][0]=y0;
  for(double xx=x0;xx<xn;xx+=h)
   {a[0][i+1]=xx+h;
   a[1][i+1]=a[1][i]+f.func(a[0][i],a[1][i])*h;
   i++;
   }
  return a;  
  }
  
  public static void main(String args[]) throws IOException
  {
   double x0=0;
   double y0=1.0;
   fxy f1=new fxy();
   double a[][]=euler(f1,x0,y0,1.0,0.01);
   Text.print(Text.T(a));
   Plot p1=new Plot(a);
   p1.setPlotType(0,24);
   p1.addSpline(a,5);
   p1.plot();
  }
}