//======================================================
// 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 x*y;}
}




class diferansiyelA
{
	
  public static double[][] heun(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+1];
  int i=0;
  a[0][0]=x0;
  a[1][0]=y0;
  double yi0=0;
  for(double xx=x0;xx<xn;xx+=h)
   {a[0][i+1]=xx+h;  
    yi0=a[1][i]+f.func(a[0][i],a[1][i])*h;
    for(int j=0;j<5;j++)
    {yi0=a[1][i]+(f.func(a[0][i],a[1][i])+f.func(a[0][i+1],yi0) )/2.0; }
     a[1][i+1]=a[1][i]+(f.func(a[0][i],a[1][i])+f.func(a[0][i+1],yi0) )/2.0;
   i++;
   }
  return a;  
  }
    
  public static void main(String args[]) throws IOException
  {
   double x0=0;
   double y0=1.0;
   fxy f1=new fxy();
   double a[][]=heun(f1,x0,y0,1.0,0.001);
   Text.print(Text.T(a));
   Plot p1=new Plot(a);
   p1.setPlotType(0,24);

   p1.plot();
  }
}
