//======================================================
// 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 -2*x*x*x+12*x*x-20*x+8.5;}
}

class fxyKC extends f_x
{
  double func(double x)
  {return -0.5*x*x*x*x+4.0*x*x*x-10.0*x*x+8.5*x+1.0;}
}

class diferansiyelH
{  
  public static double[][] RK6(f_xy f,double x0,double y0,double xn,double h)
  {
  // Runge-Kutta 6
  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,k5,k6;
  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.25*h,a[1][i]+0.25*k1*h);
   k3=f.func(a[0][i]+0.25*h,a[1][i]+1.0/8.0*k1*h+1.0/8.0*k2*h);
   k4=f.func(a[0][i]+0.5*h,a[1][i]-0.5*k2*h+k3*h);
   k5=f.func(a[0][i]+3.0/4.0*h,a[1][i]+3.0/16.0*k1*h+9.0/16.0*k4*h);
   k6=f.func(a[0][i]+h,a[1][i]-3.0/7.0*k1*h+2.0/7.0*k2*h+12.0/7.0*k3*h-12.0/7.0*k4*h+8.0/7.0*k5*h);   
   a[1][i+1]=a[1][i]+(7.0*k1+32.0*k3+12.0*k4+32.0*k5+7*k6)/90.0*h;
   i++;
   }
  return a; 
  }
  
  
  public static void main(String args[]) throws IOException
  {double x0=0;
   double y0=1.0;
   double yn=4.0;
   double h=0.5;
   fxy f2=new fxy();
   fxyKC fKC=new fxyKC();
   double a[][]=RK6(f2,x0,y0,yn,h);
   Text.print(Text.T(a));
   Plot p1=new Plot(a);
   p1.setPlotType(0,24);
   p1.addSpline(a,10);
   p1.addFunction(fKC,0.0,4.0,200);
   p1.plot();
  }
}