//B spline eğri uydurma
import java.io.*;
import javax.swing.*;

class SCO11M
{ 
  public static double[] hi(double ti[])
  {
  int n=ti.length-1;//ti 0..n
  int nn=n+2;
  double h[]=new double[nn];
  for(int i=1;i<=n;i++)
     {h[i]=ti[i]-ti[i-1];
     }
  h[0]=h[1];
  h[n+1]=h[n];
  return h;
  }
  
  public static double [] B_Spline(double ti[],double yi[])
  {
  int n=ti.length-1;
  int nn=n+2;
  int i;
  double delta,gamma,p,q,r;
  double a[]=new double[nn];
  double h[]=new double[nn];
  h=hi(ti);
  delta=-1;
  gamma=2.0*yi[0];
  p=delta*gamma;
  q=2.0;
  for(i=1;i<=n;i++)
  {r=h[i+1]/h[i];
  delta=-r*delta;
  gamma=-r*gamma+(r+1)*yi[i];
  p=p+gamma*delta;
  q=q+delta*delta;
  }
  a[0]=-p/q;
  for(i=1;i<=(n+1);i++)
  {a[i]=((h[i-1]+h[i])*yi[i-1]-h[i]*a[i-1])/h[i-1];}
  return a;
 }
 
  public static double funcB_Spline(double ti[],double a[],double x)
  {
  double h[]=hi(ti);
  int i;
  int n=ti.length-1;
  double nn=n+2;
  double d,e;
  for(i=(n-1);i>=1;i--)
    {if((x-ti[i])>=0) break;}
  i++;
  d=(a[i+1]*(x-ti[i-1])+a[i]*(ti[i]-x+h[i+1]))/(h[i]+h[i+1]);
  e=(a[i]*(x-ti[i-1]+h[i-1])+a[i-1]*(ti[i-1]-x+h[i]))/(h[i-1]+h[i]);
  double bs=(d*(x-ti[i-1])+e*(ti[i]-x))/h[i];
  return bs;
  }
   
 public static double[][]  funcB_Spline(double ti[],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=ti.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double A[]=B_Spline(ti,yi);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {  z[0][k]=ti[i];z[1][k]=funcB_Spline(ti,A,z[0][k]);k++;
          for(int j=0;j<aradegersayisi;j++) 
             {dx=(ti[i+1]-ti[i])/((double)aradegersayisi+1.0);
          z[0][k]=z[0][k-1]+dx;z[1][k]=funcB_Spline(ti,A,z[0][k]);k++;}
       }
       z[0][k]=ti[i];z[1][k]=funcB_Spline(ti,A,z[0][k]);
    return z;
 } 
     
  public static void main(String args[]) throws IOException
  {  
  double x[]={0.0,  0.6 ,1.5 ,1.7 ,1.9 ,2.1,2.3 ,2.6 ,2.8,3.0 ,3.6 ,4.7  ,5.2  ,5.7  ,5.8  , 6.0 , 6.4 ,6.9,7.6,8.0};
  double y[]={-0.8,-0.34,0.59,0.59,0.23,0.1,0.28,1.03,1.5,1.44,0.74,-0.82,-1.27,-0.92,-0.92,-1.04,-0.79,-0.06,1.0,0.0};
  int n=x.length;
  double z1[][]=funcB_Spline(x,y,20);
  //System.out.println(" z1\n"+Matrix.toString(z1)); 
  Plot pp=new Plot(z1[0],z1[1]);
  pp.setPlabel("B-spline yöntemi eğri uydurma");
  pp.setXlabel("x");
  pp.setYlabel("y");
  pp.setPlotType(0,0); 
  pp.addData(x,y);
  pp.setPlotType(1,24); 
  pp.plot();
  }
  }
