// Temperature distribution in finite slab
// Heat Conduction : Necati Özışık
// Table 2.2 , Page 48
// Initial temp is constant(To) in a slab (Assumption) 
import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;


class case1 // Eigenvalues are positive roots 
{
   public double func(double x,double h11,double k11,double h22,double k22,double LL)
   {
	double rad=3.1415/180;   
    double H11=h11/k11;	   
	double H22=h22/k22;   
    return Math.tan(x*LL)-((x*(H11+H22))/(x*x-H11*H22));	   
   }	
}

class case2
{
	public double func2(double x,double h11,double k11,double LL)
	{
		double rad=3.1415/180;   
	double H11=h11/k11;
	return x*Math.tan(x*LL)-H11;	
	}
}

class case3
{
    public double func3(double x,double h11,double k11,double LL)
    {
	double rad=3.1415/180;   
	double H11=h11/k11;
	return x*(1/Math.tan(x*LL))+H11;    
	}	
}

class case4
{
	public double func4(double x,double h22,double k22,double LL)
	{
    double rad=3.1415/180;   
	double H22=h22/k22;
	return x*Math.tan(x*LL)-H22;	
	}
}

class case7
{
    public double func7(double x,double h22,double k22,double LL)
    {
	double H22=h22/k22;
	return x*(1/(Math.tan(x*LL)))+H22;    
	}	
}


public class temp_dist_in_slab
{
double h1;
double k1;
double h2;
double k2;
double H1;
double H2;
double L;
case1 cc1;
case2 cc2;
case3 cc3;
case4 cc4;
//case5 cc5;
case7 cc7;
double rad;
double kwall;
double rowall;
double Cpwall;
double To;// initial temp of the wall
double alfa; // thermal diff

       public temp_dist_in_slab(double h11,double k11,double h22,double k22,double LL,double To1,double kwall1,double rowall1,double Cpwall1)
       {
	   h1=h11;
	   k1=k11;
	   h2=h22;
	   k2=k22;
	   H1=h11/k11;
	   H2=h22/k22;    
	   L=LL;  
	   rad=3.1415/180.0;
	   cc1=new case1();
	   cc2=new case2();
	   cc3=new case3();
	   cc4=new case4();
	   //cc5=new case5();
	   cc7=new case7();
	   To=To1;
	   kwall=kwall1;
	   rowall=rowall1;
	   Cpwall=Cpwall1;
	   alfa=kwall/(rowall*Cpwall);
	   }


public  double secant(case1 c1,double x)
{
	
int nmax=100;
double tolerance=1.0e-10;
double fx,dfx;
double dx=0.01;
for(int i=0;i<nmax;i++)
{
  fx=c1.func(x,h1,k1,h2,k2,L);
  dfx=(c1.func(x+dx,h1,k1,h2,k2,L)-c1.func(x-dx,h1,k1,h2,k2,L))/(2.0*dx);
  x-=fx/dfx;
  if(Math.abs(fx)<tolerance) { return x;}
}
JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+
   " çözüm geçerli olmıyabilir","MAXİMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
return x;
}
     public double[] Bm1()
     {
	    double xx[]=new double[25]; 
	    int i=0;
	    for(double xinit=(Math.PI/2.0)-0.05;xinit<25*(Math.PI/2.0);xinit=xinit+Math.PI)
	    {
	    xx[i]=secant(cc1,xinit);
        //System.out.println("bm = "+xx[i]);
	    i++;
	    }
	   // System.out.println();
	 return xx;   
	 }

     public double[] XBmx1(double x)
     {
	 double[] bm1=Bm1();
	 double[] xx=new double[25];
	 for(int j=0;j<25;j++)
	 {
	  xx[j]=bm1[j]*Math.cos(rad*bm1[j]*x)+H1*Math.sin(rad*bm1[j]*x);
	  //System.out.println("XBMX1 = "+xx[j]);
	 }    
	// System.out.println();
	 return xx;    
	 }	 
	 
