import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.swing.*;

public class HT_wall_x_t
{

public static double fx(double lambda,double Bi)
{return lambda*Math.tan(lambda)-Bi;}

public static double dfx(double x,double h,double Bi)
{ //Türev hesabı
  return (-fx((x+2.0*h),Bi)+8.0*fx((x+h),Bi)-8.0*fx((x-h),Bi)+fx((x-2.0*h),Bi))/(12.0*h);
}

public static double Cn(double ln,double Bi)
{
 return 4.0*Math.sin(ln)/(2.0*ln+Math.sin(2.0*ln));
}

public static double lambda(double n,double Bi)
{
 //root of the non-linear equation : bisection-Newton Raphson method
 //lambda*tan(lambda)-Bi=0 
 double x1=(n-1)*Math.PI;
 double x2=	x1+Math.PI/2-0.000001;
double xacc=1.0e-10; //limit of error
double h=0.0001; //derivative step
int MAXIT=100;  //Maximum number of iterations
int j;
double dfr,dx,dxold,fr,fh,fl;
double temp,b,a,r;
fl=fx(x1,Bi);
fh=fx(x2,Bi);
//System.out.println("x1 ="+x1+"fl="+fl+"x2="+x2+"fh="+fh);
//Check if there is any root in the region
if ((fl > 0.0 && fh > 0.0) || (fl < 0.0 && fh < 0.0))
System.out.println("n = "+n+"x1="+x1+"x2="+x2+"verilen sınırlar içinde kök değeri yok");
if (fl == 0.0) return x1;
if (fh == 0.0) return x2;
if (fl < 0.0)   { a=x1;b=x2;} 
else            { b=x1;a=x2;}
r=0.5*(x1+x2); //orta nokta değeri 
dxold=Math.abs(x2-x1); 
dx=dxold; 
fr=fx(r,Bi);   //value of function at r
dfr=dfx(r,h,Bi); //value of derivative of function at r
for (j=1;j<=MAXIT;j++) 
{ 
if ((((r-b)*dfr-fr)*((r-a)*dfr-fr) > 0.0) || (Math.abs(2.0*fr) > Math.abs(dxold*dfr))) 
    { //bisection
	  dxold=dx;
      dx=0.5*(b-a);
      r=a+dx;
      if (a == r) return r; //çözüm!!!!
    } 
else{ //Newton-Raphson
	  dxold=dx;
      dx=fr/dfr;
      temp=r;
      r -= dx;
      if (temp == r) return r;  //çözüm!!!!!
    }
if (Math.abs(dx) < xacc) return r; //çözüm!!!!!
fr=fx(r,Bi);
dfr=dfx(r,h,Bi);
if (fr < 0.0) 
a=r;
else
b=r;
}
System.out.println("Maximum iteration number is exceeded");
return 0.0; //program should not reach here
}

public static double teta(double Fo,double Bi,double x)
{double tt=0;double ln=0;
 for(int n=1;n<20;n++)
 {ln=lambda(n,Bi);tt+=Cn(ln,Bi)*Math.exp(-ln*ln*Fo)*Math.cos(ln*x);
 System.out.println("ln="+ln+"n="+n);
 }
 return tt;	
}

     public static void main (String args[])  
     { fb f=new fb(); 
       double Bi,Fo,x;
       Fo=Double.parseDouble(JOptionPane.showInputDialog(" Forier sayısı Fo : "));
       Bi=Double.parseDouble(JOptionPane.showInputDialog(" Biot sayısı   Bi : "));
       x=Double.parseDouble(JOptionPane.showInputDialog("         x* = x/L : "));
       JOptionPane.showMessageDialog(null," teta = "+teta(Fo,Bi,x),
       "time dependent heat transfer in wall  : ",JOptionPane.PLAIN_MESSAGE);
       System.exit(0); 
     } 
}
