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

public class HT_kanat
{
// ucu izole kanat genelleştirilmiş sıcaklık profili
  public static double [] gauss_seidel(double a[][],double b[],double x[],double lambda)
  {
  //denklem sistemi çözümü b iteratif gauss_seidel metodu metodu
  //yakınsama faktörü lambda ile birlikte verilmiş formül	  
  int n=a.length;
  int imax=1000;//maksimum iterasyon sayısı 
  double es=1.0e-10;
  double dummy=0;
  double ea=1e-1;
  double sum=0;
  double old=0;
  int iter=0;
  int sentinel=0;
  //System.out.print("iter = "+(iter+1)+" ");
  for(int i=0;i<n;i++)
  {  dummy=a[i][i];
     for(int j=0;j<n;j++)
       {a[i][j]/=dummy;}
     b[i]/=dummy;
  }
  for(int i=0;i<n;i++)
  {  sum=b[i];
     for(int j=0;j<n;j++)
       {if(i!=j)sum-=a[i][j]*x[j];}
     x[i]=sum;
     //System.out.print(x[i]+ "  ");  
  }
   //System.out.println();  
  iter++;
  do
  {
  sentinel=1;
  //System.out.print("iter = "+(iter+1)+" ");  
  for(int i=0;i<n;i++)
     { 
	 old=x[i];
	 sum=b[i];
	 for(int j=0;j<n;j++)
	    {if(i!=j) sum-=a[i][j]*x[j];}
	    x[i]=lambda*sum+(1.0-lambda)*old;
	    if(sentinel==1 && x[i]!=0.0 ) {ea=Math.abs((x[i]-old)/x[i])*100.0;}
	    if(ea<es) sentinel=0;
	  //System.out.print(x[i]+ "  ");     
     }
  //System.out.println();  
  iter++;
  }while(sentinel==1 && iter<imax);  
  if(iter>=imax) System.out.println("Maksimum iterasyon sayısı aşıldı sonuç geçerli olmayabilir iter = "+iter);
  return x;
  }
  
  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_kanatcoz( double L,double dx, double m)
{
int n=(int)(L/dx+0.000001);
double A[][]=new double[n][n];
double b[]=new double[n];
double c=-(2+m*m*dx*dx);
for(int i=1;i<(n-1);i++)
{A[i][i]=c;A[i][i-1]=1.0;A[i][i+1]=1.0;}
A[0][0]=c;A[0][1]=1.0;b[0]=-1.0;
A[n-1][n-1]=c;A[n-1][n-2]=2.0;
double T[]=pivotlugauss(A,b);
return T;
}

public static double [][] HT_analitik_kanat( double L, double dx,double m)
{
int n=(int)(L/dx+0.000001);
double T[][]=new double[2][n];
double x=0;
for(int i=0;i<n;i++)
{x=i*dx;
T[1][i]=Math.cosh(m*(L-x))/Math.cosh(m*L);
T[0][i]=x;}
return T;
}
  public static void main(String args[]) 
  {
	  double T1[]=HT_kanatcoz(0.1,0.001,16.0);
	  double T2[][]=HT_analitik_kanat(0.1,0.001,16.0);
	  Text.printT(T1,"numeric solution");
	  Text.print(Text.T(T2),"analytic solution");
	  Plot pp=new Plot(T2[0],T1);
	  pp.addData(T2[0],T2[1]);
	  pp.plot();
  }
}