	 public double[] NBm()
	 {
	   double bm1[]=Bm1();
	   double[] xx=new double[25];
	   for(int j=0;j<25;j++)
	   {
	    xx[j]=(((bm1[j]*bm1[j]+H1*H1)*(L+(H2/(bm1[j]*bm1[j]+H2*H2))))+H1);
	  //  System.out.println("Nbm = "+xx[j]);
	   }
	  // System.out.println();
	 return xx;
	} 
	 
	 public double[] int1()
	 {
	   double bm1[]=Bm1();
	   double[] xx=new double[25];
	   for(int j=0;j<25;j++)
	   {
		xx[j]=(Math.sin(rad*bm1[j]*L))-(H1*Math.cos(rad*bm1[j]*L)/bm1[j])+(H1/bm1[j]);   
	   // System.out.println("int = "+xx[j]);
	   } 
	   //System.out.println();
		return xx; 
     }
	 
     public double Txt_1(double x,double t)
     {
	  double[] bm=Bm1();   
	  double[] nbm=NBm();
	  double[] xbm=XBmx1(x);
	  double[] integ=int1();
	  double sum1=0.0;
	  double sum=0.0;
	  for(int i=0;i<25;i++)
	  {
		sum1+=(Math.exp(-alfa*bm[i]*bm[i]*t)/(nbm[i]))*(xbm[i])*(integ[i]);  
	    sum+=Math.exp(-alfa*bm[i]*bm[i]*t)  /   (((bm[i]*bm[i]+H1*H1)*(L+((H2)/(bm[i]*bm[i]+H2*H2))))+(H1))* (   bm[i]*Math.cos(rad*bm[i]*x) + H1*Math.sin(rad*bm[i]*x)) * (        Math.sin(rad*bm[i]*L)-(H1*Math.cos(rad*bm[i]*L)/bm[i])+(H1/bm[i])    );                    
	  }
	  return (To/2)*(sum1);   
     }
     public void printcase1()
	 {
	 double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i=0;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=dx)	 
	    {block[tt][i]=Txt_1(x,tt);i++;}	 
	 }
	 System.out.println(Matrix.toString(block)); 	 
     }
     
//////////////////////
/// case 2 
//////////////////////

public  double secant(case2 c2,double x)
{
	
int nmax=100;
double tolerance=1.0e-10;
double fx,dfx;
double dx=0.01;
for(int i=0;i<nmax;i++)
{
  fx=c2.func2(x,h1,k1,L);
  dfx=(c2.func2(x+dx,h1,k1,L)-c2.func2(x-dx,h1,k1,L))/(2.0*dx);
  x-=fx/dfx;
  if(Math.abs(fx)<tolerance) { return x;}
}
JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+
   " çözüm geçerli olmıyabilir","MAXİMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
return x;
}     
  
     public double[] Bm2()
     {
	    double xx[]=new double[25]; 
	    int i=0;
	    for(double xinit=Math.PI-0.05;xinit<25*Math.PI;xinit=xinit+Math.PI)
	    {
	    xx[i]=secant(cc2,xinit);
        //System.out.println("bm = "+xx[i]);
	    i++;
	    }
	    //System.out.println();
	 return xx;   
	 }   
     public double Txt_2(double x,double t)
     {
	  double[] bm2=Bm2();
	  double sum=0.0;
	  for(int i=0;i<25;i++)
	  {sum+=Math.exp(-alfa*bm2[i]*bm2[i]*t)*((bm2[i]*bm2[i]+H1*H1)/(L*(bm2[i]*bm2[i]+H1*H1)+H1))*(Math.cos(rad*bm2[i]*(L-x)))*(Math.sin(rad*bm2[i]*L)/(bm2[i]));                         }   
	  return 2*To*sum;   
	     
	 }
     
	 public void printcase2()
	 {
	 double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i=0;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=0.1)	 
	    {block[tt][i]=Txt_2(x,tt);i++;}	 
	    
     }
	 System.out.println(Matrix.toString(block)); 	 
     }
