import java.io.*;
import java.util.*;
import javax.swing.*;

      class fxa extends f_xj
      {  
	  double xi[]; // m independent variable data set
      double yi[]; //dependent variable data set
      double a[];  //fit function coefficient set
      int nn;//number of data
  	  public fxa(String filename,double ia[])
      {
        //read the data to curvefit
        //last column is treated as independent data set
        //get the data file and initial fit coefficient when class is defined
        int n=ia.length;
        a=new double[n];
        seta(ia);
        double b[][]=Text.readDoubleT(filename);
        nn=yi.length;
        //System.out.println("m="+m);
        xi=b[0];
        yi=b[1];
        System.out.println("xi=\n"+Matrix.toString(xi));
        System.out.println("yi=\n"+Matrix.toString(yi));      
      nn=yi.length;
      a=ia;
      }
      
      public void seta(double ia[])
      {
	      //assign new fit coefficient set 
	      for(int ii=0;ii<nn;ii++)
              a[ii]=ia[ii];
	  }

	  public double[] geta()
      {
	     // return fit coefficient set
	     return a;
	  }	  

      public  double Ps(double t)
      {//ilk tanım Ps formülü	
      double T=t+273.15;
      double ln10=Math.log(10.0);
      double A[]={40.69889*ln10,-2.36254e3*ln10,-13.06883,7.616005e-3*ln10,0.2342564,3.761111e2};
      double p1=A[0]+A[1]/T+A[2]*Math.log(T)+A[3]*T+A[4]*((A[5]-T)/T)*Math.log(A[5]-T);
      return Math.exp(p1);
      }

      public double dPsdT(double t)
      {
      double T=t+273.15;
      double ln10=Math.log(10.0);
      double A[]={40.69889*ln10,-2.36254e3*ln10,-13.06883,7.616005e-3*ln10,0.2342564,3.761111e2};
      double p1=A[0]+A[1]/T+A[2]*Math.log(T)+A[3]*T+A[4]*((A[5]-T)/T)*Math.log(A[5]-T);
      p1=Math.exp(p1);
      double p2=-A[1]/(T*T)+A[2]/T+A[3]-A[4]/T-A[4]*(-A[5]/(T*T)-1)*Math.log(A[5]-T);
      p2=p2*p1;
      return p2;	
      }

      public double ortalama(double x[])
      {//ortalama değerleri hesaplar
      int N=x.length;
      double tot=0;
      for(int i=0;i<N;i++) tot+=x[i];
      return tot/N;  
      }

      public double s(double x[],int j,int m)
      {// j çevresinde +/- m/2 veri bölgesinde 
       // yerel standart sapma değerlerini hesaplar
       int N=x.length;
       int m1,m2,m3,m4;
       m1=m/2;
       m2=j-m1;
       m3=j+m1;
       if(m2<0) {m2=0;m3=m;if(m3>N){m3=N;}}
       else if(m3>N) {m3=N;m2=m3-m;if(m2<0){m2=0;}} 
       double tot=0;
       m4=m3-m2;
       double y[]=new double[m];
       for(int i=m2;i<m3;i++) {y[i-m2]=x[i];}
       double yort=ortalama(y);
       double yi=0;
       for(int i=0;i<m;i++)
       {yi=y[i]-yort;
        tot+=yi*yi;
       }
       return Math.sqrt(tot/(m-1));  
       }

       public double W(int i,int m)
       {   //weight function
	   return s(xi,i,m)+dPsdT(xi[i])*s(yi,i,m);
       }

	  double Ps(double t,double ai[])
      {
	  // t derece C 
      // sample specific function m dimensional independent variable
      // independent variables : x[0],x[1]...x[m-1]
      double T=t+273.15;
      double b0=ai[0];
      double b1=ai[1];
      double a4=ai[3];
      double a5=ai[4];
      double a6=ai[5];
      double a7=ai[6];
      double Tc=374.23;
      double Tt=169.861;
      double Pt=0.42; //kPa
      double Pc=4056;
      double A=(Tc-Tt)/Tt;
      double tt=(T-Tt)/(Tc-Tt);
      double R=8314.5e-3/102.3;
      double s=b0/R;
      double r=b1/(R*Tt)-s;
      double q=(1+A*tt);
      double P0=1.0-Pt/(Pc-Pt)*(1-q*q*q*q*q*Math.exp(A*r*tt/q));
      double tf=1-tt;
      double teta=0.2;
      double P1=2-a4*tf+a5*Math.pow(tf,(2.0-teta))+ a6*tf*tf*tf+a7*tf*tf*tf*tf;
      double N=8.0*Tt/Tc;
      double pi=Math.pow((Math.pow(P0,N)+Math.pow(P1,N)),(1.0/N));
      return pi; //kPa
      }
	        
      public double func(double ai[])
      {
      double ff=0;
      double dP;
      double yy;
      double xx[]=new double[xi.length];
      double Wi;
       for(int i=0;i<nn;i++)
	    {   
	    dP=Ps(xi[i],ai);
	    yy=yi[i];
        //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff);
	    dP-=yy; 
	    Wi=W(i,10);
        ff+=Wi*dP*dP;
        //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); 
	    }
      return ff;
	  }
}

