public class D1_HT
{
int N;
double Cd;
double K;
double dt;
double dx;
double A0;
double Tb;
double Tin;
double T0[];
double E[];
double G[];
double F[];
double T[];
double R[];

       public D1_HT(int iN,double iCd,double iK,double idt,double idx,double iTb,double iTin)
       {
	   N=iN;
	   Cd=iCd;
	   K=iK; 
	   dt=idt;  
	   dx=idx;
	   //A0=Cd*dx/dt;
	   Tb=iTb;
	   Tin=iTin;
	   T0=new double[N];
	   E=new double[N]; 
	   G=new double[N]; 
	   F=new double[N]; 
	   T=new double[N];
	   R=new double[N];     
	   for(int i=0;i<N;i++)
	   {T0[i]=Tin;}
	   
	   
	   //System.out.println(A0);
	   }

	   
	    /*public double[][] equ
	      {
		      
		  double out=new double[4][N];    
		  for(int i=0;i<N;i++)
		  {
		   out[0][i]=E[i];
		   out[1][i]=F[i];
		   out[2][i]=G[i];
		   out[3][i]=H[i];	  
		   	  
		  }
		  return out    
		      
		  }*/
	   
	   
	   public void equ()
	   {
		double   A0=Cd*dx/dt;
		  for(int i=0;i<N;i++)
		  {
			   //(G-EAST) --(F-POINT)--(E-WEST)-(RR--R)
		    if(i==0)
		    {
			E[i]=0.0;
			G[i]=-K/dx;    
			F[i]=-G[i]+A0;
	        R[i]=A0*T0[i];    
			}	  
			else if((i>0)&&(i<N-1))
			{
			G[i]=-K/dx;	
			E[i]=-K/dx;
			F[i]=-G[i]-E[i]+A0;
			R[i]=A0*T0[i];
		    }  
			else if(i==N-1)
			{
			E[i]=-K/dx;
			G[i]=0.0;	
			F[i]=-E[i]+A0+(2*K/dx);	
			R[i]=A0*T0[i];	
		    }  
		    //System.out.println ("node="+i+"   "+"WEST="+E[i]);
		    //System.out.println ("node="+i+"   "+"EAST="+G[i]);
		    //System.out.println ("node="+i+"   "+"POINT="+F[i]);
		    //System.out.println ("node="+i+"   "+"CONST="+R[i]);
		    
		  }
		  
	  }
	  
	  // public double TDMA(double T[],String shth ,int g,double ff,)
	  
	  public void TDMA()
	  //(G-EAST) --(F-POINT)--(E-WEST)-(RR--R)
	  {
		  for(int i=1;i < N;i++)
	      {   E[i]=E[i]/F[i-1];//   E[i]/=F[i-1];
	         F[i]=F[i]-E[i]*G[i-1];}
	         
	       for(int i=1;i<N;i++)
           {R[i]=R[i]-E[i]*R[i-1];}
            //R[i]-=E[i]*R[i-1]
     
            T[N-1]=R[N-1]/F[N-1];  // ---> old version N
            for(int i=(N-2);i>=0;i--)
            {T[i]=(R[i]-G[i]*T[i+1])/F[i];}
      }
      
	  public String toString(int time)
	  {
		String s="time="+time+"   "+"T(1)="+T[1]+"\n";
		s+=time;
		s+="temp=\n"+Matrix.toStringT(T);
        return s; 
	  }
      
      public void solution()
      {
	      int timemax=41;
	      for(int time=1; time < timemax;time++)
	      {
		     equ(); 
		     TDMA(); 
		     System.out.println(toString(time));
		     for(int i=0;i<N;i++)
		     {T0[i]=T[i];} 
		  }      
	  } 
	   
	   public static void main(String args[])
	   {
		   int N=5;
		   double Cd=1e7;
		   double K=10.0;
		   double dt=2.0;
		   double dx=0.004;
		   double Tb=0.0;
		   double Tin=200.0;
		   D1_HT d1=new D1_HT(N,Cd,K,dt,dx,Tb,Tin);
		   d1.solution();
		   
	   }

}