////////////////////
//// case 3 ////////
////////////////////

public  double secant(case3 c3,double x)
{
	
int nmax=100;
double tolerance=1.0e-10;
double fx,dfx;
double dx=0.01;
for(int i=0;i<nmax;i++)
{
  fx=c3.func3(x,h1,k1,L);
  dfx=(c3.func3(x+dx,h1,k1,L)-c3.func3(x-dx,h1,k1,L))/(2.0*dx);
  x-=fx/dfx;
  if(Math.abs(fx)<tolerance) { return x;}
}
JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+
   " çözüm geçerli olmıyabilir","MAXİMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
return x;
}

      public double[] Bm3()
     {
	    double xx[]=new double[25]; 
	    int i=0;
	    for(double xinit=Math.PI-0.05;xinit<25*Math.PI;xinit=xinit+Math.PI)
	    {
	    xx[i]=secant(cc3,xinit);
        //System.out.println("bm = "+xx[i]);
	    i++;
	    }
	    //System.out.println();
	 return xx;   
	 }

	 public double Txt_3(double x,double t)
     {
	  double[] bm3=Bm3();
	  double sum=0.0;
	  for(int i=0;i<25;i++)
	  {sum+=Math.exp(-alfa*bm3[i]*bm3[i]*t)*((bm3[i]*bm3[i]+H1*H1)/(L*(bm3[i]*bm3[i]+H1*H1)+H1))*(Math.sin(rad*bm3[i]*(L-x)))*((1/bm3[i])-(Math.cos(rad*bm3[i]*L)/(bm3[i])));                         }   
	  return 2*To*sum;   
	     
	 }

	 public void printcase3()
	 {
	 double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i=0;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=0.1)	 
	    {block[tt][i]=Txt_3(x,tt);i++;}	 
	    
     }
	 System.out.println(Matrix.toString(block)); 	 
     }	 

//////////////////////////////
//// case 4 /////////////////
////////////////////////////
public  double secant(case4 c4,double x)
{
	
int nmax=100;
double tolerance=1.0e-10;
double fx,dfx;
double dx=0.01;
for(int i=0;i<nmax;i++)
{
  fx=c4.func4(x,h2,k2,L);
  dfx=(c4.func4(x+dx,h2,k2,L)-c4.func4(x-dx,h2,k2,L))/(2.0*dx);
  x-=fx/dfx;
  if(Math.abs(fx)<tolerance) { return x;}
}
JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+
   " çözüm geçerli olmıyabilir","MAXİMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
return x;
}   

     public double[] Bm4()
     {
	    double xx[]=new double[25]; 
	    int i=0;
	    for(double xinit=Math.PI-0.05;xinit<25*Math.PI;xinit=xinit+Math.PI)
	    {
	    xx[i]=secant(cc4,xinit);
        //System.out.println("bm = "+xx[i]);
	    i++;
	    }
	    //System.out.println();
	 return xx;   
	 }
     
	 public double Txt_4(double x,double t)
     {
	  double[] bm4=Bm4();
	  double sum=0.0;
	  for(int i=0;i<25;i++)
	  {sum+=Math.exp(-alfa*bm4[i]*bm4[i]*t)*((bm4[i]*bm4[i]+H2*H2)/(L*(bm4[i]*bm4[i]+H2*H2)+H2))*(Math.cos(rad*bm4[i]*(x)))*((Math.sin(rad*bm4[i]*L)/(bm4[i])));                         }   
	  return 2*To*sum;   
	     
	 }
     
	 public void printcase4()
	 {
     double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i=0;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=0.1)	 
	    {block[tt][i]=Txt_4(x,tt);i++;}	 
	    
     }
	 System.out.println(Matrix.toString(block)); 	 
     }	
  
