//======================================================
// Numerical Analysis package in java
// example to show differential equation solution
// Runge-Kutta 4rd order solution RK4
// Dr. Turhan Coban
// =====================================================
// dy/dx=f(x,y)

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

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




class NA87
{
  
  
  public static double[][] RK4(f_xy f,double x0,double y0,double xn,double h)
  {
  // Runge-Kutta 4
  int n=(int)((xn-x0)/h);	  
  double a[][]=new double[2][n+1];
  int i=0;
  a[0][0]=x0;
  a[1][0]=y0;
  double k1,k2,k3,k4;
  for(double xx=x0;xx<xn;xx+=h)
   {a[0][i+1]=xx+h;
   k1=f.func(a[0][i],a[1][i]);
   k2=f.func(a[0][i]+0.5*h,a[1][i]+0.5*k1*h);
   k3=f.func(a[0][i]+0.5*h,a[1][i]+0.5*k2*h);
   k4=f.func(a[0][i]+h,a[1][i]+k3*h);   
   a[1][i+1]=a[1][i]+(k1+2.0*k2+2.0*k3+k4)/6.0*h;
   System.out.println("k1="+k1);
   System.out.println("k2="+k2);
   System.out.println("k3="+k3);
   System.out.println("k4="+k4);
   System.out.println("i="+i+"ai="+a[1][i+1]);
   i++;
   }
  return a;  
  }
  
  public static void main(String args[]) throws IOException
  {double x0=0;
   double xn=1.0;
   double y0=2.0;
   double h=0.01;
   fxy f2=new fxy();
   //fxyKC fKC=new fxyKC();
   double a[][]=RK4(f2,x0,y0,xn,h);
   Text.print(Text.T(a));
   Plot p1=new Plot(a);
   p1.setPlotType(0,24);
   //p1.addFunction(fKC,0.0,0.2,200);
   p1.plot();
  }

}
