import java.io.*;
import javax.swing.*;

class f1 extends f_x
{ double func(double x)
  {return Math.sin(5.0*x);}
}
class f2 extends f_x
{ double func(double x)
  {return 5.0*Math.cos(5.0*x);}
}
class NA81B
{     
  public static double [] thomas(double a[][],double r[])
  {
  //Padé formula
  int n=a.length;
  double f[]=new double[n];
  double e[]=new double[n];
  double g[]=new double[n];
  double x[]=new double[n];
  for(int i=0;i<n;i++)     {f[i]=a[i][i];}
  for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];}
  for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];}
  for(int k=1;k<n;k++)
     {e[k]=e[k]/f[k-1];
      f[k]=f[k]-e[k]*g[k-1];
     }
  for(int k=1;k<n;k++)
     {r[k]=r[k]-e[k]*r[k-1];
     }
  x[n-1]=r[n-1]/f[n-1];   
  for(int k=(n-2);k>=0;k--)
     {x[k]=(r[k]-g[k]*x[k+1])/f[k];}
  return x;      
  }
  
  public static double [] thomas(double f[],double e[],double g[],double r[])
  {
  int n=f.length;
  double x[]=new double[n];
  for(int k=1;k<n;k++)
     {e[k]=e[k]/f[k-1];
      f[k]=f[k]-e[k]*g[k-1];
     }
  for(int k=1;k<n;k++)
     {r[k]=r[k]-e[k]*r[k-1];
     }
  x[n-1]=r[n-1]/f[n-1];   
  for(int k=(n-2);k>=0;k--)
     {x[k]=(r[k]-g[k]*x[k+1])/f[k];}
  return x;      
  }
  public static double[][] df(f_x ff,double x0,double xn, int n)
  {
  int nn=n+1;
  double h=(xn-x0)/n;
  double y[]=new double[nn];
  double x[]=new double[nn];
  double f[]=new double[nn];
  double g[]=new double[nn];
  double e[]=new double[nn];
  for(int i=0;i<=n;i++)
  {x[i]=x0+h*i;}
  
  for(int i=0;i<=n;i++)
  {
   if(i==0) {f[i]=1;}
   else if(i==n) {f[i]=1;}
   else {f[i]=4;}
   if(i==1) {g[i]=2;}
   else if(i==n) {g[i]=0;}
   else {g[i]=1;}
   if(i==(n-1)) {e[i]=2;}
   else if(i==0) {e[i]=0;}
   else {e[i]=1;}
   if(i==0) {y[i]=(ff.func(x[3])+8.0*ff.func(x[2])-8.0*ff.func(x[1])+ff.func(x[0]))/(12.0*h);}
   else if(i==n) {y[i]=(ff.func(x[n])+8.0*ff.func(x[n-1])-8.0*ff.func(x[n-2])+ff.func(x[n-3]))/(12.0*h);;}
   else {y[i]=(1.0/h*3.0*(ff.func(x[i+1])-ff.func(x[i-1])));}
  }
   double a[][]=new double[3][nn];
   a[0]=x;
   a[1]=y;
   a[2]=thomas(f,e,g,y);
   return a;
   }
  public static void main(String args[]) throws IOException
  {  
   double a[][]=df(new f1(),0.0,3.0, 200);
   Plot pp=new Plot(a[0],a[2]);
   int n=a[0].length;
   double y[]=new double[n];
   f2 dy=new f2();
   for(int i=0;i<n;i++)
   {y[i]=dy.func(a[0][i]);
    System.out.println(a[0][i]+" "+a[2][i]+" "+y[i]);
   }   
   pp.addData(a[0],y);
   pp.plot();
  }
  }
