Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package polynomial;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileReader;
  11. import java.text.DecimalFormat;
  12. import java.util.Scanner;
  13.  
  14. /**
  15.  *
  16.  * @author casey
  17.  */
  18. public class Polynomial {
  19.  
  20.    
  21.     public static void main(String[] args) throws FileNotFoundException {
  22.        
  23.         Scanner read = new Scanner(new BufferedReader(new FileReader("poly.txt")));
  24.        
  25.             //first line into x array
  26.             String line = read.nextLine();
  27.             System.out.println("LINE: "+line);
  28.             String[] x = line.split("\\s+");
  29.            
  30.             //second line into y array
  31.             line = read.nextLine();
  32.             System.out.println("LINE: "+line);
  33.             String[] y = line.split("\\s+");    
  34.        
  35.         read.close();
  36.        
  37.         Double[][] array = new Double[(x.length)*2][y.length+1];
  38.        
  39.         for(int i = 0; i<x.length*2;i++){
  40.             for(int j = 0;j<array[0].length;j++){
  41.                 array[i][j] = 0.0;
  42.             }
  43.         }
  44.        
  45.         DecimalFormat df = new DecimalFormat("###.###");  
  46.          int a = 0;
  47.            
  48.         for(int i = 0; i<(x.length*2); i=i+2){  
  49.            
  50.             array[i][0] = Double.parseDouble(x[a]);
  51.             a++;
  52.            
  53.         }
  54.         a=0;
  55.         for(int j = 0; j<(y.length*2); j=j+2){  
  56.             array[j][1] = Double.parseDouble(y[a]);
  57.             a++;
  58.  
  59.         }
  60.        
  61.         //LAGRANGES METHODS
  62.         /*System.out.println("x: ");
  63.         for(int i=0; i<x.length;i++){
  64.             System.out.print(x[i]+ "\t");
  65.         }
  66.         System.out.println("\ny: ");
  67.         for(int i=0; i<y.length;i++){
  68.             System.out.print(y[i]+ "\t");
  69.         }
  70.         System.out.println();
  71.        
  72.         System.out.println("l_0");
  73.         int c = 0;
  74.         Double[] denominator = new Double[x.length];
  75.         int[] values = new int[x.length];
  76.         for(int i = 0; i<x.length;i++){
  77.             values[i] = i;
  78.         }
  79.        
  80.         for(int i = 0; i<1; i++){
  81.         denominator[i] = ((Double.parseDouble(x[c]) - Double.parseDouble(x[c+1])) * (Double.parseDouble(x[c]) - Double.parseDouble(x[c+2]))
  82.                  * (Double.parseDouble(x[c]) - Double.parseDouble(x[c+3])));
  83.         }
  84.         System.out.println("Denominator: "+denominator[0]);*/
  85.        
  86.         //DIVIDED METHOD
  87.    
  88.        
  89.         System.out.println("x\t"+"f[]\t"+"f[,]\t"+"f[,,]\t"+"f[,,,]");
  90.        
  91.        
  92.         int i = 0;
  93.         for(int j=2; j<x.length-1;j++ ){  
  94.           for(i = 0; i <2*(x.length-1); i=i+2){  
  95.             array[i+1][j] = (((array[i+2][j-1]) - (array[i][j-1])) /((array[i+j][0]) - (array[i][0])));  
  96.             System.out.println("i+j: "+(i+j));
  97.           }
  98.            System.out.println("i: "+i);
  99.         }
  100.        
  101.        
  102.        
  103.        
  104.        //i=6;
  105.        //j=0;
  106.            // array[i-2][j+3] = ((array[i-1][j+2]) - (array[i-3][j+2])) /((array[i][j]) - (array[i-4][j]));
  107.            // array[i-4][j+3] = ((array[i-3][j+2]) - (array[i-5][j+2])) /((array[i-2][j]) - (array[i-6][j]));
  108.             //array[i-3][j+4] = ((array[i-2][j+3]) - (array[i-4][j+3])) /((array[i][j]) - (array[i-6][j]));
  109.            
  110.        
  111.        
  112.         for(int s = 0; s<x.length*2; s++){
  113.             for(int t = 0; t<y.length+1; t++){
  114.                 //System.out.println("Piece: "+df.format(array[0][0]));
  115.                  System.out.print((array[s][t])+"\t");
  116.             }
  117.            
  118.             System.out.println();
  119.         }
  120.        
  121.        
  122.         String polynomial = String.valueOf(df.format(array[0][1]));
  123.         String xValue ="";
  124.         int j = 1;
  125.         int count = 0;
  126.         for(int I=0; I<x.length-1;I++){
  127.             xValue = xValue+"(x-"+x[count]+")";
  128.            
  129.             count++;
  130.             System.out.println("Before i: "+I+" j: "+j);
  131.            
  132.             polynomial = polynomial + " + " + array[I+1][j+1]+ xValue;
  133.             j++;
  134.         }
  135.         System.out.println("Interpolating polynimal is:");
  136.         System.out.println(polynomial);
  137.      
  138.     }
  139.    
  140.    
  141.    
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement