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

abstract class f_xj
{ 
  // single function  multi independent variable
  // a single value is returned  (
  // example f=x[0]+sin(x[1])
  // func(x) returns the value of f
  abstract public double func(double x[]);
}
      class yy extends f_xj
      {  
	  double xi[]; // independent variable data set
      double yi[]; //dependent variable data set
      double a[];  //fit function coefficient set
      int nn;
  	  public yy(String filename,double ia[])
      {
        //read the data to curvefit
        //get the data file and initial fit coefficient when class is defined
	    xi=new double[500];
        yi=new double[500];
        int n=ia.length;
        a=new double[n];
        seta(ia);
        int i=-1;
        try{
	    BufferedReader fin=new BufferedReader(new FileReader(filename));
        try {
        while(fin != null)
        {
        i++;
        xi[i]=Text.readDouble(fin);
        yi[i]=Text.readDouble(fin);
        }
        } catch(EOFException e_eof) {System.out.println("end of file"); }
        }catch(IOException e_io) {System.out.println("dosya bulunamadư"); }        
      nn=i;
      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;
	  }
	  
	  double Ps(double t,double ai[])
      {
      //sample specific function 
	  // saturation pressure as a function of saturation temperature
      double T=t+273.15;
	  a=ai;
      double pi=a[0]+a[1]/T+a[2]*Math.log10(T)+a[3]*T+a[4]*((a[5]-T)/T)*Math.log10(a[5]-T);
      double pp=Math.pow(10,pi); //kPa
      //System.out.println("pp="+pp+"kPa");
      return pp;
      }      
      
      public double func(double ai[])
      {
      double ff=0;
      double w;
      double yy;
       for(int i=0;i<nn;i++)
	    {w=Ps(xi[i],ai);
	    yy=yi[i];
        //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff);
	    w-=yy; 
        ff+=w*w;
        //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); 
	    }
      return ff;
	  }
}

class zz extends yy
{     public zz(String filename,double ia[])
      {super(filename,ia);}
      public double func(double ai[])
      {
      return -super.func(ai);
	  }
}

public class NA50
{
    public static void main(String args[]) throws IOException
    {
    //String in_name=JOptionPane.showInputDialog(" enter name of the input file : ");
    String in_name="R123_Ps.txt";
    double a[];
    a=new double[6];
    a[0] =1.656333e03;
    a[1] =-2.480583e06;
    a[2] =1.792522e1;
    a[3] =-8.86838e-2;
    a[4] =4.61781e2;
    a[5] =1.666667e3;

    double da[];
    da=new double[6];
    da[0] =0.1*a[0];
    da[1] =0.1*a[1];
    da[2] =0.1*a[2];
    da[3] =0.1*a[3];
    da[4] =0.1*a[4];
    da[5] =0.1*a[5];
 
    yy f=new yy(in_name,a);
    int i=0;
    for(double t=-80;t<150;t+=1)
    {System.out.println("t="+t+"ti="+f.xi[i]+"Pi="+f.yi[i]+"P="+f.Ps(t,a));i++;};  
    System.out.println("f input = "+f.func(a));
    double p[]=NA41.nelder(f,a,da,1e-10);
    String s1=" coefficient of the curve fitting equation  : \n"+Matrix.toStringT(p)+"\n";
    String s2="Nelder-Mead nonlinear least square curve fitting : ";
    JOptionPane.showMessageDialog(null,s1,s2,JOptionPane.PLAIN_MESSAGE);
    }

}