//////////////
/// case5 ///
////////////        


     public double[] Bm5()
     {
	    double xx[]=new double[25]; 
	    xx[0]=0.0;
	    double y=Math.PI/L;
	    for(int i=1;i<25;i++)
	    {xx[i]=i*Math.PI/L;
	    //System.out.println(xx[i-1]);
	    }
	    return xx;   
	 }
 
	 public double[] Nbm5()
	 {
	  double bm5[]=Bm5();
	  double[] nbm=new double[25];	 
	    for(int i=0;i<25;i++)
	    {
		  if(bm5[i]==0.0)
		  {nbm[i]=1/L;}
		  else
		  {nbm[i]=2/L;}
	       nbm[24]=1/L;
		   //System.out.println(nbm[i]); 	  
	    }	
	    return bm5; 
	 }
	 
	 public double Txt_5(double x,double t)
	 {
		double bm5[]=Bm5(); 
		double nbm5[]=Nbm5();
		double sum=0.0;
		for(int i=0;i<25;i++)
		{sum+=Math.exp(-alfa*bm5[i]*bm5[i]*t)*nbm5[i]*Math.cos(rad*bm5[i]*x)*(Math.sin(rad*bm5[i]*L)/(bm5[i]+0.00001));}
		//System.out.println(sum*To);
		return To*sum;
	 }
	 
	 public void printcase5()
	 {
	 double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i=0;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=dx)	 
	    {block[tt][i]=Txt_5(x,tt);i++;}	 
	 }
	 System.out.println(Matrix.toString(block)); 	 
     }
////////////////////////
//// case 6 ////////////
////////////////////////

     public double[] Bm6()
     {
	  double xx[]=new double[25];
	  for(int i=0;i<25;i++)   
	  {xx[i]=((2.0*i)+1.0)*(Math.PI/(2.0*L));}       
	  return xx;    
	 }
	 
	 public double Txt_6(double x,double t)
	 {
		 double[] bm6=Bm6();
		 double sum=0.0;
		 for(int i=0;i<25;i++)
		 {sum+=Math.exp(-alfa*bm6[i]*bm6[i]*t)*Math.cos(rad*bm6[i]*x)*(Math.sin(rad*bm6[i]*L)/(bm6[i]));}
		 return 2.0*sum*To/L; 
	 }
	 
	 public void printcase6()
	 {
	 double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i=0;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=dx)	 
	    {block[tt][i]=Txt_6(x,tt);i++;}	 
	 }
	 System.out.println(Matrix.toString(block)); 	 
     }
     
//////////////
//// case7 ///
//////////////
    
public  double secant(case7 c7,double x)
{
int nmax=100;
double tolerance=1.0e-10;
double fx,dfx;
double dx=0.01;
for(int i=0;i<nmax;i++)
{
  fx=c7.func7(x,h2,k2,L);
  dfx=(c7.func7(x+dx,h2,k2,L)-c7.func7(x-dx,h2,k2,L))/(2.0*dx);
  x-=fx/dfx;
  if(Math.abs(fx)<tolerance) { return x;}
}
JOptionPane.showMessageDialog(null,"Warning : Maximum number of iteration is exceeded \n"+
   " çözüm geçerli olmıyabilir","MAXİMUM ITERATION NUMBER WARNING",JOptionPane.WARNING_MESSAGE);
return x;
}

     public double[] Bm7()
     {
	    double xx[]=new double[25]; 
	    int i=0;
	    for(double xinit=Math.PI-0.05;xinit<25*Math.PI;xinit=xinit+Math.PI)
	    {
	    xx[i]=secant(cc7,xinit);
        //System.out.println("bm = "+xx[i]);
	    i++;
	    }
	    //System.out.println();
	 return xx;   
	 }

	 public double Txt_7(double x,double t)
	 {
		 double bm7[]=Bm7();
		 //System.out.println(Matrix.toString(bm7));
		 double sum=0.0;
		 for(int i=0;i<25;i++)
		 {sum+=Math.exp(-alfa*bm7[i]*bm7[i]*t)*   ((bm7[i]*bm7[i]+H2*H2)/(L*(bm7[i]*bm7[i]+H2*H2)+H2))*(Math.sin(rad*bm7[i]*x))*((1.0/bm7[i])-(Math.cos(rad*bm7[i]*L)/bm7[i]));     }
		 return 2*To*sum;
	 }
	 public void printcase7()
	 {
	 double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i=0;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=dx)	 
	    {block[tt][i]=Txt_7(x,tt);i++;}	 
	 }
	 System.out.println(Matrix.toString(block)); 	 
     }
	 
