import java.io.*;

class SCO11D
{ 
  public static double[][]  Lagrange(double xi[],double yi[])
  {
	 int n=xi.length;
     double L[][]=new double[2][n];
     for(int i=0;i<n;i++)
     {L[0][i]=yi[i];
     for(int j=0;j<n;j++)
     {if(i!=j) L[0][i]/=(xi[i]-xi[j]);}
     }
     for(int i=0;i<n;i++)
     {L[1][i]=xi[i];}
     return L;
  }
  
	
  public static double  funcLagrange(double L[][],double x)
  {
	 int n=L[0].length;
     double carpim=1.0;
     double toplam=0;
     for(int i=0;i<n;i++)
     {carpim=L[0][i];
     for(int j=0;j<n;j++)
     {if(i!=j) carpim*=(x-L[1][j]);}
     toplam+=carpim;
     }
     return toplam;
  }
  
 
 public static double[][]  funcLagrange(double xi[],double yi[],int aradegersayisi)
 {
	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	int n=xi.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double Q[][]=Lagrange(xi,yi);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {  z[0][k]=xi[i];z[1][k]=funcLagrange(Q,z[0][k]);k++;
          for(int j=0;j<aradegersayisi;j++) 
             {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0);
          z[0][k]=z[0][k-1]+dx;z[1][k]=funcLagrange(Q,z[0][k]);k++;}
       }
       z[0][k]=xi[i];z[1][k]=funcLagrange(Q,z[0][k]);
    return z;
 }  
     
  public static void main(String args[]) throws IOException
  {
  double x[]={1.3,1.6,1.9};
  double y[]={0.620086,0.4554022,0.281818186};

  double z1[][]=funcLagrange(x,y,8);
  System.out.println("Lagrange interpolasyonu\n"+Matrix.toStringT(z1)); 
   Text.print(Text.T(z1),"Lagrange interpolasyon  yöntemi ile eğri uydurma");
  Plot pp=new Plot(x,y);
  pp.setPlabel("Lagrange interpolasyonu yöntemi ile eğri uydurma");
  pp.setPlotType(0,20); 
  pp.addData(z1[0],z1[1]);
  pp.plot();  
  }
}