//======================================================
// Numerical Analysis package in java
// example to show differential equation solution
// and differentiation (derivative) functions
// Dr. Turhan Coban
// =====================================================
import java.io.*;


class fm1 extends f_xi
{
double y[];
 public fm1()
 {y=new double[4];}	
//multivariable function
 double func(double x[],int x_ref)
 {
   double p=1.0;
   double R=82.05;
   double T=1500.0; //K
   double xfi=1/24.8;
   double n=p/(R*T);
   double nfi=xfi*n;
   y[0]=x[1];
   double nCO2f=3*nfi;
   double nO2=5*x[1];
   y[1]=nO2;
   double nH2O=5.0*(nfi-x[1]);
   y[2]=nH2O;
   double nCO2=3*(nfi-x[1]);
   y[3]=nCO2;
   double rf=-8.6e11*Math.exp(-30000/(1.987*T))*Math.pow(x[1],0.1)*Math.pow(nO2,1.65);
   if(x_ref==0)  return rf; 
   else return 0;
 }
}


public class diferansiyel3_1
{

public static double[][] RK6(f_xi fp,double x0,double xn,double f0[],int N)
{
//6th order Runge Kutta Method
//fp  : given set of derivative functions dfj/dxi(fj,x)
// xo : initial value of the independent variable
// xn : final value of the independent variable
// f0 : initial value of the dependent variable
// N  : number of dependent variable to be calculated
// fi : dependent variable
double h=(xn-x0)/N;
int M=f0.length;
double fi[][];
fi=new double[M][N+1];
double xi[]=new double[M+1];
double k[]=new double[6];
int i,j;
double x;
for(j=0;j<M;j++)
  {
  fi[j][0]=f0[j];
  xi[j+1]=f0[j];
  }
for(x=x0,i=0;i<N;x+=h,i++)
  {
  for(j=1;j<=M;j++)
    {
    xi[0]=x;
    xi[j]=fi[j-1][i];
    k[0]=h*fp.func(xi,j-1);
    xi[0]=x+h/2.0;
    xi[j]=fi[j-1][i]+k[0]/2;
    k[1]=h*fp.func(xi,j-1);
    xi[0]=x+h/2.0;
    xi[j]=fi[j-1][i]+k[0]/4.0+k[1]/4.0;
    k[2]=h*fp.func(xi,j-1);
    xi[0]=x+h;
    xi[j]=fi[j-1][i]-k[1]+2.0*k[2];
    k[3]=h*fp.func(xi,j-1);
    xi[0]=x+2.0/3.0*h;
    xi[j]=fi[j-1][i]+7.0/27.0*k[0]+10.0/27.0*k[1]+1.0/27.0*k[3];
    k[4]=h*fp.func(xi,j-1);
    xi[0]=x+1.0/5.0*h;
    xi[j]=fi[j-1][i]+28.0/625.0*k[0]-1.0/5.0*k[1]+546.0/625.0*k[2]+54.0/625.0*k[3]-378/625.0*k[4];
    k[5]=h*fp.func(xi,j-1);
    fi[j-1][i+1]=fi[j-1][i]+k[0]/24.0+5.0*k[3]/48.0+27.0*k[4]/56.0+125.0*k[5]/336.0;
    xi[j]=fi[j-1][i];
    }
  }
double a[][]=new double[M+1][N+1];
for(x=x0,i=0;i<=N;x+=h,i++)
{
  a[0][i]=x;
  for(j=1;j<=M;j++)
  {a[j][i]=fi[j-1][i];}
}
return a;
}

public static void main(String args[]) throws IOException
  {

  fm1 b3=new fm1();
  double y[];
  y=new double[1];
  double p=1.0;
  double R=82.05;
  double T=1500.0; //K
  double n=p/(R*T);
  double xfi=1/24.8;
  double nfi=xfi*n;
  double nCO2f=3*nfi;
  y[0]=nfi;
  double a[][]=RK6(b3,0.0,2.0e-3,y,200);
  //for(int i=0;i<a[0].length;i++) a[1][i]=a[1][i]/nfi;
  Text.print(Text.T(a));  
  double x[]=a[0];
  double y1[]=a[1];
  double y2[]=new double[y1.length];
  for(int i=0;i<y2.length;i++) { y1[i]=a[1][i];y2[i]=3.0*(nfi-a[1][i])/nCO2f;}
  Plot pp=new Plot(x,y1);
  //pp.addData(x,y2);
  pp.plot();
 }
}
