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

public class Psat
{
double ti[],pi[];

public Psat(double tii[],double pii[])
{int n=tii.length;
ti=new double[n];
pi=new double[n];
for(int i=0;i<n;i++) {ti[i]=tii[i];pi[i]=pii[i];}
}

public Psat(String s)
{
 double a[][]=Text.readDoubleT("datafile.dat");
 double ti[]=a[0];
 double pi[]=a[1];
}

 
public  double Ps(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);
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)
{return s(pi,i,m)+dPsdT(ti[i])*s(ti,i,m);}

//public static double[] W(double x[])
//{}

public static void main(String arg[]) throws IOException
{   //String s1=JOptionPane.showInputDialog("dosya adı : R134a_Ps.txt");	  
	double c[][]=Text.readDoubleT("R134a_Ps.txt");
    double xi[]=c[0];
    double yi[]=c[1];
	Psat pp=new Psat(xi,yi);
	for(int i=0;i<xi.length;i++)
	  System.out.println(pp.s(xi,i,10)+"  "+pp.s(yi,i,10)+" "+pp.W(i,10));
	//for(double t=-103.3;t<=101.06;t+=0.1)
  //System.out.println(t+" "+pp.Ps(t)+" "+pp.dPsdT(t));
 //System.out.println(101.06+" "+pp.Ps(101.06)+" "+pp.dPsdT(101.06)); 
}

}