///////////////
//// case8 ////
///////////////

public double[] Bm8()
{
double[] xx=new double[25];	
for(int i=0;i<25;i++)
{xx[i]=(2*i+1)*Math.PI/L;
}
return xx;	
}

public double Txt_8(double x,double t)
{
double[] bm8=Bm8();	
double sum=0.0;

for(int i=0;i<25;i++)
{sum+=Math.exp(-alfa*bm8[i]*bm8[i]*t)*Math.sin(rad*bm8[i]*x)*((1/bm8[i])-(Math.cos(rad*bm8[i]*L)/(bm8[i])));}
return 2*To*sum/L;	
}
   
     public void printcase8()
	 {
	 double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i=0;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=dx)	 
	    {block[tt][i]=Txt_8(x,tt);i++;}	 
	 }
	 System.out.println(Matrix.toString(block)); 	 
     }  
     
/////////////
/// case9 ///
///////////// 

     public double[] Bm9()
     {
      double xx[]=new double[25]; 	     
	  xx[0]=0.0;
      for(int i=1;i<25;i++)
      {xx[i]=i*Math.PI/L;}  
      return xx; 
	 }
	 
	 public double Txt_9(double x,double t)
	 {
	  double[] bm9=Bm9();
	  double sum=0.0;
	  for(int i=0;i<25;i++)
	  {sum+=Math.exp(-alfa*bm9[i]*bm9[i]*t)*Math.sin(rad*bm9[i]*x)*((1/(bm9[i]+0.01))-(Math.cos(rad*bm9[i]*L)/(bm9[i]+0.0001))); }
	  return 2.0*sum*To/L; 
		 
     }
	  
     public void printcase9()
	 {
	 double dx=0.1;
	 int M=(int)(((L-0.0)/dx)+1.0);
	 double[][] block=new double[20][M];	 
	 double t=0.0;
	 int i;	 
	 
	 for(int tt=0;tt<20;tt++)
	 {   i=0;
	    for(double x=0;x<L;x+=dx)	 
	    {block[tt][i]=Txt_9(x,tt);i++;}	 
	 }
	 System.out.println(Matrix.toString(block)); 	 
     }



     
	public static void main(String args[])
    {
	     
	    double x=1.5;
	    //double t=10;
	    
	     
	     double h1=5;
	     double k1=30;
	     double h2=7;
	     double k2=25;
	     double L=1.0;
	    // double To=100;//C
	     double kwall=20;
	     double rowall=300;
	     double Cpwall=100;
	     double Tinit=100;
	      // double xinit =1*Math.PI-0.01;
	     case1 cc1=new case1();
	     temp_dist_in_slab tt=new temp_dist_in_slab(h1,k1,h2,k2,L,Tinit,kwall,rowall,Cpwall);
	   
	    //tt.printcase1();
	    tt.printcase2();
	    //tt.printcase3();
	    //tt.printcase4();
	    //tt.printcase5();
	    //tt.printcase6();
	    //tt.printcase7();
	    //tt.printcase8();
	    //tt.printcase9();
	    
	    
	}








}