import java.io.*;
import visad.*;
import visad.java3d.DisplayImplJ3D;
import visad.java2d.DisplayImplJ2D;
import java.rmi.RemoteException;

class HT_xt_FD1
{ 
  public static double[] pivotlugauss(double a[][],double b[])
  { //kısmi pivotlu gauss eleme yöntemi
  int n=b.length;	  
  double x[]=new double[n];
  double carpan=0;
  double toplam=0;
  double buyuk;
  double dummy=0;
  //gauss eleme
  int i,j,k,p,ii,jj;
  //System.out.println("Orijinal Matris :\n"+Matrix.toString(a));
  for(k=0;k<(n-1);k++)
  { //pivotlama
	p=k;
	buyuk=Math.abs(a[k][k]);
	for(ii=k+1;ii<n;ii++)
	{  dummy=Math.abs(a[ii][k]);
	   if(dummy > buyuk) {buyuk=dummy;p=ii;}
	}
	if(p!=k)
	{ for(jj=k;jj<n;jj++)
	  { dummy=a[p][jj];
	    a[p][jj]=a[k][jj];
	    a[k][jj]=dummy;
	  }
	  dummy=b[p];
	  b[p]=b[k];
	  b[k]=dummy;
	}
	  //System.out.println("pivotlu Matris : k="+k+"\n"+Matrix.toString(a));
	//gauss elemeyi çözme  
    for(i=k+1;i<n;i++)
    {  carpan=a[i][k]/a[k][k];
       a[i][k]=0;
       for(j=k+1;j<n;j++)
       {   a[i][j]-=carpan*a[k][j]; }
           b[i]   =b[i]   -carpan*b[k];
	}
  }
  //geriye doğru yerine koyma
  x[n-1]=b[n-1]/a[n-1][n-1];
  for(i=n-2;i>=0;i--)
  {
    toplam=0;
    for(j=i+1;j<n;j++)
    {  toplam+=a[i][j]*x[j];}
    x[i]=(b[i]-toplam)/a[i][i];
  }
  return x;
  }
  	     
  public static double [][] HT_xtcoz( double L,double t, double dt,double T0[],double alpha,double Tinf,double h, double kc)
  {
  //  
  int nx=T0.length;
  double dx=L/nx;
  int nt=(int)(t/dt+0.001);
  double r=alpha*dt/(dx*dx);
  if(r>0.5) System.out.println("r="+r+" is larger than 0.5");
  double T[][]=new double[nt][nx];
  for(int m=0;m<nx;m++) {T[0][m]=T0[m];} 
  for(int i=1;i<nt;i++)
  { T[i][0]=(-T[i-1][1]+h*dx/kc*Tinf)/(1.0+h*dx/kc*Tinf);
    T[i][nx-1]=(-T[i-1][nx-2]+h*dx/kc*Tinf)/(1.0+h*dx/kc*Tinf);
	for(int m=1;m<(nx-1);m++)
    { T[i][m]=r*(T[i-1][m-1]+T[i-1][m+1])+(1-2.0*r)*T[i-1][m]; 
  }
  }
  return T;
  }
       
  public static void main(String args[]) throws IOException,RemoteException, VisADException
  {
	  double T0[]=new double[100];
	  for(int m=0;m<T0.length;m++) {T0[m]=400.0;}
	  double a[][]=HT_xtcoz(1.0,20.0,0.01,T0,17.7e-4,20.0,500.0,200.0);
	  Text.print(a);
	  Plot3D.data(a,0.0,20.0,0.0,1.0,20,"x","t","T=f(x,t)");
  }
}